Skip to content

fix(ai-adapters): cancel orphaned stream handlers on turn cancel - #1302

Merged
wsp1911 merged 1 commit into
GCWing:mainfrom
wsp1911:main
Jun 24, 2026
Merged

fix(ai-adapters): cancel orphaned stream handlers on turn cancel#1302
wsp1911 merged 1 commit into
GCWing:mainfrom
wsp1911:main

Conversation

@wsp1911

@wsp1911 wsp1911 commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

Stop detached provider stream handlers from continuing to wait for SSE / TTFT after a dialog turn has already been cancelled.

Fixes #

Type and Areas

Type:

bug fix / regression fix / test / refactor

Areas:

AI adapters

Motivation / Impact

After a user cancelled a dialog turn, the top-level stream processor stopped correctly, but the provider-side SSE handler could remain alive in the background and continue waiting for TTFT or later stream events. That produced late timeout errors and kept network work alive after the user had already cancelled the turn.

This change moves stream-handler lifecycle ownership into execute_sse_request, wraps the returned stream in a managed stream type, and cancels the underlying handler task when the response stream is dropped. Provider adapters now return handler futures instead of spawning detached tasks themselves.

Impact:

  • cancelled turns no longer leave orphaned SSE handler tasks behind
  • late TTFT timeout logs after user cancellation should stop
  • provider stream lifecycle behavior is centralized in one shared adapter path

Verification

  • cargo test -p bitfun-ai-adapters managed_response_stream_drop_cancels_handler_task
  • cargo test -p bitfun-ai-adapters ttft_timeout_waits_for_first_effective_stream_output_not_http_200 -- --exact
  • cargo check -p bitfun-ai-adapters

Reviewer Notes

  • This is intentionally a shared lifecycle fix rather than a provider-specific patch.
  • The cancellation behavior now lives in the common SSE request helper, which should reduce future drift between OpenAI, Responses, Anthropic, and Gemini streaming paths.
  • The real desktop cancellation repro was analyzed from app.log, but that exact end-to-end manual repro was not rerun after the code change in this pass.

Checklist

  • This PR is focused and does not include secrets, temporary prompts, generated scratch files, or unrelated artifacts.
  • Relevant verification is recorded above, or skipped checks are explained.
  • User-facing strings, docs, and locales are updated where applicable.

- manage provider stream handler lifecycle in execute_sse_request
- cancel background SSE handlers when the response stream is dropped
- stop provider adapters from spawning detached stream tasks
- add coverage for managed stream cancellation on drop
@wsp1911
wsp1911 marked this pull request as ready for review June 24, 2026 11:19
@wsp1911
wsp1911 merged commit 94ed215 into GCWing:main Jun 24, 2026
4 checks passed
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.

1 participant