Skip to content

fix(dio): keep QueuedInterceptor callbacks serial after cancellation - #2648

Open
martinusgalih wants to merge 1 commit into
cfug:mainfrom
martinusgalih:fix/queued-interceptor-cancel-overlap
Open

martinusgalih wants to merge 1 commit into
cfug:mainfrom
martinusgalih:fix/queued-interceptor-cancel-overlap

Conversation

@martinusgalih

@martinusgalih martinusgalih commented Dec 5, 2026 •

Copy link
Copy Markdown

Fixes #2643.

When the active request of a QueuedInterceptor was cancelled, the queue released its slot immediately on cancellation, so the next queued callback could start while the cancelled callback was still running. With a token-refresh interceptor this runs two refreshes at once, which breaks the serial guarantee QueuedInterceptor exists to provide. With the repro from the issue, the refresh runs twice before this change and once after.

This change releases the slot on cancellation only after the cancelled callback has returned (either it completed the handler, or it returned without doing so after seeing the cancellation). advance stays idempotent, so the normal path and the cancellation path cannot double-advance. For QueuedInterceptorsWrapper, whose callbacks are typed void, the wrapper records the callback's runtime Future on the handler so the queue can wait for it.

Trade-off to review

The existing test "should not stall the queue when the active request is cancelled mid-callback" asserted that the next request runs while the cancelled callback is still awaiting (a 5-second delay). That is exactly the overlap #2643 reports, so I changed that test to release the cancelled callback after cancellation instead of waiting on a fixed delay. The consequence: if a cancelled callback awaits a future that never completes, the queue now waits for it instead of moving on. I think serial execution is the right default for QueuedInterceptor, but please say if you would rather keep the old release-on-cancel behaviour, for example behind an option.

Testing

  • New regression test fails on main and passes with this change.
  • dart analyze and dart format clean; full dio VM test suite passes (254 passed, 6 skipped).
  • Not run: web and http2 adapter suites.

Before / After

Before and after: the new regression test fails on main and passes with this change

Test output as text

Before (main):

00:00 +0 -1: QueuedInterceptor does not start the next callback before the cancelled active callback completes [E]
  Expected: false
    Actual: <true>
  The next queued callback started while the cancelled active callback was still running.
00:00 +0 -1: Some tests failed.

After (this PR), dart test test/queued_interceptor_test.dart:

00:00 +9: QueuedInterceptor does not start the next callback before the cancelled active callback completes
00:00 +10: All tests passed!

AI disclosure: implementation and tests were written by OpenCode (DeepSeek); the diff was reviewed and the tests were re-run locally with Claude Opus 5.5 before submitting. I've read the change and will respond to review myself.

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

Release a cancelled QueuedInterceptor task only after its callback has returned, instead of immediately on cancellation. This preserves the serial guarantee for token-refresh style interceptors while still unblocking the queue when a cancelled callback returns without completing its handler.

Closes cfug#2643

Co-Authored-By: OpenCode <noreply@opencode.ai>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@martinusgalih
martinusgalih requested a review from a team as a code owner December 5, 2026 04:03
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.

QueuedInterceptor callbacks can overlap after the active request is cancelled (since 5.10.0)

1 participant