Skip to content

fix(flask): isolate event loops for concurrent SSE responses - #182

Open
pcbeingused33 wants to merge 2 commits into
genkit-ai:mainfrom
pcbeingused33:fix/flask-concurrent-sse
Open

pcbeingused33 wants to merge 2 commits into
genkit-ai:mainfrom
pcbeingused33:fix/flask-concurrent-sse

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Concurrent Flask SSE requests to the same flow share the event loop captured when the handler is created. If one WSGI thread is driving the loop, the next stream raises RuntimeError: This event loop is already running before it can deliver a chunk.

Create an event loop inside each WSGI response iterator. Closing the iterator first closes the async stream, which lets the flow finish its cancellation cleanup, then closes the loop.

Tests

python -m pytest -o addopts= -q packages/genkit-flask/tests --tb=short passed all 33 tests. The new regression test holds the first invocation open until the second response delivers both its chunk and result, then verifies that the first response also completes. On the base revision, the second response raises the event-loop error.

This change was prepared with AI assistance.

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM! Pushed a one-line formatting fix (09b8ee3) adding the blank line after the docstring in _handler.py so ruff format --check passes.

@huangjeff5
huangjeff5 enabled auto-merge (squash) November 30, 2026 19:52
@huangjeff5

Copy link
Copy Markdown
Collaborator

@pcbeingused33 — pushing that small commit triggered a fresh cla/google check, which flagged a GitHub username mismatch (it looks like your CLA is still under pcbeingused333). Once you update your GitHub username at https://cla.developers.google.com/clas and click the rescan link in the cla/google check details, auto-merge will take care of the rest!

@pcbeingused33

Copy link
Copy Markdown
Contributor Author

@googlebot I fixed it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants