Skip to content

fix(dio): release response streams when consumers cancel - #2624

Open
JiyadAhammad wants to merge 1 commit into
cfug:mainfrom
JiyadAhammad:fix/2618-response-stream-cancellation
Open

JiyadAhammad wants to merge 1 commit into
cfug:mainfrom
JiyadAhammad:fix/2618-response-stream-cancellation

Conversation

@JiyadAhammad

Copy link
Copy Markdown

Consumer cancellation of a ResponseType.stream response now cancels the adapter-response subscription, stops receive-timeout work, and awaits asynchronous source cleanup. Normal completion and later CancelTokens do not trigger duplicate closures.

Closes #2618

Motivation & Background

When a consumer stopped consuming a response stream early (e.g. response.data!.stream.first, take(), or manual subscription cancellation), handleResponseStream had no onCancel handler. As a result, the adapter socket remained connected and receiving bytes, the receive timer kept ticking, and late CancelToken invocations threw StateError.

Verification

Added 6 regression tests covering .first, direct cancellation, paused cancellation, asynchronous cleanup, and late tokens after normal completion/error. All 16 tests in dio/test/response/response_stream_test.dart pass on the Dart VM. Formatting and dart analyze are clean.

@cfug/dio-core This touches timeout/cancellation cleanup as called out in AGENTS.md §3. Public APIs and adapter signatures remain unchanged.

Implementation and tests developed with Antigravity assistance.

New Pull Request Checklist

  • I have read the Documentation
  • I have read the Agent Contribution Guidelines (required if any part of the change was produced with AI assistance)
  • I have searched for a similar pull request in the project and found none
  • I have updated this branch with the latest main branch to avoid conflicts (via merge from master or rebase)
  • I have added the required tests to prove the fix/feature I'm adding
  • I have updated the documentation (if necessary)
  • I have run the tests without failures
  • I have updated the CHANGELOG.md in the corresponding package

Additional context and info (if any)

Consumer cancellation of a ResponseType.stream response now cancels the adapter-response subscription, stops receive-timeout work, and awaits asynchronous source cleanup. Normal completion and later CancelTokens do not trigger duplicate closures.

Closes cfug#2618

Co-authored-by: Antigravity <antigravity@google.com>
@JiyadAhammad
JiyadAhammad requested a review from a team as a code owner October 18, 2026 17:54
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.

Cancelling a response stream subscription leaves the adapter response subscribed

1 participant