Skip to content

Commit 9af2521

Browse files
fix: cancel running subgraphs on v3 stream abort [closes #8029] (#8057)
## Description v3 event streaming's `stream.abort()` (sync and async) only closed the mux and stopped pumping, leaving the underlying `astream`/`stream` generator — and any running subgraphs — alive until they finished, burning resources. The fix closes the underlying graph iterator so `GeneratorExit` propagates into in-flight nodes/subgraphs and cancels them, matching v2's `aclose()` behavior. Fixes #8029. ## Release Note v3 streaming `stream.abort()` now cancels running subgraphs instead of letting them run to completion. ## Test Plan - [x] `TEST="tests/test_pregel_stream_events_v3.py -k abort" make test` (new `test_abort_cancels_running_subgraph` asserts the looping subgraph stops after abort) Made by [Open SWE](https://openswe.vercel.app) --------- Co-authored-by: Nick Hollon <[email protected]> Co-authored-by: open-swe[bot] <[email protected]>
1 parent 1a9baae commit 9af2521

2 files changed

Lines changed: 220 additions & 7 deletions

File tree

‎libs/langgraph/langgraph/stream/run_stream.py‎

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,23 @@ def _pump_next(self) -> bool:
132132
def abort(self) -> None:
133133
"""Stop the run early.
134134
135-
Closes the mux and marks the stream exhausted. The graph
136-
iterator is dropped; any in-flight nodes see the closure on
137-
their next yield point. Idempotent.
135+
Closes the underlying graph iterator (propagating `GeneratorExit`
136+
so in-flight nodes and subgraphs are cancelled), closes the mux,
137+
and marks the stream exhausted. Idempotent.
138138
"""
139139
if self._exhausted:
140140
return
141141
self._exhausted = True
142+
graph_iter = self._graph_iter
143+
self._graph_iter = None
144+
if (
145+
graph_iter is not None
146+
and (close := getattr(graph_iter, "close", None)) is not None
147+
):
148+
try:
149+
close()
150+
except Exception:
151+
pass
142152
try:
143153
self._mux.close()
144154
except Exception:
@@ -348,6 +358,8 @@ def __init__(
348358
self._scope_list: list[str] = list(mux.scope)
349359
self._pump_cond = asyncio.Condition()
350360
self._pumping = False
361+
self._anext_task: asyncio.Future[Any] | None = None
362+
self._aborting = False
351363
for key in mux.native_keys:
352364
setattr(self, key, mux.extensions[key])
353365
if wire_pump:
@@ -407,7 +419,25 @@ async def _apump_next(self) -> bool:
407419

408420
try:
409421
try:
410-
part = await self._graph_aiter.__anext__()
422+
# Run the pull as a child task so `abort()` can cancel it
423+
# mid-flight. Cancelling propagates `CancelledError` into the
424+
# graph generator frame -> Pregel loop -> nested subgraph
425+
# nodes, which a bare `aclose()` cannot do while the generator
426+
# is running ("asynchronous generator is already running").
427+
self._anext_task = asyncio.ensure_future(self._graph_aiter.__anext__())
428+
try:
429+
part = await self._anext_task
430+
except asyncio.CancelledError:
431+
if self._aborting:
432+
# Abort-initiated cancel: stop gracefully.
433+
self._exhausted = True
434+
return False
435+
# Genuine external cancel of this task: also stop the
436+
# in-flight pull, then propagate.
437+
self._anext_task.cancel()
438+
raise
439+
finally:
440+
self._anext_task = None
411441
event = convert_to_protocol_event(part)
412442
self._observe_event(event)
413443
await self._mux.apush(event)
@@ -428,15 +458,40 @@ async def _apump_next(self) -> bool:
428458
async def abort(self) -> None:
429459
"""Stop the run early.
430460
431-
Marks the stream exhausted, wakes any pump-waiters, and closes
432-
the mux. Any `apush` blocked on backpressure wakes and returns
433-
without appending. Idempotent.
461+
Marks the stream exhausted and wakes any pump-waiters. Cancels an
462+
in-flight pull if one is running, then closes the underlying graph
463+
iterator, so running nodes and nested subgraphs are cancelled
464+
whether or not a pump is mid-pull. Closes the mux; any `apush`
465+
blocked on backpressure wakes and returns without appending.
466+
Idempotent.
434467
"""
435468
async with self._pump_cond:
436469
if self._exhausted:
437470
return
438471
self._exhausted = True
472+
self._aborting = True
473+
graph_aiter = self._graph_aiter
474+
self._graph_aiter = None
475+
anext_task = self._anext_task
439476
self._pump_cond.notify_all()
477+
# If a pump is mid-pull, cancel it so the cancellation propagates
478+
# into running nodes and nested subgraphs. Once it settles the
479+
# generator is no longer running, so the `aclose()` below is a safe
480+
# final cleanup (and handles the no-in-flight-pull case directly).
481+
if anext_task is not None and not anext_task.done():
482+
anext_task.cancel()
483+
try:
484+
await anext_task
485+
except (asyncio.CancelledError, Exception):
486+
pass
487+
if (
488+
graph_aiter is not None
489+
and (aclose := getattr(graph_aiter, "aclose", None)) is not None
490+
):
491+
try:
492+
await aclose()
493+
except Exception:
494+
pass
440495
try:
441496
await self._mux.aclose()
442497
except Exception:

‎libs/langgraph/tests/test_pregel_stream_events_v3.py‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,164 @@ async def test_context_manager_calls_abort_on_exit(self) -> None:
607607
_ = await anext(aiter(run.values))
608608
assert run._exhausted is True
609609

610+
async def test_abort_cancels_running_subgraph(self) -> None:
611+
class CountState(TypedDict):
612+
count: int
613+
614+
runs: list[int] = []
615+
616+
async def sub_node(state: CountState) -> dict:
617+
runs.append(state["count"] + 1)
618+
await asyncio.sleep(0.05)
619+
return {"count": state["count"] + 1}
620+
621+
sub_graph = (
622+
StateGraph(CountState)
623+
.add_node("sub_node", sub_node)
624+
.set_entry_point("sub_node")
625+
.add_conditional_edges(
626+
"sub_node",
627+
lambda s: END if s["count"] >= 10 else "sub_node",
628+
)
629+
.compile()
630+
)
631+
632+
async def main_node(state: CountState) -> None:
633+
await sub_graph.ainvoke({"count": 0})
634+
635+
main_graph = (
636+
StateGraph(CountState)
637+
.add_node("main_node", main_node)
638+
.set_entry_point("main_node")
639+
.compile()
640+
)
641+
642+
run = await main_graph.astream_events({"count": 0}, version="v3")
643+
async for e in run:
644+
if (
645+
e["method"] == "values"
646+
and e["params"]["namespace"]
647+
and e["params"]["data"]["count"] >= 2
648+
):
649+
break
650+
await run.abort()
651+
runs_at_abort = len(runs)
652+
# Give the (now-cancelled) subgraph a chance to keep looping.
653+
await asyncio.sleep(0.3)
654+
assert len(runs) == runs_at_abort
655+
assert len(runs) < 10
656+
657+
async def test_abort_cancels_deeply_nested_subgraph(self) -> None:
658+
class CountState(TypedDict):
659+
count: int
660+
661+
runs: list[int] = []
662+
663+
async def deep_node(state: CountState) -> dict:
664+
runs.append(state["count"] + 1)
665+
await asyncio.sleep(0.05)
666+
return {"count": state["count"] + 1}
667+
668+
# Deepest graph loops until count >= 10.
669+
graph = (
670+
StateGraph(CountState)
671+
.add_node("deep_node", deep_node)
672+
.set_entry_point("deep_node")
673+
.add_conditional_edges(
674+
"deep_node",
675+
lambda s: END if s["count"] >= 10 else "deep_node",
676+
)
677+
.compile()
678+
)
679+
680+
# Wrap it three times: graph -> subgraph -> subgraph -> subgraph.
681+
for _ in range(3):
682+
683+
async def caller(state: CountState, _child: Any = graph) -> dict:
684+
return await _child.ainvoke({"count": 0})
685+
686+
graph = (
687+
StateGraph(CountState)
688+
.add_node("caller", caller)
689+
.set_entry_point("caller")
690+
.compile()
691+
)
692+
693+
run = await graph.astream_events({"count": 0}, version="v3")
694+
async for e in run:
695+
if (
696+
e["method"] == "values"
697+
and e["params"]["namespace"]
698+
and e["params"]["data"]["count"] >= 2
699+
):
700+
break
701+
await run.abort()
702+
runs_at_abort = len(runs)
703+
# Give the (now-cancelled) nested subgraph a chance to keep looping.
704+
await asyncio.sleep(0.3)
705+
assert len(runs) == runs_at_abort
706+
assert len(runs) < 10
707+
708+
async def test_abort_cancels_subgraph_during_inflight_pump(self) -> None:
709+
class CountState(TypedDict):
710+
count: int
711+
712+
started = asyncio.Event()
713+
cancelled = asyncio.Event()
714+
715+
async def sub_node(state: CountState) -> dict:
716+
started.set()
717+
try:
718+
# Long-running node: still in flight when abort fires.
719+
await asyncio.sleep(5)
720+
except asyncio.CancelledError:
721+
cancelled.set()
722+
raise
723+
return {"count": state["count"] + 1}
724+
725+
sub_graph = (
726+
StateGraph(CountState)
727+
.add_node("sub_node", sub_node)
728+
.set_entry_point("sub_node")
729+
.compile()
730+
)
731+
732+
async def main_node(state: CountState) -> None:
733+
await sub_graph.ainvoke({"count": 0})
734+
735+
main_graph = (
736+
StateGraph(CountState)
737+
.add_node("main_node", main_node)
738+
.set_entry_point("main_node")
739+
.compile()
740+
)
741+
742+
run = await main_graph.astream_events({"count": 0}, version="v3")
743+
744+
# A consumer task drives the pump. Once the subgraph node is
745+
# running, no further event is produced, so the consumer parks
746+
# inside _apump_next awaiting graph_aiter.__anext__() — the
747+
# generator is "running" and a plain aclose() would raise.
748+
async def consume() -> None:
749+
async for _e in run:
750+
pass
751+
752+
consumer = asyncio.create_task(consume())
753+
try:
754+
await asyncio.wait_for(started.wait(), timeout=2.0)
755+
# Let the consumer drain and park in __anext__.
756+
await asyncio.sleep(0.05)
757+
# Abort from a different task while the consumer is in __anext__.
758+
await run.abort()
759+
# The in-flight subgraph node must observe cancellation.
760+
await asyncio.wait_for(cancelled.wait(), timeout=2.0)
761+
finally:
762+
consumer.cancel()
763+
try:
764+
await consumer
765+
except asyncio.CancelledError:
766+
pass
767+
610768
async def test_extensions_has_native_keys(self) -> None:
611769
run = await _build_simple_graph().astream_events(
612770
{"value": "x", "items": []}, version="v3"

0 commit comments

Comments
 (0)