Files
buzz/mobile/lib/shared/relay/relay_session_subscriptions.dart
Tom Brow e421ac9870 perf(mobile): trim relay call-site stacks and capture them before async waits (#9949)
## Summary

Two follow-ups to the call-site stacks that escaped relay errors carry:

- **Trim:** Sentry parses a whole stack synchronously and has no cap, so
a call-site stack is now cut to its innermost 64 lines (async gap lines
included). When the kept lines hold no `<asynchronous suspension>` line,
one is added after them, so Sentry's stack parser still reads it as a VM
stack directly instead of first scanning it for other formats. The cut
is made when the stack is first formatted, so an error its caller
discards costs no formatting. One `completeErrorAt` helper next to the
error types applies it at every completion site, and query timeouts
throw with the cut stack too.
- **Capture at entry:** `publish`, `fetchHistory` and
`SignedEventRelay.submit` took their call site after the rate-limit
wait, `mayPublish` or `onSigned`. A caller that doesn't await the call
was already gone from the stack by then, so its frame was missing. The
call site is now taken before the first `await`. `submit` re-stacks
relay call errors from `publish` with its own entry stack, since
`publish` only starts after `onSigned`.

### Validation

Added tests: [stack
trimming](https://github.com/block/buzz/blob/tomb/mobile-relay-stack-trim/mobile/test/shared/relay/relay_call_error_test.dart)
(a deep stack is cut to the cap and keeps the frames nearest the call; a
short one is kept whole; a cut stack keeps exactly one async gap
wherever its first gap falls; the stack is formatted only when read, and
only once), and [capture before the
wait](https://github.com/block/buzz/blob/tomb/mobile-relay-stack-trim/mobile/test/shared/relay/relay_session_test.dart)
for a publish and a history request held by the rate limit and a submit
held by `onSigned`, each checking that the caller's frame is on the
completed error's stack.


Full-app check on an iOS simulator signed in to a Builderlab community:
the channel list loads, opening two channels fetches their history, and
a sent message publishes and shows as sent. No relay call errors logged.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-25 15:05:52 -08:00

130 lines
3.6 KiB
Dart

part of 'relay_session.dart';
class _HistorySubscription {
final List<NostrEvent> events = [];
final Completer<List<NostrEvent>> completer;
final Timer timeout;
final DateTime startedAt;
/// Where the call was made, before any wait. Errors completed later, from a
/// timer or a socket callback, carry it, so an escaped error's stack reaches
/// the caller.
final StackTrace callSite;
void fail(Object error) => completer.completeErrorAt(error, callSite);
void timedOut(Duration timeout) => fail(
RelayTimeoutException(
RelayCall.history,
'Relay history request timed out after $timeout',
timeout,
),
);
/// The filter of a request held until the socket authenticates.
NostrFilter? heldFilter;
_HistorySubscription({
required this.completer,
required this.timeout,
required this.startedAt,
required this.callSite,
});
}
class _LiveSubscription {
final NostrFilter filter;
final void Function(NostrEvent) onEvent;
final void Function(String message)? onClosed;
final void Function(RelaySubscriptionStatus status)? onStatusChanged;
/// Whether a replay narrows [filter] with a `since` from what this
/// subscription last saw.
final bool resumeSince;
/// Where the call was made. Errors completed later, from a timer or a
/// socket callback, carry it, so an escaped error's stack reaches the caller.
final StackTrace callSite = StackTrace.current;
Completer<void>? readyCompleter;
int? lastSeenCreatedAt;
/// Whether EOSE arrived on the current connection and no CLOSED has
/// interrupted the stream since.
bool liveOnConnection = false;
/// Client clock (seconds) when the last connection this subscription was
/// live on closed. Kept across later connections that never reach EOSE.
int? liveUntil;
/// The connection generation of this subscription's latest REQ, and the
/// one on which it sent a second REQ, leaving its EOSE ambiguous.
int? reqGeneration;
int? ambiguousGeneration;
int closedRetryAttempt = 0;
Timer? closedRetryTimer;
_LiveSubscription({
required this.filter,
required this.onEvent,
this.onClosed,
this.onStatusChanged,
this.resumeSince = true,
this.readyCompleter,
});
/// The REQ filter for a replay, resumed from what this subscription saw.
NostrFilter get replayFilter {
if (!resumeSince) return filter;
final since = relayResumeSince(
originalSince: filter.since,
until: filter.until,
lastSeenCreatedAt: lastSeenCreatedAt,
liveUntil: liveUntil,
);
return since == null || since == filter.since
? filter
: filter.copyWithSince(since);
}
}
class _ClosedRetry {
final _LiveSubscription subscription;
final int generation;
_ClosedRetry({required this.subscription, required this.generation});
}
class _PendingEvent {
final Completer<NostrEvent> completer;
final Timer timeout;
final DateTime startedAt;
/// Where the call was made, before any wait. Errors completed later, from a
/// timer or a socket callback, carry it, so an escaped error's stack reaches
/// the caller.
final StackTrace callSite;
void fail(Object error) => completer.completeErrorAt(error, callSite);
void timedOut(String eventId, Duration timeout) => fail(
RelayAckTimeoutError(
'Event $eventId not acknowledged within $timeout',
timeout,
),
);
_PendingEvent({
required this.completer,
required this.timeout,
required this.startedAt,
required this.callSite,
});
}
class _BufferedEvent {
final String subId;
final NostrEvent event;
_BufferedEvent(this.subId, this.event);
}