Skip to content

fix(py): stop nested hop() cancel from hitting RecursionError on 3.10/3.11 - #7001

Merged
huangjeff5 merged 1 commit into
mainfrom
fix/py-generate-hop-recursion
Nov 2, 2026
Merged

huangjeff5 merged 1 commit into
mainfrom
fix/py-generate-hop-recursion

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

On Python 3.10/3.11, cancelling a generate() run that has nested enough hop() calls raises RecursionError instead of CancelledError. Found on CI run 40817731842 (3.10 and 3.11) while working on #6947, in test_generate_with_400_middleware_cancelled_on_a_later_turn_raises_cancelled_error. It reproduces on main (effdb1471, local 3.10), unrelated to that PR, so it lands on its own. #6947 lands after this.

Mechanism: hop() awaited its child task directly (await asyncio.create_task(child())). Each direct await task sets parent._fut_waiter = child_task, turning a stack of hops into an N-deep Task -> Task linked list. On Python 3.10 and 3.11, task.cancel() and repr(task) walk _fut_waiter recursively on the call stack (one frame per hop). With a 400-entry use= list, that chain exceeds the recursion limit.

Fix: extract await_unchained(task) into genkit._core._compat (hop() itself stays a one-line await await_unchained(asyncio.create_task(child()))):

  • While not task.done(), the caller suspends on a leaf Future[None] resolved by task.add_done_callback(...), so parent._fut_waiter never points to another Task (cancel() and repr() stay O(1) in stack depth).
  • If the caller is cancelled while waiting, await_unchained forwards task.cancel(msg) and keeps waiting on fresh leaf Futures until task finishes cleanup—matching standard await task semantics (including absorbed CancelledError, late caller cancels, and KeyboardInterrupt/SystemExit returned by child()).
  • Once task finishes, return task.result() propagates its value, exception, or CancelledError.
# 400 middleware, cancelled on turn 2, recursion limit pinned at 1000
sys.setrecursionlimit(1000)
assert repr(task)
# => before: RecursionError on 3.10/3.11
task.cancel()
with pytest.raises(asyncio.CancelledError):
    await task
# => before: RecursionError on 3.10/3.11; after: CancelledError on 3.10-3.14

Where to look

  • py/packages/genkit/src/genkit/_core/_compat.py — await_unchained + _wake_waiter (~35 lines + docstring).
  • py/packages/genkit/src/genkit/_ai/_generate.py — hop() swaps await asyncio.create_task(child()) for await await_unchained(asyncio.create_task(child())).
  • py/packages/genkit/tests/genkit/core/compat_test.py — unit tests for await_unchained (normal result, raised error, cancel cleanup, absorbed cancel, late cancel race, 500-deep chain under sys.setrecursionlimit(200)).
  • py/packages/genkit/tests/genkit/ai/generate_test.py — pins sys.setrecursionlimit(1000) + repr(task) on the 400-middleware turn-2 cancel test.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the hop function in genkit/_ai/_generate.py to prevent Task._fut_waiter from chaining across hops. By having the parent await a local Future instead of the child task directly, task.cancel() and repr(task) maintain O(1) stack depth complexity on Python 3.10 and 3.11. Additionally, a test case has been updated to verify this behavior under a reduced recursion limit. There are no review comments, and I have no feedback to provide.

…/3.11

Extract await_unchained(task) into genkit._core._compat so hop() awaits a leaf Future per step instead of chaining Task._fut_waiter across nested tasks.
@huangjeff5
huangjeff5 force-pushed the fix/py-generate-hop-recursion branch from 28fe738 to 8f35511 Compare November 2, 2026 12:06

@pavelgj pavelgj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@huangjeff5
huangjeff5 merged commit 9da9303 into main Nov 2, 2026
25 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-generate-hop-recursion branch November 2, 2026 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants