Repository navigation
Conversation
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().
There was a problem hiding this comment.
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.
|
@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. |
|
@huangjeff5 @Blevene, friendly ping on this one. It's still mergeable against 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! |
|
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). |
Summary
loop_local_clientto prune closed-loop cache entries so warm Cloud Run / sync callers that donew_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).genkit_flaskstreaming to resolve a live event loop per request (never reuse a closedget_event_loop()result captured at decorator time).Fixes #4925
cc @Blevene
Test plan
pytestloop_cache_test.py, flask handler tests, andtest_googleai_model_action_survives_closed_event_loops(12 passed)Event loop is closed/ retry noise