Skip to content

Commit aa7bb70

Browse files
fix(client): scope slow-link frame tracking to socket lifetime
1 parent cd95cbc commit aa7bb70

6 files changed

Lines changed: 33 additions & 38 deletions

File tree

‎apps/mobile/src/connection/app-state-wakeups.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ export type MobileApplicationActiveWakeup = Extract<
77
"application-active-probe" | "application-active-reconnect" | "application-refocused"
88
>;
99

10-
/**
11-
* The wakeup for returning to the foreground. Without a background visit the
12-
* app only went inactive, such as for Control Center or Face ID, and its
13-
* socket was never suspended, so it skips the probe.
14-
*/
1510
export function mobileApplicationActiveWakeup(
1611
backgroundedAtMs: number | null,
1712
activeAtMs: number,

‎docs/internals/connection-runtime.md‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ failures wait for a wakeup instead of spending attempts on unchanged conditions.
1818

1919
Foregrounding, an explicit retry, and an offline report probe the established
2020
session, and only a failed probe reconnects. Offline reports are often wrong, for
21-
example for a loopback server. On a slow link the answer can wait behind other
22-
data, so a probe that times out while frames still arrive waits another window,
23-
up to the full probe timeout. A long mobile background suspension replaces the
24-
session at once instead, because the OS can kill a socket without reporting
21+
example for a loopback server. Probe replies share the RPC read loop with
22+
subscriptions, so a full stream buffer can delay the reply while WebSocket
23+
messages still arrive. Measure progress before that read loop and allow it to
24+
extend a quick probe, with an absolute deadline so unrelated traffic cannot
25+
keep a stuck probe alive forever. A long mobile background suspension replaces
26+
the session at once instead, because the OS can kill a socket without reporting
2527
closure, and a probe would hold a dead socket in "Resuming" until it times out.
26-
That fresh attempt runs even while the network reports offline. A mobile return
27-
without a background visit, such as from Control Center, never suspended the
28-
socket, so it neither probes nor resubscribes. Foregrounding also wakes a
28+
That fresh attempt runs even while the network reports offline. An inactive-only
29+
mobile return, such as from Control Center, must stay distinct from a background
30+
resume: its socket and subscriptions were not suspended. It wakes retries and
31+
route checks without probing or resubscribing. Foregrounding also wakes a
2932
pending retry immediately and leaves an ordinary in-flight attempt alone.
3033

3134
The [registry](../../packages/client-runtime/src/connection/registry.ts) scopes

‎packages/client-runtime/src/authorization/service.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
8585
}
8686
>()("@t3tools/client-runtime/authorization/service/RemoteEnvironmentAuthorization") {}
8787

88-
// A cached token whose ticket fails on the T3 Connect route is replaced, so
89-
// this must outlast a slow but working link, not just a healthy one.
88+
// A premature ticket timeout on T3 Connect needlessly replaces a valid cached token.
9089
const CACHED_ENDPOINT_SOCKET_TIMEOUT_MS = 6_000;
9190
const BEARER_DESCRIPTOR_CACHE_TTL_MS = 10_000;
9291
const DPOP_AUTHORIZATION_TIMEOUT_MS = 30_000;

‎packages/client-runtime/src/connection/supervisor.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ const CONNECTION_ESTABLISHMENT_TIMEOUT = "15 seconds";
4545
const establishmentTimeout = Duration.fromInputUnsafe(CONNECTION_ESTABLISHMENT_TIMEOUT);
4646
const synchronizationTimeout = Duration.fromInputUnsafe("75 seconds");
4747
const CONNECTION_PROBE_TIMEOUT = "15 seconds";
48-
// Mobile resumes, explicit retries, and offline events want a fast answer:
49-
// the user is waiting, or the network may be gone. It still has to cover a
50-
// lost packet and its retransmit on a 500ms round trip, or a slow link reads
51-
// as a dead socket and every resume tears down a working session.
48+
// Leave room for packet retransmission before replacing a slow but working link.
5249
const QUICK_CONNECTION_PROBE_TIMEOUT = "6 seconds";
5350
const BACKOFF_RESET_AFTER_MS = 30_000;
5451
const BETTER_ROUTE_CHECK_INTERVAL = "60 seconds";
@@ -595,13 +592,12 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
595592
continue;
596593
}
597594
yield* Ref.set(probeUnanswered, true);
598-
const probe = yield* Effect.forkChild(lease.session.probe);
599-
// Monotonic nanoseconds, so a wall-clock correction cannot move the deadline.
600595
const probeStartedAt = yield* Clock.monotonicTimeNanos;
601596
const latestDeadline = probeStartedAt + Duration.toNanosUnsafe(CONNECTION_PROBE_TIMEOUT);
602597
let probeWindow = probeTimeout;
603598
let deadline = probeStartedAt + Duration.toNanosUnsafe(probeWindow);
604599
let receivedFrames = yield* lease.session.receivedFrames;
600+
const probe = yield* Effect.forkChild(lease.session.probe);
605601
for (;;) {
606602
const remaining = deadline - (yield* Clock.monotonicTimeNanos);
607603
const probeEvent = yield* Effect.raceAllFirst([
@@ -614,9 +610,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
614610
),
615611
]);
616612
if (probeEvent._tag === "TimedOut") {
617-
// Frames still arriving mean a slow link, not a dead one: the answer
618-
// may be queued behind them. Wait another window, up to the full
619-
// probe timeout.
613+
// The reply may be behind other traffic; the absolute cap still bounds a stuck probe.
620614
const received = yield* lease.session.receivedFrames;
621615
const now = yield* Clock.monotonicTimeNanos;
622616
if (received > receivedFrames && now < latestDeadline) {

‎packages/client-runtime/src/connection/wakeups.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ export type ConnectionWakeup =
66
| "application-active"
77
| "application-active-probe"
88
| "application-active-reconnect"
9-
// The app came back from an inactive state, such as Control Center or Face
10-
// ID, without a background visit. Inactive apps keep running, so the socket
11-
// was never suspended, but a pending retry should still run now.
9+
// Inactive-only mobile returns wake retries without probing or resubscribing.
1210
| "application-refocused"
1311
| "credentials-changed"
1412
| "network-changed";

‎packages/client-runtime/src/rpc/session.ts‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export interface RpcSession {
5252
) => ServerConfigSubscription;
5353
readonly ready: Effect.Effect<void, ConnectionAttemptError>;
5454
readonly probe: Effect.Effect<void, ConnectionAttemptError>;
55-
/** How many WebSocket frames have arrived, pongs included. A rising count means the link still delivers. */
55+
/** Message arrivals, including Effect's application pongs, before RPC backpressure. */
5656
readonly receivedFrames: Effect.Effect<number>;
5757
readonly closed: Effect.Effect<never, ConnectionAttemptError>;
5858
}
@@ -193,21 +193,27 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
193193
Effect.asVoid,
194194
),
195195
});
196-
// Counted as each frame arrives, not when the RPC read loop takes it, so a
197-
// loop held up by a full stream buffer still shows a link that delivers.
196+
// RPC reads can stall behind a full subscription buffer while the link still delivers.
198197
let receivedFrames = 0;
199198
const countFrame = () => {
200199
receivedFrames += 1;
201200
};
202-
const layerSocket = Socket.layerWebSocket(connection.socketUrl, {
203-
openTimeout: SOCKET_OPEN_TIMEOUT,
204-
}).pipe(
205-
Layer.provide(
206-
Layer.succeed(Socket.WebSocketConstructor, (url, options) => {
207-
const socket = webSocketConstructor(url, options);
208-
socket.addEventListener("message", countFrame);
209-
return socket;
210-
}),
201+
const layerSocket = Layer.effect(
202+
Socket.Socket,
203+
Socket.fromWebSocket(
204+
Effect.acquireRelease(
205+
Effect.sync(() => {
206+
const socket = webSocketConstructor(connection.socketUrl);
207+
socket.addEventListener("message", countFrame);
208+
return socket;
209+
}),
210+
(socket) =>
211+
Effect.sync(() => {
212+
socket.removeEventListener("message", countFrame);
213+
socket.close(1000);
214+
}),
215+
),
216+
{ openTimeout: SOCKET_OPEN_TIMEOUT },
211217
),
212218
);
213219
const layerProtocol = Layer.effect(

0 commit comments

Comments
 (0)