Skip to content

Conversation

@Danilospano00
Copy link

Fixes a bug where QueuedInterceptor would cause requests to hang indefinitely when an interceptor callback threw a synchronous exception.

The _handleQueue method now accepts an onError callback that properly completes the handler when a synchronous exception is caught, ensuring requests fail gracefully instead of hanging.

New Pull Request Checklist

  • I have read the Documentation
  • 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)

Description

Fixes a bug where QueuedInterceptor would cause requests to hang indefinitely when an interceptor callback threw a synchronous exception.

Problem

When using QueuedInterceptor, if an interceptor callback (onRequest, onResponse, or onError) threw a synchronous exception (not wrapped in a Future), the request would hang until timeout. This occurred because the _handleQueue method's try-catch block caught the exception but only called _processNextInQueue() without completing the handler's Completer, leaving the request waiting forever.

Changes

Modified _handleQueue to accept an onError callback parameter that properly handles synchronous exceptions:

  • onRequest/onResponse: Wraps the error in DioException and calls handler.reject() to fail the request gracefully
  • onError: Wraps the error in DioException and calls handler.next() to pass the error to the next interceptor in the chain

Danilospano added 3 commits January 5, 2026 10:51
Fixes a bug where QueuedInterceptor would cause requests to hang
indefinitely when an interceptor callback threw a synchronous exception.

The _handleQueue method now accepts an onError callback that properly
completes the handler when a synchronous exception is caught, ensuring
requests fail gracefully instead of hanging.
@Danilospano00 Danilospano00 requested a review from a team as a code owner January 5, 2026 10:01
@AlexV525
Copy link
Member

AlexV525 commented Jan 5, 2026

Thanks for the fix. I was wondering:

  1. Can the code add more comments to tell people what happens at this place?
  2. Does it also fix A request with an interceptor, which produces an error, makes the code stuck. #2138, which fix: Catch sync/async* exceptions in interceptor's handlers #2139 didn't make it?

@Danilospano00
Copy link
Author

Thanks for the review!

I've added some comments.

This PR fixes the same type of issue as #2138, but for QueuedInterceptor instead of only the regular Interceptor.

I've also added test that reproduces the #2138 scenario with QueuedInterceptor to verify that the fix works correctly.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 5, 2026

Code Coverage Report: Only Changed Files listed

Package Base Coverage New Coverage Difference
dio/lib/src/interceptor.dart 🟢 97.66% 🟢 98.6% 🟢 0.94%
Overall Coverage 🟢 87.95% 🟢 88.1% 🟢 0.15%

Minimum allowed coverage is 0%, this run produced 88.1%

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.

2 participants