Skip to content

Commit

Permalink
fix(langgraph): abort graph execution when config.signal is aborted (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincburns authored Jan 22, 2025
1 parent 31131e6 commit db24751
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/langgraph/src/pregel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,7 @@ export class Pregel<
);
}
},
signal: config.signal,
});
}
if (loop.status === "out_of_steps") {
Expand Down
38 changes: 38 additions & 0 deletions libs/langgraph/src/tests/pregel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9429,6 +9429,44 @@ graph TD;
const thirdState = await graph.getState(config);
expect(thirdState.tasks).toHaveLength(0);
});

it("should cancel when signal is aborted", async () => {
let oneCount = 0;
let twoCount = 0;
const graph = new StateGraph(MessagesAnnotation)
.addNode("one", async () => {
oneCount += 1;
await new Promise((resolve) => setTimeout(resolve, 100));
return {};
})
.addNode("two", () => {
twoCount += 1;
throw new Error("Should not be called!");
})
.addEdge(START, "one")
.addEdge("one", "two")
.addEdge("two", END)
.compile();

const abortController = new AbortController();
const config = {
signal: abortController.signal,
};

setTimeout(() => abortController.abort(), 10);

await expect(
async () =>
await graph.invoke(
{
messages: [],
},
config
)
).rejects.toThrow("Aborted");
expect(oneCount).toEqual(1);
expect(twoCount).toEqual(0);
});
}

runPregelTests(() => new MemorySaverAssertImmutable());

0 comments on commit db24751

Please sign in to comment.