mirror of
https://github.com/block/buzz.git
synced 2026-12-01 05:32:24 +00:00
## 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>
151 lines
4.7 KiB
Dart
151 lines
4.7 KiB
Dart
import 'dart:async';
|
|
|
|
/// The most lines of a call site's stack that an error carries, counting
|
|
/// `<asynchronous suspension>` gaps and any symbolication header. Sentry parses
|
|
/// a whole stack synchronously and has no cap of its own.
|
|
const maxCallSiteFrames = 64;
|
|
|
|
/// The line the Dart VM writes for an async gap in a stack.
|
|
const _asyncGap = '<asynchronous suspension>';
|
|
|
|
/// [callSite] cut to its [maxCallSiteFrames] innermost lines. The cut is made
|
|
/// only when the stack is first formatted, so an error its caller discards
|
|
/// costs no formatting.
|
|
StackTrace trimCallSite(StackTrace callSite) => _TrimmedCallSite(callSite);
|
|
|
|
final class _TrimmedCallSite implements StackTrace {
|
|
_TrimmedCallSite(this._callSite);
|
|
|
|
final StackTrace _callSite;
|
|
|
|
late final String _text = () {
|
|
final text = _callSite.toString();
|
|
final lines = text.trimRight().split('\n');
|
|
if (lines.length <= maxCallSiteFrames) return text;
|
|
final kept = lines.take(maxCallSiteFrames).toList();
|
|
// stack_trace's `Chain.parse`, which Sentry uses, reads a VM stack
|
|
// directly only when it holds a gap line; without one it first tries
|
|
// other formats with whole-text regex scans. When the kept lines hold no
|
|
// gap, one after them keeps that fast path; a trailing gap adds no frame.
|
|
if (!kept.contains(_asyncGap)) kept.add(_asyncGap);
|
|
return '${kept.join('\n')}\n';
|
|
}();
|
|
|
|
@override
|
|
String toString() => _text;
|
|
}
|
|
|
|
extension RelayCallSiteCompleter<T> on Completer<T> {
|
|
/// Completes with [error] and the trimmed stack of the call that made this
|
|
/// completer, for errors that a timer or a socket callback completes later.
|
|
void completeErrorAt(Object error, StackTrace callSite) =>
|
|
completeError(error, trimCallSite(callSite));
|
|
}
|
|
|
|
/// The relay call an error came from.
|
|
enum RelayCall {
|
|
/// An EVENT sent over the socket and acknowledged with OK.
|
|
publish,
|
|
|
|
/// A one-shot query over the relay's HTTP bridge.
|
|
query,
|
|
|
|
/// A one-shot REQ over the socket, read until EOSE.
|
|
history,
|
|
|
|
/// A live REQ, up to its first EOSE.
|
|
subscribe,
|
|
}
|
|
|
|
/// The reason class of a failed relay call: the relay's own reason prefix, or
|
|
/// a timeout. Never the reason text, which is free-form and can quote what
|
|
/// the app sent.
|
|
enum RelayReason {
|
|
invalid('invalid'),
|
|
rateLimited('rate-limited'),
|
|
restricted('restricted'),
|
|
authRequired('auth-required'),
|
|
blocked('blocked'),
|
|
duplicate('duplicate'),
|
|
pow('pow'),
|
|
unsupported('unsupported'),
|
|
error('error'),
|
|
|
|
/// The call timed out here, before the relay answered.
|
|
timeout('timeout'),
|
|
|
|
/// A reason with no known prefix.
|
|
other('other');
|
|
|
|
const RelayReason(this.tag);
|
|
|
|
/// The wording that leaves the device.
|
|
final String tag;
|
|
|
|
static final _prefixes = {
|
|
for (final reason in values)
|
|
if (reason != timeout && reason != other) reason.tag: reason,
|
|
};
|
|
|
|
/// The class of a relay `OK` or `CLOSED` [message]: its prefix, up to the
|
|
/// first colon, when that is one of the relay's.
|
|
static RelayReason fromMessage(String message) {
|
|
final colon = message.indexOf(':');
|
|
if (colon < 0) return other;
|
|
return _prefixes[message.substring(0, colon).trim().toLowerCase()] ?? other;
|
|
}
|
|
|
|
/// The class of an HTTP error [status] from the relay's `/query`. Its body
|
|
/// is not read: the status alone says rate limit, refusal or server error.
|
|
static RelayReason fromHttpStatus(int status) => switch (status) {
|
|
429 => rateLimited,
|
|
401 => authRequired,
|
|
403 => restricted,
|
|
>= 500 && < 600 => error,
|
|
_ => other,
|
|
};
|
|
}
|
|
|
|
/// An error from a relay call that says which call and why.
|
|
///
|
|
/// Diagnostic reports of these carry [call] and [reason] as tags, because the
|
|
/// error's own text is removed before a report leaves the device.
|
|
abstract interface class RelayCallError implements Exception {
|
|
RelayCall get call;
|
|
RelayReason get reason;
|
|
}
|
|
|
|
/// A relay call timed out before the relay answered.
|
|
class RelayTimeoutException extends TimeoutException implements RelayCallError {
|
|
RelayTimeoutException(this.call, String message, [Duration? duration])
|
|
: super(message, duration);
|
|
|
|
@override
|
|
final RelayCall call;
|
|
|
|
@override
|
|
RelayReason get reason => RelayReason.timeout;
|
|
|
|
/// The text without the duration, which [message] already states. Screens
|
|
/// show this text.
|
|
@override
|
|
String toString() => 'TimeoutException: $message';
|
|
}
|
|
|
|
/// The relay closed a history or live subscription with [message].
|
|
class RelayClosedException implements RelayCallError {
|
|
RelayClosedException(this.call, this.message);
|
|
|
|
@override
|
|
final RelayCall call;
|
|
|
|
/// The relay's `CLOSED` reason. Callers read its text.
|
|
final String message;
|
|
|
|
@override
|
|
RelayReason get reason => RelayReason.fromMessage(message);
|
|
|
|
@override
|
|
String toString() => 'Exception: $message';
|
|
}
|