Repository navigation
fix(py): stop nested hop() cancel from hitting RecursionError on 3.10/3.11 - #7001
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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.
huangjeff5
force-pushed
the
fix/py-generate-hop-recursion
branch
from
November 2, 2026 04:34
8f5355b to
28fe738
Compare
…/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
force-pushed
the
fix/py-generate-hop-recursion
branch
from
November 2, 2026 12:06
28fe738 to
8f35511
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Python 3.10/3.11, cancelling a
generate()run that has nested enoughhop()calls raisesRecursionErrorinstead ofCancelledError. Found on CI run 40817731842 (3.10 and 3.11) while working on #6947, intest_generate_with_400_middleware_cancelled_on_a_later_turn_raises_cancelled_error. It reproduces onmain(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 directawait tasksetsparent._fut_waiter = child_task, turning a stack of hops into an N-deepTask -> Tasklinked list. On Python 3.10 and 3.11,task.cancel()andrepr(task)walk_fut_waiterrecursively on the call stack (one frame per hop). With a 400-entryuse=list, that chain exceeds the recursion limit.Fix: extract
await_unchained(task)intogenkit._core._compat(hop()itself stays a one-lineawait await_unchained(asyncio.create_task(child()))):not task.done(), the caller suspends on a leafFuture[None]resolved bytask.add_done_callback(...), soparent._fut_waiternever points to anotherTask(cancel()andrepr()stay O(1) in stack depth).await_unchainedforwardstask.cancel(msg)and keeps waiting on fresh leafFutures untiltaskfinishes cleanup—matching standardawait tasksemantics (including absorbedCancelledError, late caller cancels, andKeyboardInterrupt/SystemExitreturned bychild()).taskfinishes,return task.result()propagates its value, exception, orCancelledError.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()swapsawait asyncio.create_task(child())forawait await_unchained(asyncio.create_task(child())).py/packages/genkit/tests/genkit/core/compat_test.py— unit tests forawait_unchained(normal result, raised error, cancel cleanup, absorbed cancel, late cancel race, 500-deep chain undersys.setrecursionlimit(200)).py/packages/genkit/tests/genkit/ai/generate_test.py— pinssys.setrecursionlimit(1000)+repr(task)on the 400-middleware turn-2 cancel test.