Repository navigation
fix(dio): prevent dispatch after cancellation during encoding - #2647
Open
SalilHarit wants to merge 2 commits into
Open
SalilHarit wants to merge 2 commits into
SalilHarit wants to merge 2 commits into
Conversation
…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
marked this pull request as ready for review
December 2, 2026 05:44
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
@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
mainbranch to avoid conflicts (via merge from master or rebase)CHANGELOG.mdin the corresponding package