Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[patch]: Remove signal event listener when completed #6497

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions langchain-core/src/runnables/tests/signal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
FakeChatMessageHistory,
FakeListChatModel,
} from "../../utils/testing/index.js";
import { StringOutputParser } from "../../output_parsers/string.js";
import { Document } from "../../documents/document.js";
import { ChatPromptTemplate } from "../../prompts/chat.js";

const chatModel = new FakeListChatModel({ responses: ["hey"], sleep: 500 });

Expand Down Expand Up @@ -152,3 +155,48 @@ describe.each(Object.keys(TEST_CASES))("Test runnable %s", (name) => {
}).rejects.toThrowError();
});
});

test("Should not raise node warning", async () => {
const formatDocumentsAsString = (documents: Document[]) => {
return documents.map((doc) => doc.pageContent).join("\n\n");
};
const retriever = RunnableLambda.from(() => {
return [
new Document({ pageContent: "test1" }),
new Document({ pageContent: "test2" }),
new Document({ pageContent: "test4" }),
new Document({ pageContent: "test5" }),
];
});
const ragChainWithSources = RunnableMap.from({
// Return raw documents here for now since we want to return them at
// the end - we'll format in the next step of the chain
context: retriever,
question: new RunnablePassthrough(),
}).assign({
answer: RunnableSequence.from([
(input) => {
return {
// Now we format the documents as strings for the prompt
context: formatDocumentsAsString(input.context as Document[]),
question: input.question,
};
},
ChatPromptTemplate.fromTemplate("Hello"),
new FakeListChatModel({ responses: ["test"] }),
new StringOutputParser(),
]),
});

const stream = await ragChainWithSources.stream(
{
question: "What is the capital of France?",
},
{
signal: new AbortController().signal,
}
);
for await (const _ of stream) {
// console.log(_);
}
});
8 changes: 5 additions & 3 deletions langchain-core/src/utils/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export async function raceWithSignal<T>(
if (signal === undefined) {
return promise;
}
let listener: () => void;
return Promise.race([
promise.catch<T>((err) => {
if (!signal?.aborted) {
Expand All @@ -14,13 +15,14 @@ export async function raceWithSignal<T>(
}
}),
new Promise<never>((_, reject) => {
signal.addEventListener("abort", () => {
listener = () => {
reject(new Error("Aborted"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the remove call should be inside the listener?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add it in both places but it should be removed once the promise resolves too?

What's the benefit to putting it within the listener vs. in finally?

});
};
signal.addEventListener("abort", listener);
// Must be here inside the promise to avoid a race condition
if (signal.aborted) {
reject(new Error("Aborted"));
}
}),
]);
]).finally(() => signal.removeEventListener("abort", listener));
}
Loading