Skip to content

fix(py): avoid Event loop is closed on warm sync Cloud Run callers - #5996

Closed
a2105z wants to merge 2 commits into
genkit-ai:mainfrom
a2105z:fix/py-event-loop-closed-warm-instances
Closed

a2105z wants to merge 2 commits into
genkit-ai:mainfrom
a2105z:fix/py-event-loop-closed-warm-instances

Conversation

@a2105z

@a2105z a2105z commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Harden loop_local_client to prune closed-loop cache entries so warm Cloud Run / sync callers that do new_event_loop() → run_until_complete → loop.close() per request get a fresh google-genai client on the live loop (Python: 'Event loop is closed' on warm Cloud Run instances with sync callers #4925).
  • Fix genkit_flask streaming to resolve a live event loop per request (never reuse a closed get_event_loop() result captured at decorator time).
  • Add regression tests for loop-cache warm-instance reuse, Flask closed-loop recovery, and GoogleAI model actions across sequential closed loops.

Fixes #4925

cc @Blevene

Test plan

  • pytest loop_cache_test.py, flask handler tests, and test_googleai_model_action_survives_closed_event_loops (12 passed)
  • Reproduce the issue sync bridge on a warm Cloud Run instance and confirm no Event loop is closed / retry noise

a2105z added 2 commits August 3, 2026 03:24
Harden loop-local client caching to prune closed loops, resolve a live
Flask streaming loop per request, and lock in a Cloud Run-style
regression test so google-genai clients are not reused across closed loops.
Address review: use a lazy loop runner so Flask can close the temporary
async-handler loop before streaming iteration, and prune with pop().
@a2105z

a2105z commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

@Blevene ready for review when you have a moment.

This locks in the loop-local client behavior for the Cloud Run sync warm-instance case from #4925 (plus Flask closed-loop recovery). Happy to adjust anything.

@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 addresses issues with closed event loops in warm environments (such as Cloud Run) by ensuring live event loops are resolved per request and pruning closed-loop entries from the loop-local client cache. The review feedback highlights a critical issue in the Flask handler where calling _create_loop() immediately inside an async function can still result in a closed loop error during generator iteration, suggesting a lazy loop proxy instead. Additionally, it is recommended to use pop instead of del when pruning closed loops from the weak dictionary to avoid potential KeyError exceptions.

@a2105z

a2105z commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

@huangjeff5 ready for review when you have a moment.

This builds on the loop-local google-genai client pattern from #4812 to close out #4925 for warm sync Cloud Run / Flask callers. Happy to adjust anything that doesn’t match how you want the Python runtime ownership to work.

@a2105z

a2105z commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@huangjeff5 @Blevene, friendly ping on this one. It's still mergeable against main with no conflicts.

It closes out #4925 (the "Event loop is closed" error on warm sync Cloud Run instances) by building on the loop-local client pattern from #4812, with tests for the loop cache, the Flask handler, and the google-genai plugin. The fork CI workflows may need a maintainer to approve them. Happy to adjust anything. Thanks!

@MichaelDoyle

Copy link
Copy Markdown
Contributor

Thanks @a2105z! Python SDK development has moved to https://github.com/genkit-ai/genkit-python, and several related lifecycle changes have landed since this PR was opened — including @loop_local_client (#6708), decoupling genkit-flask from genkit-google-genai (#6631), private _-prefixed plugin internals (#6508), and flask[async] (genkit-ai/genkit-python#42). Closing here due to the repository move and module drift; if #4925 still reproduces on genkit-python, please feel free to open a fresh PR there (keeping genkit-flask decoupled from genkit-google-genai).

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.

Python: 'Event loop is closed' on warm Cloud Run instances with sync callers

2 participants