Skip to content

Commit 51c6771

Browse files
committed
fix(dio): guard dispatch-stage handler completion and add request context to duplicate-completion errors
Fixes #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.
1 parent 4684e29 commit 51c6771

4 files changed

Lines changed: 63 additions & 10 deletions

File tree

‎dio/CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
55

66
## Unreleased
77

8-
*None.*
8+
- Fix duplicate interceptor handler completions: the dispatch stage no longer double-completes a handler racing a cancellation, and the resulting `StateError` (if any) now identifies the offending request.
99

1010
## 5.11.1
1111

‎dio/lib/src/dio_mixin.dart‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ abstract class DioMixin implements Dio {
437437
return listenCancelForAsyncTask(
438438
requestOptions.cancelToken,
439439
Future(() async {
440-
final handler = RequestInterceptorHandler();
440+
final handler = RequestInterceptorHandler(requestOptions);
441441
final result = cb(state.data as RequestOptions, handler);
442442
_observeInterceptorCallback(
443443
result,
@@ -467,7 +467,7 @@ abstract class DioMixin implements Dio {
467467
return listenCancelForAsyncTask(
468468
requestOptions.cancelToken,
469469
Future(() async {
470-
final handler = ResponseInterceptorHandler();
470+
final handler = ResponseInterceptorHandler(requestOptions);
471471
final result = cb(state.data as Response, handler);
472472
_observeInterceptorCallback(
473473
result,
@@ -495,7 +495,7 @@ abstract class DioMixin implements Dio {
495495
? error
496496
: InterceptorState(assureDioException(error, requestOptions));
497497
Future<InterceptorState> handleError() async {
498-
final handler = ErrorInterceptorHandler();
498+
final handler = ErrorInterceptorHandler(requestOptions);
499499
final result = cb(state.data, handler);
500500
_observeInterceptorCallback(
501501
result,
@@ -552,9 +552,14 @@ abstract class DioMixin implements Dio {
552552
requestOptions = reqOpt;
553553
try {
554554
final value = await _dispatchRequest<T>(reqOpt);
555-
handler.resolve(value, true);
555+
// A racing cancellation may have already completed this handler.
556+
if (!handler.isCompleted) {
557+
handler.resolve(value, true);
558+
}
556559
} on DioException catch (e) {
557-
handler.reject(e, true);
560+
if (!handler.isCompleted) {
561+
handler.reject(e, true);
562+
}
558563
}
559564
return null;
560565
}),

‎dio/lib/src/interceptor.dart‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,29 @@ class InterceptorState<T> {
2121
}
2222

2323
abstract class _BaseHandler {
24+
_BaseHandler([this._requestOptions]);
25+
2426
final _completer = Completer<InterceptorState>();
2527
void Function()? _processNextInQueue;
2628

29+
// Identifies which request a duplicate-completion error belongs to.
30+
final RequestOptions? _requestOptions;
31+
2732
@protected
2833
Future<InterceptorState> get future => _completer.future;
2934

3035
bool get isCompleted => _completer.isCompleted;
3136

3237
void _throwIfCompleted() {
3338
if (_completer.isCompleted) {
39+
final requestOptions = _requestOptions;
40+
final requestDescription = requestOptions == null
41+
? ''
42+
: ' (${requestOptions.method} ${requestOptions.uri})';
3443
throw StateError(
3544
'The `handler` has already been called, '
36-
'make sure each handler gets called only once.',
45+
'make sure each handler gets called only once.'
46+
'$requestDescription',
3747
);
3848
}
3949
}
@@ -92,6 +102,8 @@ Object? _invokeCallbackDynamically<T, V extends _BaseHandler>(
92102

93103
/// The handler for interceptors to handle before the request has been sent.
94104
class RequestInterceptorHandler extends _BaseHandler {
105+
RequestInterceptorHandler([super.requestOptions]);
106+
95107
/// Deliver the [requestOptions] to the next interceptor.
96108
///
97109
/// Typically, the method should be called once interceptors done
@@ -152,6 +164,8 @@ class RequestInterceptorHandler extends _BaseHandler {
152164

153165
/// The handler for interceptors to handle after respond.
154166
class ResponseInterceptorHandler extends _BaseHandler {
167+
ResponseInterceptorHandler([super.requestOptions]);
168+
155169
/// Deliver the [response] to the next interceptor.
156170
///
157171
/// Typically, the method should be called once interceptors done
@@ -203,6 +217,8 @@ class ResponseInterceptorHandler extends _BaseHandler {
203217

204218
/// The handler for interceptors to handle error occurred during the request.
205219
class ErrorInterceptorHandler extends _BaseHandler {
220+
ErrorInterceptorHandler([super.requestOptions]);
221+
206222
/// Deliver the [error] to the next interceptor.
207223
///
208224
/// Typically, the method should be called once interceptors done

‎dio/test/interceptor_test.dart‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ void main() {
6161
allOf([
6262
isA<DioException>(),
6363
(DioException e) => e.error is StateError,
64-
(DioException e) => (e.error as StateError).message == message,
64+
(DioException e) =>
65+
(e.error as StateError).message.startsWith(message),
6566
]),
6667
),
6768
);
@@ -71,7 +72,8 @@ void main() {
7172
allOf([
7273
isA<DioException>(),
7374
(DioException e) => e.error is StateError,
74-
(DioException e) => (e.error as StateError).message == message,
75+
(DioException e) =>
76+
(e.error as StateError).message.startsWith(message),
7577
]),
7678
),
7779
);
@@ -81,12 +83,42 @@ void main() {
8183
allOf([
8284
isA<DioException>(),
8385
(DioException e) => e.error is StateError,
84-
(DioException e) => (e.error as StateError).message == message,
86+
(DioException e) =>
87+
(e.error as StateError).message.startsWith(message),
8588
]),
8689
),
8790
);
8891
});
8992

93+
test(
94+
'Duplicate handler call StateError identifies the offending request',
95+
() async {
96+
final dio = Dio()
97+
..options.baseUrl = MockAdapter.mockBase
98+
..httpClientAdapter = MockAdapter()
99+
..interceptors.add(
100+
InterceptorsWrapper(
101+
onRequest: (options, handler) {
102+
handler.next(options);
103+
handler.next(options);
104+
},
105+
),
106+
);
107+
await expectLater(
108+
dio.get('/test'),
109+
throwsA(
110+
allOf([
111+
isA<DioException>(),
112+
(DioException e) => e.error is StateError,
113+
(DioException e) =>
114+
(e.error as StateError).message.contains('GET') &&
115+
(e.error as StateError).message.contains('/test'),
116+
]),
117+
),
118+
);
119+
},
120+
);
121+
90122
group('InterceptorState', () {
91123
test('toString()', () {
92124
final data = DioException(requestOptions: RequestOptions());

0 commit comments

Comments
 (0)