Skip to content

Commit 17220a3

Browse files
committed
fix: handle missing edge cases on retry request fn
1 parent b186b29 commit 17220a3

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

lib/http/intercepted_client.dart

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,33 @@ class InterceptedClient extends BaseClient {
297297

298298
// Set up timeout with proper cleanup
299299
Timer? timeoutTimer;
300-
late StreamSubscription streamSubscription;
300+
bool isCompleted = false;
301301

302302
timeoutTimer = Timer(requestTimeout!, () {
303-
if (!completer.isCompleted) {
303+
if (!isCompleted) {
304+
isCompleted = true;
304305
if (onRequestTimeout != null) {
305306
// If timeout callback is provided, use it
306-
final timeoutResponse = onRequestTimeout!();
307-
if (timeoutResponse is Future<StreamedResponse>) {
308-
timeoutResponse.then((response) {
307+
try {
308+
final timeoutResponse = onRequestTimeout!();
309+
if (timeoutResponse is Future<StreamedResponse>) {
310+
timeoutResponse.then((response) {
311+
if (!completer.isCompleted) {
312+
completer.complete(response);
313+
}
314+
}).catchError((error) {
315+
if (!completer.isCompleted) {
316+
completer.completeError(error);
317+
}
318+
});
319+
} else {
309320
if (!completer.isCompleted) {
310-
completer.complete(response);
321+
completer.complete(timeoutResponse);
311322
}
312-
});
313-
} else {
323+
}
324+
} catch (error) {
314325
if (!completer.isCompleted) {
315-
completer.complete(timeoutResponse);
326+
completer.completeError(error);
316327
}
317328
}
318329
} else {
@@ -328,13 +339,19 @@ class InterceptedClient extends BaseClient {
328339
// Handle the actual request completion
329340
requestFuture.then((streamResponse) {
330341
timeoutTimer?.cancel();
331-
if (!completer.isCompleted) {
332-
completer.complete(streamResponse);
342+
if (!isCompleted) {
343+
isCompleted = true;
344+
if (!completer.isCompleted) {
345+
completer.complete(streamResponse);
346+
}
333347
}
334348
}).catchError((error) {
335349
timeoutTimer?.cancel();
336-
if (!completer.isCompleted) {
337-
completer.completeError(error);
350+
if (!isCompleted) {
351+
isCompleted = true;
352+
if (!completer.isCompleted) {
353+
completer.completeError(error);
354+
}
338355
}
339356
});
340357

0 commit comments

Comments
 (0)