Skip to content

Commit 96d2eac

Browse files
committed
fix(mobile): abort relay information reads that are dropped unread
The relay author and archive readers returned on a non-200 status, or timed out waiting for headers, without reading or aborting the response. On a shared URLSession the task kept downloading that body for the life of the app. Give both an abort trigger completed on every exit. Also correct the in-flight and upload-progress comments. Signed-off-by: Tom Brow <[email protected]>
1 parent 1dcda62 commit 96d2eac

5 files changed

Lines changed: 89 additions & 19 deletions

File tree

‎mobile/lib/shared/http/app_http_client.dart‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class SharedSessionClient extends http.BaseClient {
6060

6161
/// One abort trigger per request still running, so [close] can cancel it.
6262
/// A request leaves the set once its response body is done, failed or
63-
/// cancelled, so a long-lived handle holds nothing for finished requests.
63+
/// cancelled, or once the caller's own abort trigger fires. A body nobody
64+
/// listens to stays until then: the session keeps downloading it, so
65+
/// callers that drop a response unread must abort it.
6466
final _inFlight = <Completer<void>>{};
6567
var _closed = false;
6668
http.Client? _cleartext;
@@ -159,7 +161,8 @@ class SharedSessionClient extends http.BaseClient {
159161
..maxRedirects = request.maxRedirects
160162
..persistentConnection = request.persistentConnection;
161163
// addStream keeps backpressure: the body is read only as the session
162-
// reads the copy, so upload progress still tracks bytes actually sent.
164+
// reads the copy, so upload progress tracks bytes the session has taken
165+
// (including its read-ahead buffers), as with plain cupertino_http.
163166
unawaited(copy.sink.addStream(body).whenComplete(copy.sink.close));
164167
return copy;
165168
}

‎mobile/lib/shared/identity_archives/identity_archives.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ final relayInformationClientProvider = Provider<http.Client>((ref) {
2222
final identityArchivesProvider = FutureProvider<Set<String>>((ref) async {
2323
final sync = ref.watch(meSyncProvider);
2424
final client = ref.watch(relayInformationClientProvider);
25+
// Completed on every exit, so a dropped, refused or timed-out response
26+
// stops downloading on a shared session instead of staying in flight.
27+
final abort = Completer<void>();
2528
try {
2629
final uri = Uri.parse(sync.community);
2730
final response = await client
2831
.send(
29-
http.Request('GET', uri)
32+
http.AbortableRequest('GET', uri, abortTrigger: abort.future)
3033
..headers['Accept'] = 'application/nostr+json',
3134
)
3235
.timeout(const Duration(seconds: 3));
@@ -66,6 +69,8 @@ final identityArchivesProvider = FutureProvider<Set<String>>((ref) async {
6669
return (await _parseArchives(events, authority))..remove(sync.pubkey);
6770
} catch (_) {
6871
return {};
72+
} finally {
73+
if (!abort.isCompleted) abort.complete();
6974
}
7075
});
7176

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,35 @@ final relayAuthorProvider = FutureProvider.autoDispose.family<String?, String>((
2525
},
2626
);
2727
final client = ref.watch(relayInformationClientProvider);
28-
final response = await client
29-
.send(
30-
http.Request('GET', uri)..headers['Accept'] = 'application/nostr+json',
31-
)
32-
.timeout(const Duration(seconds: 3));
33-
final chunks = StreamIterator(
34-
response.stream.timeout(const Duration(seconds: 3)),
35-
);
28+
// Completed on every exit, so a dropped, refused or timed-out response
29+
// stops downloading on a shared session instead of staying in flight.
30+
final abort = Completer<void>();
3631
final bytes = <int>[];
3732
try {
33+
final response = await client
34+
.send(
35+
http.AbortableRequest('GET', uri, abortTrigger: abort.future)
36+
..headers['Accept'] = 'application/nostr+json',
37+
)
38+
.timeout(const Duration(seconds: 3));
3839
if (response.statusCode != 200) return null;
39-
await (() async {
40-
while (await chunks.moveNext()) {
41-
if (bytes.length + chunks.current.length > 256 * 1024) {
42-
throw const FormatException('Relay information exceeds size limit');
40+
final chunks = StreamIterator(
41+
response.stream.timeout(const Duration(seconds: 3)),
42+
);
43+
try {
44+
await (() async {
45+
while (await chunks.moveNext()) {
46+
if (bytes.length + chunks.current.length > 256 * 1024) {
47+
throw const FormatException('Relay information exceeds size limit');
48+
}
49+
bytes.addAll(chunks.current);
4350
}
44-
bytes.addAll(chunks.current);
45-
}
46-
})().timeout(const Duration(seconds: 3));
51+
})().timeout(const Duration(seconds: 3));
52+
} finally {
53+
await chunks.cancel();
54+
}
4755
} finally {
48-
await chunks.cancel();
56+
if (!abort.isCompleted) abort.complete();
4957
}
5058
final info = jsonDecode(utf8.decode(bytes));
5159
final author = info is Map ? info['self'] : null;

‎mobile/test/features/agents/agent_instructions_test.dart‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:convert';
23
import 'package:buzz/shared/relay/relay_author_provider.dart';
34
import 'package:hooks_riverpod/hooks_riverpod.dart';
@@ -91,6 +92,34 @@ void main() {
9192
}
9293
});
9394

95+
test('a refused relay information response is aborted', () async {
96+
Future<void>? aborted;
97+
final container = ProviderContainer(
98+
overrides: [
99+
relayInformationClientProvider.overrideWithValue(
100+
MockClient.streaming((request, _) async {
101+
aborted = (request as http.Abortable).abortTrigger;
102+
return http.StreamedResponse(
103+
StreamController<List<int>>().stream,
104+
503,
105+
);
106+
}),
107+
),
108+
],
109+
);
110+
addTearDown(container.dispose);
111+
final subscription = container.listen(
112+
relayAuthorProvider('wss://relay.example'),
113+
(_, _) {},
114+
);
115+
addTearDown(subscription.close);
116+
expect(
117+
await container.read(relayAuthorProvider('wss://relay.example').future),
118+
isNull,
119+
);
120+
await aborted!.timeout(const Duration(seconds: 1));
121+
});
122+
94123
test(
95124
'instruction sources preserve precedence and do not revive unavailable config',
96125
() async {

‎mobile/test/shared/identity_archives/identity_archives_test.dart‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,31 @@ void main() {
170170
expect(relay.author, isNull);
171171
});
172172

173+
test('a refused metadata response is aborted, not left unread', () async {
174+
final owner = nostr.Keys.generate();
175+
final relay = _ArchiveRelay(null);
176+
Future<void>? aborted;
177+
final client = MockClient.streaming((request, _) async {
178+
aborted = (request as http.Abortable).abortTrigger;
179+
return http.StreamedResponse(StreamController<List<int>>().stream, 503);
180+
});
181+
final container = ProviderContainer(
182+
overrides: [
183+
meSyncProvider.overrideWithValue(
184+
MeSync(
185+
relay,
186+
RelayConfig(baseUrl: 'https://relay.test', nsec: owner.nsec),
187+
),
188+
),
189+
relayInformationClientProvider.overrideWithValue(client),
190+
],
191+
);
192+
addTearDown(container.dispose);
193+
addTearDown(client.close);
194+
expect(await container.read(identityArchivesProvider.future), isEmpty);
195+
await aborted!.timeout(const Duration(seconds: 1));
196+
});
197+
173198
test(
174199
'contact keys and unavailable metadata are not archive authority',
175200
() async {

0 commit comments

Comments
 (0)