Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion plugins/web_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## Unreleased

*None.*
- Stop the connect and receive timeout timers once a request has ended.
They stayed armed until they fired, keeping each request and its response
in memory for up to the configured timeout.

## 2.2.2

Expand Down
10 changes: 9 additions & 1 deletion plugins/web_adapter/lib/src/adapter_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,17 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
options.onReceiveProgress!(event.loaded, event.total);
}
},
onDone: () => stopWatchReceiveTimeout(),
);

// DOM event streams never close, so the timers must be stopped once the
// request ends (load, error, abort or timeout). An armed timer keeps the
// XHR and its response reachable until it fires.
web.EventStreamProviders.loadEndEvent.forTarget(xhr).first.then((_) {
connectTimeoutTimer?.cancel();
connectTimeoutTimer = null;
stopWatchReceiveTimeout();
});

xhr.onError.first.then((_) {
connectTimeoutTimer?.cancel();
// Unfortunately, the underlying XMLHttpRequest API doesn't expose any
Expand Down
42 changes: 42 additions & 0 deletions plugins/web_adapter/test/timers_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@TestOn('browser')
import 'dart:async';

import 'package:dio/browser.dart';
import 'package:dio/dio.dart';
import 'package:test/test.dart';

void main() {
test('no timeout timer stays active once the request has completed',
() async {
const connectTimeout = Duration(minutes: 3);
const receiveTimeout = Duration(minutes: 5);
final timers = <Timer>[];
final dio = DioForBrowser(
BaseOptions(
connectTimeout: connectTimeout,
receiveTimeout: receiveTimeout,
),
);

// Record the timers armed by the adapter for this request.
final response = await runZoned(
() => dio.get<String>('data:text/plain,${'x' * 200000}'),
zoneSpecification: ZoneSpecification(
createTimer: (self, parent, zone, duration, callback) {
final timer = parent.createTimer(zone, duration, callback);
if (duration >= connectTimeout) {
timers.add(timer);
}
return timer;
},
),
);
// `loadend` is dispatched right after `load`.
await Future<void>.delayed(const Duration(milliseconds: 50));

expect(response.data, hasLength(200000));
expect(timers, isNotEmpty, reason: 'the adapter armed its timeout timers');
// An armed timer keeps the XHR and its response reachable until it fires.
expect(timers.where((timer) => timer.isActive), isEmpty);
});
}