Skip to content

Commit c9f71c9

Browse files
wesbillmanPinky
andcommitted
fix(mobile): stop retrying terminally rejected channel chunks
Honor RelaySession terminal admission failures instead of immediately refreshing memberships and reinstalling the unchanged filter. Quarantine rejected desired chunks until the filter, scope, or connection changes. Keep periodic membership discovery and transient session retry policy. Add fake-clock regressions for persistent rejection before/after readiness, bounded requests and timers, reconnect recovery, changed membership, stale callbacks, scope isolation, and pending-close disposal. Preserve fallback coverage until reconnect can restore the rejected chunk. Co-authored-by: Pinky <5f5ab050ec58ae208332edd544ebf705221e24c1b86d82a6ca07038a7a8f6ac9@buzz.block.builderlab.xyz> Signed-off-by: Wes <[email protected]>
1 parent a1ac2f6 commit c9f71c9

7 files changed

Lines changed: 292 additions & 30 deletions

File tree

‎mobile/lib/features/channels/channels_provider.dart‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
4545
Set<String> _desiredLiveChannelIds = const {};
4646
int _subscriptionVersion = 0;
4747
int _nextLiveChunkGeneration = 0;
48-
bool _liveReconcileRunning = false;
49-
bool _liveReconcileRequested = false;
48+
final Set<String> _terminallyClosedLiveChunks = {};
5049
String? _subscriptionRelayBaseUrl;
5150
Timer? _backstopTimer;
5251
final Map<String, int> _latestObservedByChannel = {};
@@ -117,6 +116,9 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
117116
!connected.isCompleted) {
118117
connected.complete();
119118
} else if (previous?.status != SessionStatus.connected) {
119+
// A new authenticated connection can change relay admission policy.
120+
// Ordinary refreshes must not retry an unchanged terminal rejection.
121+
_terminallyClosedLiveChunks.clear();
120122
unawaited(_backstopRefresh());
121123
}
122124
});

‎mobile/lib/features/channels/channels_provider_lifecycle.dart‎

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ extension _ChannelsNotifierLiveSubscriptions on ChannelsNotifier {
2323
};
2424
final relayBaseUrl = _lifecycleRef.read(relayConfigProvider).baseUrl;
2525
_desiredLiveChannelIds = channelIds;
26+
// A changed filter may be admitted, but an unchanged terminal rejection
27+
// cannot recover just because a timer or manual refresh fetched it again.
28+
// Drop obsolete keys so this quarantine stays bounded by desired chunks.
29+
final desiredKeys = chunkChannelIdsForLiveSubscriptions(
30+
channelIds,
31+
).map(_liveChunkKey).toSet();
32+
_terminallyClosedLiveChunks.retainAll(desiredKeys);
2633
final subscriptionVersion = ++_subscriptionVersion;
2734

2835
final sync = _liveSubscriptionQueue.then(
@@ -78,7 +85,10 @@ extension _ChannelsNotifierLiveSubscriptions on ChannelsNotifier {
7885
final session = _lifecycleRef.read(relaySessionProvider.notifier);
7986
for (final chunk in desiredChunks) {
8087
final chunkKey = _liveChunkKey(chunk);
81-
if (_liveSubscriptionsByChunk.containsKey(chunkKey)) continue;
88+
if (_liveSubscriptionsByChunk.containsKey(chunkKey) ||
89+
_terminallyClosedLiveChunks.contains(chunkKey)) {
90+
continue;
91+
}
8292
if (_lifecycleRef.read(relaySessionProvider).status !=
8393
SessionStatus.connected) {
8494
return;
@@ -160,36 +170,21 @@ extension _ChannelsNotifierLiveSubscriptions on ChannelsNotifier {
160170
}
161171

162172
void _handleLiveChunkClosed(String chunkKey, int generation, String message) {
173+
if (!_lifecycleRef.mounted) return;
163174
final subscription = _liveSubscriptionsByChunk[chunkKey];
164175
if (subscription == null || subscription.generation != generation) return;
165176
_liveSubscriptionsByChunk.remove(chunkKey);
177+
// RelaySession calls onClosed only for terminal admission failures. Its
178+
// transient/rate-limit retries have their own backoff. Do not turn a
179+
// terminal rejection into a fresh membership/history/REQ loop here.
180+
if (chunkChannelIdsForLiveSubscriptions(
181+
_desiredLiveChannelIds,
182+
).any((chunk) => _liveChunkKey(chunk) == chunkKey)) {
183+
_terminallyClosedLiveChunks.add(chunkKey);
184+
}
166185
debugPrint(
167186
'[ChannelsNotifier] live subscription closed by relay: $message',
168187
);
169-
_requestLiveReconcile();
170-
}
171-
172-
void _requestLiveReconcile() {
173-
if (!_lifecycleRef.mounted ||
174-
_lifecycleRef.read(relaySessionProvider).status !=
175-
SessionStatus.connected) {
176-
return;
177-
}
178-
if (_liveReconcileRunning) {
179-
_liveReconcileRequested = true;
180-
return;
181-
}
182-
_liveReconcileRunning = true;
183-
unawaited(() async {
184-
try {
185-
do {
186-
_liveReconcileRequested = false;
187-
await _backstopRefresh();
188-
} while (_liveReconcileRequested && _lifecycleRef.mounted);
189-
} finally {
190-
_liveReconcileRunning = false;
191-
}
192-
}());
193188
}
194189

195190
void _removeUndesiredLiveChunks() {
@@ -243,7 +238,7 @@ extension _ChannelsNotifierLiveSubscriptions on ChannelsNotifier {
243238
void _clearLiveSubscriptions() {
244239
_subscriptionVersion++;
245240
_desiredLiveChannelIds = const {};
246-
_liveReconcileRequested = false;
241+
_terminallyClosedLiveChunks.clear();
247242
_clearRetainedLiveChunks();
248243
_subscriptionRelayBaseUrl = null;
249244
_backstopTimer?.cancel();

‎mobile/pubspec.lock‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ packages:
354354
source: hosted
355355
version: "0.3.12"
356356
fake_async:
357-
dependency: transitive
357+
dependency: "direct dev"
358358
description:
359359
name: fake_async
360360
sha256: "5368f224a74523e8d2e7399ea1638b37aecfca824a3cc4dfdf77bf1fa905ac44"

‎mobile/pubspec.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ dev_dependencies:
5151
flutter_test:
5252
sdk: flutter
5353
flutter_lints: ^6.0.0
54+
fake_async: ^1.3.3
5455
camera_platform_interface: ^2.13.1
5556
crypto: ^3.0.7
5657
custom_lint: ^0.8.0

‎mobile/test/features/channels/channels_provider_live_cases.dart‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void _liveSubscriptionTests() {
223223
);
224224

225225
test(
226-
'terminal closure reinstalls desired coverage without retiring fallback',
226+
'terminal closure preserves fallback until reconnect retries coverage',
227227
() async {
228228
final channelIds = [
229229
for (var i = 1; i <= 129; i++) _generatedChannelId(i),
@@ -249,6 +249,13 @@ void _liveSubscriptionTests() {
249249
channelIds.last,
250250
'restricted: terminal closure',
251251
);
252+
await _settle();
253+
// The rejected chunk itself is gone, but the previously retained
254+
// fallback must still cover its original first 128 channels.
255+
expect(session.activeChannels, channelIds.take(128).toSet());
256+
expect(session.activeChannels, isNot(contains(addedId)));
257+
session.setStatus(SessionStatus.disconnected);
258+
session.setStatus(SessionStatus.connected);
252259
await _waitUntil(() => session.activeChannels.contains(addedId));
253260

254261
expect(session.activeChannels, containsAll({...channelIds, addedId}));
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
part of 'channels_provider_test.dart';
2+
3+
void _testWithClock(String name, void Function(FakeAsync) body) {
4+
test(name, () => fakeAsync(body));
5+
}
6+
7+
void _terminalSubscriptionTests() {
8+
for (final beforeReady in [true, false]) {
9+
_testWithClock(
10+
'persistent terminal rejection ${beforeReady ? 'before' : 'after'} '
11+
'readiness cannot amplify refreshes or retries',
12+
(clock) {
13+
final session = _TerminalRelaySession(beforeReady: beforeReady);
14+
final logs = <String>[];
15+
final originalDebugPrint = debugPrint;
16+
debugPrint = (message, {wrapWidth}) {
17+
if (message != null) logs.add(message);
18+
};
19+
final container = _buildContainer(session: session);
20+
container.read(channelsProvider);
21+
try {
22+
clock.flushMicrotasks();
23+
// The finite snapshot is usable even before live readiness settles.
24+
expect(
25+
container.read(channelsProvider).requireValue.single.id,
26+
_channelA,
27+
);
28+
expect(session.totalSubscribeCount, 1);
29+
final initialMemberships = session.membershipRequestCount;
30+
clock.elapse(const Duration(milliseconds: 2));
31+
final historyAfterClose = session.historyFilters.length;
32+
final batchesAfterClose = session.queryBatches.length;
33+
final logsAfterClose = logs.length;
34+
35+
// Virtual time permits many immediate retries on the broken path.
36+
for (var i = 0; i < 20; i++) {
37+
clock.elapse(const Duration(milliseconds: 10));
38+
}
39+
expect(session.rejectionCount, 1);
40+
expect(session.membershipRequestCount, initialMemberships);
41+
expect(session.historyFilters.length, historyAfterClose);
42+
expect(session.queryBatches.length, batchesAfterClose);
43+
expect(session.totalSubscribeCount, 1);
44+
expect(logs.length, logsAfterClose);
45+
expect(clock.periodicTimerCount, 1);
46+
expect(clock.nonPeriodicTimerCount, 0);
47+
48+
// Ordinary polling may refresh memberships, but never re-admits an
49+
// unchanged terminal filter, creates retry timers, or repeats logs.
50+
final pollStart = session.membershipRequestCount;
51+
for (var i = 0; i < 3; i++) {
52+
clock.elapse(const Duration(seconds: 60));
53+
expect(session.totalSubscribeCount, 1);
54+
expect(session.activeSubscriptionCount, 0);
55+
expect(logs.length, logsAfterClose);
56+
expect(clock.periodicTimerCount, 1);
57+
expect(clock.nonPeriodicTimerCount, 0);
58+
expect(
59+
container.read(channelsProvider).requireValue.single.id,
60+
_channelA,
61+
);
62+
}
63+
expect(session.membershipRequestCount - pollStart, 6);
64+
65+
// A pull-to-refresh still isn't evidence that admission changed.
66+
unawaited(container.read(channelsProvider.notifier).refresh());
67+
clock.flushMicrotasks();
68+
clock.elapse(const Duration(milliseconds: 2));
69+
expect(session.totalSubscribeCount, 1);
70+
expect(logs.length, logsAfterClose);
71+
} finally {
72+
container.dispose();
73+
debugPrint = originalDebugPrint;
74+
}
75+
clock.elapse(const Duration(seconds: 60));
76+
expect(clock.pendingTimers, isEmpty);
77+
expect(session.totalSubscribeCount, 1);
78+
},
79+
);
80+
}
81+
82+
for (final beforeReady in [true, false]) {
83+
_testWithClock(
84+
'disposal fences pending terminal closure ${beforeReady ? 'before' : 'after'} readiness',
85+
(clock) {
86+
final session = _TerminalRelaySession(beforeReady: beforeReady);
87+
final container = _buildContainer(session: session);
88+
container.read(channelsProvider);
89+
clock.flushMicrotasks();
90+
final requests = session.membershipRequestCount;
91+
container.dispose();
92+
clock.elapse(const Duration(minutes: 3));
93+
expect(session.totalSubscribeCount, 1);
94+
expect(session.membershipRequestCount, requests);
95+
expect(session.activeSubscriptionCount, 0);
96+
expect(clock.pendingTimers, isEmpty);
97+
},
98+
);
99+
}
100+
101+
_testWithClock('terminal rejection retries only after a new connection', (
102+
clock,
103+
) {
104+
final session = _TerminalRelaySession();
105+
final container = _buildContainer(session: session);
106+
try {
107+
container.read(channelsProvider);
108+
clock.flushMicrotasks();
109+
clock.elapse(const Duration(milliseconds: 2));
110+
final oldClosed = session.closedCallbacks.single;
111+
for (var attempt = 2; attempt <= 4; attempt++) {
112+
session.setStatus(SessionStatus.disconnected);
113+
clock.flushMicrotasks();
114+
session.setStatus(SessionStatus.connected);
115+
clock.flushMicrotasks();
116+
clock.elapse(const Duration(milliseconds: 2));
117+
clock.elapse(const Duration(milliseconds: 20));
118+
expect(session.totalSubscribeCount, attempt);
119+
expect(session.rejectionCount, attempt);
120+
expect(session.activeSubscriptionCount, 0);
121+
expect(
122+
container.read(channelsProvider).requireValue.single.id,
123+
_channelA,
124+
);
125+
}
126+
session.reject = false;
127+
session.setStatus(SessionStatus.disconnected);
128+
clock.flushMicrotasks();
129+
session.setStatus(SessionStatus.connected);
130+
clock.flushMicrotasks();
131+
expect(session.totalSubscribeCount, 5);
132+
expect(session.activeChannels, {_channelA});
133+
oldClosed();
134+
clock.flushMicrotasks();
135+
expect(session.activeChannels, {_channelA});
136+
expect(session.totalSubscribeCount, 5);
137+
} finally {
138+
container.dispose();
139+
}
140+
clock.flushMicrotasks();
141+
});
142+
143+
_testWithClock(
144+
'changed membership admits a new filter, not a rejected old one',
145+
(clock) {
146+
final session = _TerminalRelaySession();
147+
final container = _buildContainer(session: session);
148+
try {
149+
container.read(channelsProvider);
150+
clock.flushMicrotasks();
151+
clock.elapse(const Duration(milliseconds: 2));
152+
session.reject = false;
153+
session.memberships.add(_membership(_channelB, 'me'));
154+
session.metadata.add(_meta(id: _channelB, name: 'b'));
155+
unawaited(container.read(channelsProvider.notifier).refresh());
156+
clock.flushMicrotasks();
157+
expect(session.totalSubscribeCount, 2);
158+
expect(session.activeChannels, {_channelA, _channelB});
159+
// Removing then rejoining is a meaningful transition even if it returns
160+
// to an earlier filter; obsolete quarantine keys must not leak forever.
161+
session.memberships.removeLast();
162+
unawaited(container.read(channelsProvider.notifier).refresh());
163+
clock.flushMicrotasks();
164+
expect(session.totalSubscribeCount, 3);
165+
expect(session.activeChannels, {_channelA});
166+
} finally {
167+
container.dispose();
168+
}
169+
clock.flushMicrotasks();
170+
},
171+
);
172+
173+
for (final switchIdentity in [true, false]) {
174+
_testWithClock(
175+
'terminal quarantine does not cross ${switchIdentity ? 'identity' : 'community'} scope',
176+
(clock) {
177+
final session = _TerminalRelaySession();
178+
final container = _buildContainer(session: session);
179+
try {
180+
container.read(channelsProvider);
181+
clock.flushMicrotasks();
182+
clock.elapse(const Duration(milliseconds: 2));
183+
session.reject = false;
184+
if (switchIdentity) {
185+
session.memberships = [_membership(_channelA, _otherPk)];
186+
container.read(_testPubkeyProvider.notifier).set(_otherPk);
187+
} else {
188+
container
189+
.read(relayConfigProvider.notifier)
190+
.update(baseUrl: 'https://new-community.example');
191+
}
192+
container.read(channelsProvider);
193+
clock.flushMicrotasks();
194+
expect(session.totalSubscribeCount, 2);
195+
expect(session.activeChannels, {_channelA});
196+
} finally {
197+
container.dispose();
198+
}
199+
clock.flushMicrotasks();
200+
},
201+
);
202+
}
203+
}
204+
205+
/// Reject every attempted subscription, including every replacement, as a
206+
/// terminal relay CLOSED. A timer models the wire turn and prevents a broken
207+
/// immediate retry loop from hanging the test's microtask drain.
208+
class _TerminalRelaySession extends _FakeRelaySession {
209+
_TerminalRelaySession({this.beforeReady = false})
210+
: super(
211+
memberships: [_membership(_channelA, 'me')],
212+
metadata: [_meta(id: _channelA, name: 'a')],
213+
);
214+
215+
final bool beforeReady;
216+
bool reject = true;
217+
int rejectionCount = 0;
218+
final List<void Function()> closedCallbacks = [];
219+
220+
@override
221+
Future<void Function()> subscribe(
222+
NostrFilter filter,
223+
void Function(NostrEvent) onEvent, {
224+
void Function(String message)? onClosed,
225+
}) async {
226+
final unsubscribe = await super.subscribe(
227+
filter,
228+
onEvent,
229+
onClosed: onClosed,
230+
);
231+
if (!reject) return unsubscribe;
232+
final key = _subscriptions.keys.last;
233+
final closed = Completer<void>();
234+
final timer = Timer(const Duration(milliseconds: 1), () {
235+
final subscription = _subscriptions.remove(key);
236+
if (subscription != null) {
237+
subscribeFilters.remove(subscription.$1);
238+
rejectionCount++;
239+
void notify() => onClosed?.call('restricted: persistent rejection');
240+
closedCallbacks.add(notify);
241+
notify();
242+
}
243+
closed.complete();
244+
});
245+
if (beforeReady) {
246+
await closed.future;
247+
throw StateError('restricted: persistent rejection');
248+
}
249+
return () {
250+
timer.cancel();
251+
unsubscribe();
252+
};
253+
}
254+
}

0 commit comments

Comments
 (0)