Skip to content

fix(py): stop orphaned stream runs on reader disconnect - #51

Merged
huangjeff5 merged 3 commits into
mainfrom
fix/py-1.0-pr5-web-serving
Nov 13, 2026
Merged

huangjeff5 merged 3 commits into
mainfrom
fix/py-1.0-pr5-web-serving

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

A stream consumer (like an HTTP client) that disconnects mid-stream left the flow running indefinitely, continuing to execute tools and call models.

Changes

  • A reader that stops also stops the run: When a streaming reader task ends cancelled (e.g. client disconnect) or with an unhandled exception, StoppableStream triggers cancellation on the underlying generation task.
  • A timeout around a single anext() (an SSE heartbeat) does not cancel, and never iterating leaves the run going so await result.response alone continues to work as expected.

Cross-SDK

Matches JS and Go streaming cancellation semantics. Returned objects over HTTP are unmodified.

@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from 900cd4a to c69c814 Compare November 9, 2026 19:08
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from c69c814 to a3f7b4d Compare November 9, 2026 19:56
@huangjeff5
huangjeff5 changed the base branch from main to fix/py-1.0-pr4-google-genai November 9, 2026 19:56
@huangjeff5
huangjeff5 added this pull request to stack #59 November 9, 2026 19:58
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from cadb036 to 3f2f21f Compare November 9, 2026 20:02
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from a3f7b4d to 686f429 Compare November 9, 2026 20:02
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from 3f2f21f to 9b18e5e Compare November 9, 2026 20:21
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from 686f429 to 107f29d Compare November 9, 2026 20:21
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from 9b18e5e to 1a69ccf Compare November 9, 2026 21:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from 107f29d to b253ce2 Compare November 9, 2026 21:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from 1a69ccf to 1e3648e Compare November 9, 2026 21:27
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from b253ce2 to dccc917 Compare November 9, 2026 21:28
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from 1e3648e to 3a2f2c1 Compare November 9, 2026 21:52
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch 2 times, most recently from 64da694 to 5498539 Compare November 9, 2026 22:03
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch 2 times, most recently from 2da210f to d675ef3 Compare November 9, 2026 23:18
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from 5498539 to 7d62b4f Compare November 9, 2026 23:18
@huangjeff5 huangjeff5 changed the title fix(py): Django WSGI streaming, ModelResponse JSON isolation, run_main thread safety fix(py): Django WSGI streaming, ModelResponse JSON isolation, and run_main thread safety Nov 10, 2026
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from d675ef3 to 0693e72 Compare November 10, 2026 07:44
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from 7d62b4f to c1c3ea5 Compare November 10, 2026 07:44
@huangjeff5 huangjeff5 changed the title fix(py): Django WSGI streaming, ModelResponse JSON isolation, and run_main thread safety fix(py): stream SSE under WSGI, stop orphaned runs, and keep ModelResponse.request off the wire Nov 10, 2026
@huangjeff5
huangjeff5 removed this pull request from stack #59 November 10, 2026 18:16
@huangjeff5
huangjeff5 added this pull request to stack #75 November 10, 2026 18:16
@huangjeff5
huangjeff5 removed this pull request from stack #75 November 10, 2026 18:16
@huangjeff5
huangjeff5 added this pull request to stack #76 November 10, 2026 18:17
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from 0693e72 to 9beb25b Compare November 10, 2026 18:37
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from c1c3ea5 to df1b003 Compare November 10, 2026 18:37
@huangjeff5
huangjeff5 removed this pull request from stack #76 November 10, 2026 18:38
@huangjeff5
huangjeff5 added this pull request to stack #88 November 11, 2026 02:37
@huangjeff5
huangjeff5 removed this pull request from stack #88 November 11, 2026 06:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from 2ba52f0 to 8bea28f Compare November 11, 2026 06:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from 629dbb8 to 04c05b2 Compare November 11, 2026 06:22
@huangjeff5
huangjeff5 added this pull request to stack #89 November 11, 2026 06:23
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from 8bea28f to 5dce93a Compare November 11, 2026 16:21
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from 04c05b2 to d1b9115 Compare November 11, 2026 16:21
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-google-genai branch from 5dce93a to c29693d Compare November 11, 2026 17:31
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from d1b9115 to 383ccc5 Compare November 11, 2026 17:31
@huangjeff5
huangjeff5 removed this pull request from stack #89 November 11, 2026 18:06
@huangjeff5
huangjeff5 changed the base branch from fix/py-1.0-pr4-google-genai to fix/py-1.0-pr3d-prompt-loader-sse November 11, 2026 18:37
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3d-prompt-loader-sse branch from 1ac7d23 to b2313a4 Compare November 11, 2026 18:38
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch 2 times, most recently from f903918 to 39c7c49 Compare November 12, 2026 06:38
@huangjeff5 huangjeff5 changed the title fix(py): stream SSE under WSGI, stop orphaned runs, and keep ModelResponse.request off the wire fix(py): stop orphaned stream runs and keep ModelResponse.request off the wire Nov 12, 2026
@huangjeff5
huangjeff5 changed the base branch from fix/py-1.0-pr3d-prompt-loader-sse to main November 12, 2026 06:38
@huangjeff5
huangjeff5 requested a review from apascal07 November 12, 2026 06:59
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr5-web-serving branch from 39c7c49 to 3490b0e Compare November 12, 2026 07:07
@huangjeff5 huangjeff5 changed the title fix(py): stop orphaned stream runs and keep ModelResponse.request off the wire fix(py): stop orphaned stream runs on reader disconnect Nov 12, 2026

@apascal07 apascal07 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.

The non-streaming branch of the web handlers (genkit_fastapi/_handler.py:186, await action_obj.run(...), and the same shape in Flask and Django) still runs to completion after the client disconnects. JS express aborts that branch on response.on('close').

@huangjeff5

Copy link
Copy Markdown
Collaborator Author

@apascal07 Good catch on the non-streaming web handler branch—kept PR #51 scoped to streaming disconnect cleanup and filed #125 to track cancelling non-streaming action_obj.run(...) on client disconnect across FastAPI, Flask, and Django.

@huangjeff5

Copy link
Copy Markdown
Collaborator Author

@apascal07 Addressed the non-streaming web handler branch directly in 7c3ffb0 (closed #125):

  • FastAPI (genkit_fastapi/_handler.py): polls request.is_disconnected() while action_obj.run(...) is running, cancels the task on disconnect, and returns 499.
  • Django (genkit_django): ASGIHandler already races listen_for_disconnect(receive) against the view and cancels await flow.run(...) on http.disconnect—added test_django_non_streaming_client_disconnect_cancels_flow to verify it.
  • Flask (genkit_flask): WSGI (async_to_sync) has no pre-response disconnect channel while the handler is awaiting, so only the streaming SSE generator can cancel on hangup.

@huangjeff5
huangjeff5 merged commit 0793c85 into main Nov 13, 2026
20 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-1.0-pr5-web-serving branch November 13, 2026 03:46
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