Skip to content

Commit 7637a45

Browse files
PR Batch Testerclaude
andcommitted
fix(client): a big server config on a slow link no longer drops the connection in a loop
The client's ping reply waits behind whatever the server is already sending, and a WebSocket message only arrives once all of it has. When the config snapshot takes longer than the 15s ping timeout to download, the client declares a healthy server dead, reconnects, and downloads the same snapshot again, forever. Clients now ask the server to send messages over 64 KiB in parts. Each part that arrives counts as the server being alive, and the client joins the parts back into the original message. Old clients and old servers keep sending and receiving whole messages. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f460ddb commit 7637a45

6 files changed

Lines changed: 236 additions & 5 deletions

File tree

‎apps/server/src/ws.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ import * as SessionStore from "./auth/SessionStore.ts";
248248
import { failEnvironmentAuthInvalid, failEnvironmentInternal } from "./auth/http.ts";
249249
import { isNetworkPath } from "@t3tools/shared/path";
250250
import * as RelayClient from "@t3tools/shared/relayClient";
251+
import {
252+
SOCKET_FRAME_PARTS_QUERY_PARAM,
253+
splitLargeSocketFrames,
254+
} from "@t3tools/shared/socketFrameParts";
251255
import { isTelemetryIdHash } from "@t3tools/shared/telemetryId";
252256
import {
253257
sameUsageLimitCommandCoverage,
@@ -3249,11 +3253,25 @@ export const layer = Layer.unwrap(
32493253
),
32503254
),
32513255
);
3256+
const framedRpcWebSocketHttpEffect = requestUrl.value.searchParams.has(
3257+
SOCKET_FRAME_PARTS_QUERY_PARAM,
3258+
)
3259+
? rpcWebSocketHttpEffect.pipe(
3260+
Effect.provideService(
3261+
HttpServerRequest.HttpServerRequest,
3262+
// The RPC server upgrades the request itself, so its socket is
3263+
// wrapped through the request it reads.
3264+
Object.create(request, {
3265+
upgrade: { value: Effect.map(request.upgrade, splitLargeSocketFrames) },
3266+
}),
3267+
),
3268+
)
3269+
: rpcWebSocketHttpEffect;
32523270
return yield* Effect.acquireUseRelease(
32533271
sessions.markConnected(session.sessionId),
32543272
() =>
32553273
Effect.raceFirst(
3256-
rpcWebSocketHttpEffect,
3274+
framedRpcWebSocketHttpEffect,
32573275
sessions.awaitInvalidation(session.sessionId).pipe(
32583276
Effect.as(HttpServerResponse.empty()),
32593277
Effect.catchTags({

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
WS_METHODS,
99
UsageLimitSourceId,
1010
} from "@t3tools/contracts";
11+
import { splitSocketFrame } from "@t3tools/shared/socketFrameParts";
1112
import { describe, expect, it } from "@effect/vitest";
1213
import * as Cause from "effect/Cause";
1314
import * as Deferred from "effect/Deferred";
@@ -21,6 +22,7 @@ import * as Schema from "effect/Schema";
2122
import * as Stream from "effect/Stream";
2223
import * as SubscriptionRef from "effect/SubscriptionRef";
2324
import * as Socket from "effect/socket/Socket";
25+
import * as TestClock from "effect/testing/TestClock";
2426

2527
import {
2628
AVAILABLE_CONNECTION_STATE,
@@ -282,14 +284,46 @@ const publishConfigEvents = Effect.fn("TestRpcSessionFactory.publishConfigEvents
282284
});
283285

284286
describe("RpcSessionFactory", () => {
287+
it.effect(
288+
"connects while a config snapshot arrives in parts for longer than the ping timeout",
289+
() =>
290+
Effect.gen(function* () {
291+
const { factory, sockets } = yield* makeFactory();
292+
const session = yield* factory.connect(PREPARED);
293+
const readyFiber = yield* Effect.forkChild(session.ready);
294+
const socket = yield* awaitSocket(sockets);
295+
socket.open();
296+
const request = yield* awaitRequest(socket);
297+
const parts = splitSocketFrame(
298+
encodeJson({
299+
_tag: "Chunk",
300+
requestId: request.id,
301+
values: [{ version: 1, type: "snapshot", config: ENCODED_SERVER_CONFIG }],
302+
}),
303+
512,
304+
);
305+
expect(parts.length).toBeGreaterThan(2);
306+
307+
for (const part of parts) {
308+
socket.serverMessage(part);
309+
yield* TestClock.adjust("10 seconds");
310+
}
311+
yield* Fiber.join(readyFiber);
312+
313+
const config = yield* session.initialConfig;
314+
expect(config).toEqual(SERVER_CONFIG);
315+
expect(socket.readyState).toBe(TestWebSocket.OPEN);
316+
}),
317+
);
318+
285319
it.effect("owns one scoped websocket attempt and exposes readiness and closure", () =>
286320
Effect.gen(function* () {
287321
const { factory, sockets } = yield* makeFactory();
288322
const session = yield* factory.connect(PREPARED);
289323
const readyFiber = yield* Effect.forkChild(session.ready);
290324
const socket = yield* awaitSocket(sockets);
291325

292-
expect(socket.url).toBe(PREPARED.socketUrl);
326+
expect(socket.url).toBe(`${PREPARED.socketUrl}&frameParts=1`);
293327
socket.open();
294328
yield* completeInitialConfig(socket);
295329
yield* Fiber.join(readyFiber);

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import {
44
WsSubscribeServerConfigRpc,
55
WS_METHODS,
66
} from "@t3tools/contracts";
7+
import {
8+
joinSocketFrameParts,
9+
SOCKET_FRAME_PARTS_QUERY_PARAM,
10+
} from "@t3tools/shared/socketFrameParts";
711
import * as Cause from "effect/Cause";
812
import * as Context from "effect/Context";
913
import * as Deferred from "effect/Deferred";
@@ -206,9 +210,19 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
206210
Effect.asVoid,
207211
),
208212
});
209-
const layerSocket = Socket.layerWebSocket(connection.socketUrl, {
210-
openTimeout: SOCKET_OPEN_TIMEOUT,
211-
}).pipe(Layer.provide(Layer.succeed(Socket.WebSocketConstructor, webSocketConstructor)));
213+
const socketUrl = new URL(connection.socketUrl);
214+
socketUrl.searchParams.set(SOCKET_FRAME_PARTS_QUERY_PARAM, "1");
215+
const layerSocket = Layer.effect(
216+
Socket.Socket,
217+
Effect.map(Effect.service(Socket.Socket), joinSocketFrameParts),
218+
).pipe(
219+
Layer.provide(
220+
Socket.layerWebSocket(socketUrl.toString(), {
221+
openTimeout: SOCKET_OPEN_TIMEOUT,
222+
}),
223+
),
224+
Layer.provide(Layer.succeed(Socket.WebSocketConstructor, webSocketConstructor)),
225+
);
212226
const layerProtocol = Layer.effect(
213227
RpcClient.Protocol,
214228
RpcClient.makeProtocolSocket({

‎packages/shared/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"types": "./src/delimitedPreview.ts",
2424
"import": "./src/delimitedPreview.ts"
2525
},
26+
"./socketFrameParts": {
27+
"types": "./src/socketFrameParts.ts",
28+
"import": "./src/socketFrameParts.ts"
29+
},
2630
"./composerPullRequestMatches": {
2731
"types": "./src/composerPullRequestMatches.ts",
2832
"import": "./src/composerPullRequestMatches.ts"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { assert, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Socket from "effect/socket/Socket";
4+
5+
import {
6+
joinSocketFrameParts,
7+
SOCKET_FRAME_PART_LENGTH,
8+
splitLargeSocketFrames,
9+
} from "./socketFrameParts.ts";
10+
11+
const unusedUpgrade = () => Effect.void;
12+
13+
function recordingSocket(sent: Array<string | Uint8Array>) {
14+
return Socket.make({
15+
reader: Effect.succeed({ pull: Effect.never, upgrade: unusedUpgrade }),
16+
writer: Effect.succeed({
17+
write: (chunk) =>
18+
Effect.sync(() => {
19+
if (!Socket.isCloseEvent(chunk)) sent.push(chunk);
20+
}),
21+
writeAll: (chunks) => Effect.sync(() => void sent.push(...chunks)),
22+
}),
23+
});
24+
}
25+
26+
function replayingSocket(batches: ReadonlyArray<readonly [string, ...Array<string>]>) {
27+
let next = 0;
28+
return Socket.make({
29+
reader: Effect.succeed({
30+
pull: Effect.suspend(() =>
31+
next < batches.length ? Effect.succeed(batches[next++]!) : Effect.never,
32+
),
33+
upgrade: unusedUpgrade,
34+
}),
35+
writer: Effect.succeed({ write: () => Effect.void, writeAll: () => Effect.void }),
36+
});
37+
}
38+
39+
it.effect("a large message reaches the client whole, with a frame for every part on the way", () =>
40+
Effect.gen(function* () {
41+
// Emoji put surrogate pairs across part boundaries.
42+
const message = JSON.stringify({
43+
_tag: "Chunk",
44+
values: ["😀".repeat(SOCKET_FRAME_PART_LENGTH), "x".repeat(SOCKET_FRAME_PART_LENGTH)],
45+
});
46+
const sent: Array<string | Uint8Array> = [];
47+
const serverWriter = yield* splitLargeSocketFrames(recordingSocket(sent)).writer;
48+
yield* serverWriter.write(message);
49+
yield* serverWriter.write('{"_tag":"Pong"}');
50+
51+
const parts = sent.filter((frame) => typeof frame === "string");
52+
assert.isAbove(parts.length, 3);
53+
assert.isTrue(parts.every((part) => part.length <= SOCKET_FRAME_PART_LENGTH + 1));
54+
assert.isTrue(
55+
parts.every((part) => new TextDecoder().decode(new TextEncoder().encode(part)) === part),
56+
);
57+
58+
const [first, ...rest] = parts;
59+
const reader = yield* joinSocketFrameParts(replayingSocket([[first!, ...rest]])).reader;
60+
const received = yield* reader.pull;
61+
62+
assert.strictEqual(received.length, parts.length);
63+
const held = received.slice(0, -2);
64+
assert.isTrue(held.every((frame) => JSON.parse(frame as string)._tag === "Pong"));
65+
assert.strictEqual(received.at(-2), message);
66+
assert.strictEqual(received.at(-1), '{"_tag":"Pong"}');
67+
}).pipe(Effect.scoped),
68+
);
69+
70+
it.effect("a server that predates parts still reaches the client whole", () =>
71+
Effect.gen(function* () {
72+
const message = JSON.stringify({
73+
_tag: "Chunk",
74+
values: ["x".repeat(3 * SOCKET_FRAME_PART_LENGTH)],
75+
});
76+
const reader = yield* joinSocketFrameParts(replayingSocket([[message]])).reader;
77+
assert.deepStrictEqual(yield* reader.pull, [message]);
78+
}).pipe(Effect.scoped),
79+
);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as Arr from "effect/Array";
2+
import * as Effect from "effect/Effect";
3+
import * as Socket from "effect/socket/Socket";
4+
5+
/**
6+
* A client sets this on its WebSocket URL to accept large messages in parts.
7+
* Servers that predate it ignore it and keep sending whole messages.
8+
*/
9+
export const SOCKET_FRAME_PARTS_QUERY_PARAM = "frameParts";
10+
11+
// The RPC client's ping is answered behind whatever the server is already
12+
// sending, and a WebSocket message only arrives once all of it has. On a slow
13+
// link a multi-megabyte config snapshot outlasts the ping timeout, so the
14+
// client drops a healthy server and asks for the same snapshot again. Parts
15+
// this size keep a frame arriving within the timeout down to a few KB/s.
16+
export const SOCKET_FRAME_PART_LENGTH = 64 * 1024;
17+
18+
// No JSON message starts with a record separator. It marks every part but the last.
19+
const PART_PREFIX = "\u001e";
20+
21+
// The RPC client only counts frames that decode to messages as liveness, and
22+
// ignores pongs, so each held-back part reads to it as one.
23+
const PART_RECEIVED_FRAME = '{"_tag":"Pong"}';
24+
25+
export function splitSocketFrame(
26+
frame: string,
27+
partLength = SOCKET_FRAME_PART_LENGTH,
28+
): Arr.NonEmptyReadonlyArray<string> {
29+
if (frame.length <= partLength) return [frame];
30+
const parts: Array<string> = [];
31+
let start = 0;
32+
while (frame.length - start > partLength) {
33+
let end = start + partLength;
34+
// A lone surrogate half does not survive the UTF-8 encoding of a text frame.
35+
const last = frame.charCodeAt(end - 1);
36+
if (last >= 0xd800 && last <= 0xdbff) end -= 1;
37+
parts.push(PART_PREFIX + frame.slice(start, end));
38+
start = end;
39+
}
40+
return Arr.append(parts, frame.slice(start));
41+
}
42+
43+
/** Server side: sends string frames longer than one part as consecutive parts. */
44+
export function splitLargeSocketFrames(socket: Socket.Socket): Socket.Socket {
45+
const split = (chunk: string | Uint8Array): Arr.NonEmptyReadonlyArray<string | Uint8Array> =>
46+
typeof chunk === "string" ? splitSocketFrame(chunk) : [chunk];
47+
return Socket.make({
48+
reader: socket.reader,
49+
writer: Effect.map(socket.writer, (writer) => ({
50+
write: (chunk) =>
51+
typeof chunk === "string" && chunk.length > SOCKET_FRAME_PART_LENGTH
52+
? writer.writeAll(splitSocketFrame(chunk))
53+
: writer.write(chunk),
54+
writeAll: (chunks) => writer.writeAll(Arr.flatMap(chunks, split)),
55+
})),
56+
});
57+
}
58+
59+
/** Client side: joins parts back into whole frames. */
60+
export function joinSocketFrameParts(socket: Socket.Socket): Socket.Socket {
61+
return Socket.make({
62+
reader: Effect.map(socket.reader, (reader) => {
63+
let pending = "";
64+
const join = (frame: string | Uint8Array) => {
65+
if (typeof frame !== "string") return frame;
66+
if (frame.startsWith(PART_PREFIX)) {
67+
pending += frame.slice(PART_PREFIX.length);
68+
return PART_RECEIVED_FRAME;
69+
}
70+
if (pending === "") return frame;
71+
const whole = pending + frame;
72+
pending = "";
73+
return whole;
74+
};
75+
return {
76+
upgrade: reader.upgrade,
77+
pull: Effect.map(reader.pull, Arr.map(join)),
78+
};
79+
}),
80+
writer: socket.writer,
81+
});
82+
}

0 commit comments

Comments
 (0)