fix: prevent reconnect loops during server stalls #5561
Mergedgfsaaser24(GitHub profile, opens in a new tab)merged 9 commits into
main fromrefs/pull/5561/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- packages/shared/src/shell.ts line 578OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -538,3 +571,8 @@538571 if (pathValue.length === 0) {539572 return yield* new CommandResolutionError({ command, reason: "not-found" });540573 }574+575+ const cacheKey = [platform, pathValue, windowsPathExtensions.join(";"), command].join(576+ COMMAND_RESOLUTION_CACHE_KEY_SEPARATOR,577+ );578+ const nowEpochMs = yield* Clock.currentTimeMillis;packages/shared/src/observability.ts lines 300 to 302Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -250,0 +252,21 @@252+const TRACE_ATTRIBUTE_TRUNCATED_LENGTH = 200;253+const TRACE_ATTRIBUTE_TRUNCATION_SUFFIX = "…[truncated]";254+const ALWAYS_TRUNCATED_TRACE_ATTRIBUTES: ReadonlySet<string> = new Set(["db.query.text"]);255+256+/**257+ * Clamps oversized attribute values on the serialized trace record so the file258+ * sink stays small. Returns a new record when anything was clamped; never259+ * mutates the input (the live span's attributes are shared with other tracers).260+ */261+function truncateTraceAttributes(attributes: TraceAttributes): TraceAttributes {262+ let truncated: Record<string, unknown> | undefined;263+ for (const [key, value] of Object.entries(attributes)) {264+ if (typeof value !== "string") continue;265+ const maxLength = ALWAYS_TRUNCATED_TRACE_ATTRIBUTES.has(key)266+ ? TRACE_ATTRIBUTE_TRUNCATED_LENGTH267+ : TRACE_ATTRIBUTE_MAX_LENGTH;268+ if (value.length <= maxLength) continue;269+ truncated ??= { ...attributes };270+ truncated[key] =271+ `${value.slice(0, TRACE_ATTRIBUTE_TRUNCATED_LENGTH)}${TRACE_ATTRIBUTE_TRUNCATION_SUFFIX}`;272+ }
- packages/shared/src/observability.ts line 302Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -250,0 +252,21 @@252+const TRACE_ATTRIBUTE_TRUNCATED_LENGTH = 200;253+const TRACE_ATTRIBUTE_TRUNCATION_SUFFIX = "…[truncated]";254+const ALWAYS_TRUNCATED_TRACE_ATTRIBUTES: ReadonlySet<string> = new Set(["db.query.text"]);255+256+/**257+ * Clamps oversized attribute values on the serialized trace record so the file258+ * sink stays small. Returns a new record when anything was clamped; never259+ * mutates the input (the live span's attributes are shared with other tracers).260+ */261+function truncateTraceAttributes(attributes: TraceAttributes): TraceAttributes {262+ let truncated: Record<string, unknown> | undefined;263+ for (const [key, value] of Object.entries(attributes)) {264+ if (typeof value !== "string") continue;265+ const maxLength = ALWAYS_TRUNCATED_TRACE_ATTRIBUTES.has(key)266+ ? TRACE_ATTRIBUTE_TRUNCATED_LENGTH267+ : TRACE_ATTRIBUTE_MAX_LENGTH;268+ if (value.length <= maxLength) continue;269+ truncated ??= { ...attributes };270+ truncated[key] =271+ `${value.slice(0, TRACE_ATTRIBUTE_TRUNCATED_LENGTH)}${TRACE_ATTRIBUTE_TRUNCATION_SUFFIX}`;272+ }
- packages/shared/src/observability.ts line 261OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -248,3 +248,14 @@248248 };249249 }250250251+const TRACE_ATTRIBUTE_MAX_LENGTH = 500;252+const TRACE_ATTRIBUTE_TRUNCATED_LENGTH = 200;253+const TRACE_ATTRIBUTE_TRUNCATION_SUFFIX = "…[truncated]";254+const ALWAYS_TRUNCATED_TRACE_ATTRIBUTES: ReadonlySet<string> = new Set(["db.query.text"]);255+256+/**257+ * Clamps oversized attribute values on the serialized trace record so the file258+ * sink stays small. Returns a new record when anything was clamped; never259+ * mutates the input (the live span's attributes are shared with other tracers).260+ */261+function truncateTraceAttributes(attributes: TraceAttributes): TraceAttributes {
- packages/shared/src/shell.ts lines 504 to 510OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -491,3 +492,19 @@491492 return Array.from(new Set(candidates));492493 }493494495+// Session bootstrap resolves the same commands over and over, each PATH scan496+// costing hundreds of 'shell.isExecutableFile' filesystem probes (tens of497+// thousands per connect). Memoize the scan outcome per498+// (platform, PATH, PATHEXT, command) for a short window: repeat scans hit the499+// cache while any change to the search environment invalidates immediately.500+// Explicit-path resolution is never cached - callers probe paths they have501+// just written (e.g. managed binary installs).502+// TTL expiry uses the monotonic clock (Clock.currentTimeNanos) so backward503+// wall-clock adjustments cannot keep expired entries alive.504+const COMMAND_RESOLUTION_CACHE_TTL_NANOS = 30_000_000_000n;505+const COMMAND_RESOLUTION_CACHE_MAX_ENTRIES = 512;506+const COMMAND_RESOLUTION_CACHE_KEY_SEPARATOR = String.fromCharCode(0);507+const commandResolutionCache = new Map<508+ string,509+ { readonly resolvedPath: string | null; readonly expiresAtNanos: bigint }510+>();
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- packages/client-runtime/src/connection/supervisor.ts line 729Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -709,3 +719,10 @@709719 continue;710720 }711721722+ if (failedWakeProbe) {723+ // The wake probe found a dead transport while the user is returning to724+ // the app, so reconnect immediately instead of sleeping the first725+ // backoff rung. Only this first attempt skips the ladder; if it fails726+ // too, normal backoff resumes.727+ resetRetryLadder();728+ continue;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -210,0 +195,21 @@195+ const hasAuthoritativeSnapshot = (yield* Ref.get(lastAuthoritativeSession)) === session;196+ let current = yield* SubscriptionRef.get(state);197+ if (!hasAuthoritativeSnapshot || Option.isNone(current.snapshot)) {198+ const prepared = yield* SubscriptionRef.get(supervisor.prepared).pipe(199+ Effect.flatMap(200+ Option.match({201+ onSome: Effect.succeed,202+ onNone: () =>203+ SubscriptionRef.changes(supervisor.prepared).pipe(204+ Stream.filter(Option.isSome),205+ Stream.map((value) => value.value),206+ Stream.runHead,207+ Effect.map(Option.getOrThrow),208+ ),209+ }),210+ ),211+ );212+ const httpSnapshot = yield* snapshotLoader.load(prepared);213+ if (Option.isSome(httpSnapshot)) {214+ yield* applyItem({ kind: "snapshot", snapshot: httpSnapshot.value });215+ yield* Ref.set(lastAuthoritativeSession, session);
- packages/client-runtime/src/state/shell.ts line 223Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -210,0 +195,21 @@195+ const hasAuthoritativeSnapshot = (yield* Ref.get(lastAuthoritativeSession)) === session;196+ let canResume = hasAuthoritativeSnapshot;197+ let current = yield* SubscriptionRef.get(state);198+ if (!hasAuthoritativeSnapshot || Option.isNone(current.snapshot)) {199+ const prepared = yield* SubscriptionRef.get(supervisor.prepared).pipe(200+ Effect.flatMap(201+ Option.match({202+ onSome: Effect.succeed,203+ onNone: () =>204+ SubscriptionRef.changes(supervisor.prepared).pipe(205+ Stream.filter(Option.isSome),206+ Stream.map((value) => value.value),207+ Stream.runHead,208+ Effect.map(Option.getOrThrow),209+ ),210+ }),211+ ),212+ );213+ const httpSnapshot = yield* snapshotLoader.load(prepared);214+ if (Option.isSome(httpSnapshot)) {215+ yield* applyItem({ kind: "snapshot", snapshot: httpSnapshot.value });
- packages/client-runtime/src/state/shell.ts line 231Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -211,3 +205,20 @@205+ Stream.filter(Option.isSome),206+ Stream.map((value) => value.value),207+ Stream.runHead,208+ Effect.map(Option.getOrThrow),209+ ),210+ }),211+ ),212+ );213+ const httpSnapshot = yield* snapshotLoader.load(prepared);214+ if (Option.isSome(httpSnapshot)) {215+ yield* applyItem({ kind: "snapshot", snapshot: httpSnapshot.value });216+ yield* Ref.set(lastAuthoritativeSession, session);217+ canResume = true;218+ current = yield* SubscriptionRef.get(state);219+ }211220 }212221213- return supportsCompletionMarker ? { requestCompletionMarker: true as const } : {};222+ // If the authoritative refresh failed, omit the cached cursor so the223+ // socket fallback sends a complete snapshot for this new session.224+ if (!canResume || Option.isNone(current.snapshot)) {
t3dotgg(GitHub profile, opens in a new tab) changed the title
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
b7ccb510-cc89-4051-8cbe-494d8a5c81c5You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share