Skip to content

Commit 1fa6704

Browse files
square-tombclaude
andcommitted
fix(mobile): abort timed-out bounded relay requests on the shared session
Retiring a generation only closes a SharedSessionClient, which leaves the URLSession task running, so sendBounded now sends an abortable copy of the request and aborts it on timeout, as post already does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent 9f9d228 commit 1fa6704

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎mobile/lib/shared/relay/relay_http_query_client.dart‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,17 @@ class RelayHttpQueryClient {
8484
required Duration timeout,
8585
http.Client Function(http.Client, bool Function())? decorate,
8686
}) async {
87-
request.followRedirects = false;
87+
// As in [post], a timed-out request on a shared session is aborted.
88+
final abort = Completer<void>();
89+
final sent =
90+
http.AbortableRequest(
91+
request.method,
92+
request.url,
93+
abortTrigger: abort.future,
94+
)
95+
..followRedirects = false
96+
..headers.addAll(request.headers)
97+
..bodyBytes = request.bodyBytes;
8898
final generation = _injectedClient == null
8999
? (_currentGeneration ??= _createGeneration())
90100
: null;
@@ -94,7 +104,7 @@ class RelayHttpQueryClient {
94104
Future<http.Response> receive() async {
95105
final transport = _injectedClient ?? generation!.client;
96106
final client = decorate?.call(transport, () => !finished) ?? transport;
97-
final response = await client.send(request);
107+
final response = await client.send(sent);
98108
if (finished) {
99109
await response.stream.listen(null).cancel();
100110
throw StateError('Relay request already timed out');
@@ -123,6 +133,7 @@ class RelayHttpQueryClient {
123133
try {
124134
return await receive().timeout(timeout);
125135
} on TimeoutException {
136+
abort.complete();
126137
if (identical(_currentGeneration, generation)) {
127138
_currentGeneration = null;
128139
}

‎mobile/test/shared/relay/relay_http_query_client_test.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ void main() {
7373
);
7474
await aborted.future.timeout(const Duration(seconds: 5));
7575
});
76+
77+
test('a timed-out bounded request is aborted, not left running', () async {
78+
final aborted = Completer<void>();
79+
final client = RelayHttpQueryClient(
80+
client: _HangingClient(onAbort: aborted.complete),
81+
);
82+
83+
await expectLater(
84+
client.sendBounded(
85+
http.Request('GET', Uri.parse('https://relay.example/')),
86+
maxBytes: 1024,
87+
timeout: const Duration(milliseconds: 10),
88+
),
89+
throwsA(isA<TimeoutException>()),
90+
);
91+
await aborted.future.timeout(const Duration(seconds: 5));
92+
});
7693
}
7794

7895
/// Never responds; reports when the request's abort trigger fires.

0 commit comments

Comments
 (0)