Skip to content

fix(dio): prevent dispatch after cancellation during encoding - #2647

Open
SalilHarit wants to merge 2 commits into
cfug:mainfrom
SalilHarit:fix/2613
Open

SalilHarit wants to merge 2 commits into
cfug:mainfrom
SalilHarit:fix/2613

Conversation

@SalilHarit

@SalilHarit SalilHarit commented Dec 2, 2026 •

Copy link
Copy Markdown

Closes #2613.

This follow-up builds on Fajrian Aidil Pratama’s request-context and dispatch-guard work in #2611.

Problem

When a request was cancelled while an async request encoder was pending, the caller received the cancellation before encoding finished. When encoding resumed, dispatch still asked the HTTP adapter to open a connection. Separately, a duplicate completion from an async interceptor could reach its Dart zone after the caller already received a response, with no request context in the error message.

Changes

  • Let dispatch and cancellation compete for a single result in the interceptor pipeline. Check cancellation before encoding and again before calling the HTTP adapter.
  • Include the interceptor’s request method and path in duplicate-completion errors. Omit URL credentials, query values, and fragments.

@AlexV525, this touches cancellation and interceptor result routing. The dispatch stage now forwards the winning result through the existing response and error interceptor paths.

Verification

A deterministic test reproduced the connection attempt after cancellation during request encoding and passes with the fix. Two additional cases verify that late decoder success and failure leave cancellation as the only interceptor outcome. A request-interceptor test covers the scenario raised in #2611 where an earlier interceptor replaces the request options. The async error-interceptor test verifies that the caller receives its recovered response and the callback’s zone receives the contextual StateError.

The final Dio VM suite run finished with 257 passed and 6 skipped. Static analysis and formatting checks passed.

Unverified

The exact production double-completion reported during a network handoff remains unconfirmed. The regression tests cover the observed late connection attempt and late decoder outcomes.

AI collaboration

I brought the original bug report and reproducer into the investigation and requested regression coverage for the cancellation race. OpenAI Codex helped investigate the race, implement the follow-up fix, and write and run the regression tests.

New Pull Request Checklist

ryanaidilp and others added 2 commits December 2, 2026 10:59
…text to duplicate-completion errors

Fixes cfug#2613

- Thread RequestOptions through _BaseHandler so the "handler already
  called" StateError identifies which request triggered it, instead of
  being a bare, untraceable message.
- Guard the post-_dispatchRequest handler.resolve/reject calls with
  isCompleted checks, since a racing cancellation can already have
  completed the handler.
Prevent a cancelled request from reaching the adapter after encoding. Report the interceptor request in duplicate-completion diagnostics and cover cancellation and replacement options with regression tests.

Co-Authored-By: Codex <codex@openai.com>
@SalilHarit
SalilHarit marked this pull request as ready for review December 2, 2026 05:44
@SalilHarit
SalilHarit requested a review from a team as a code owner December 2, 2026 05:44
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.

Duplicate handler completion can crash silently (untraceable) or throw an unguarded StateError during flaky connectivity

2 participants