Skip to content

Commit eac52f0

Browse files
t3dotggclaude
andauthored
fix(client-runtime): closing a busy stream no longer drops the connection (#15563)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d60a71e commit eac52f0

3 files changed

Lines changed: 198 additions & 153 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,50 @@ describe("RpcSessionFactory", () => {
11121112
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
11131113
);
11141114

1115+
it.effect("keeps reading replies after closing a stream with a full buffer", () =>
1116+
Effect.gen(function* () {
1117+
const { factory, sockets } = yield* makeFactory();
1118+
const session = yield* factory.connect(PREPARED);
1119+
const readyFiber = yield* Effect.forkChild(session.ready);
1120+
const socket = yield* awaitSocket(sockets);
1121+
socket.open();
1122+
yield* completeInitialConfig(socket);
1123+
yield* Fiber.join(readyFiber);
1124+
1125+
// The consumer takes one event and stops pulling, so the stream buffer fills.
1126+
const consuming = yield* Deferred.make<void>();
1127+
const streamFiber = yield* session.client[WS_METHODS.subscribeServerConfig]({}).pipe(
1128+
Stream.runForEach(() =>
1129+
Deferred.succeed(consuming, undefined).pipe(Effect.andThen(Effect.never)),
1130+
),
1131+
Effect.forkChild,
1132+
);
1133+
const streamRequest = yield* awaitRequest(socket, 1);
1134+
const snapshot = { version: 1, type: "snapshot", config: ENCODED_SERVER_CONFIG };
1135+
socket.serverMessage(
1136+
encodeJson({
1137+
_tag: "Chunk",
1138+
requestId: streamRequest.id,
1139+
values: Array.from({ length: 64 }, () => snapshot),
1140+
}),
1141+
);
1142+
yield* Deferred.await(consuming);
1143+
yield* Fiber.interrupt(streamFiber);
1144+
1145+
const probeFiber = yield* Effect.forkChild(session.probe);
1146+
const probeRequest = yield* awaitRequest(socket, 2);
1147+
expect(probeRequest).toMatchObject({ tag: WS_METHODS.serverProbe });
1148+
socket.serverMessage(
1149+
encodeJson({
1150+
_tag: "Exit",
1151+
requestId: probeRequest.id,
1152+
exit: { _tag: "Success", value: {} },
1153+
}),
1154+
);
1155+
yield* Fiber.join(probeFiber);
1156+
}).pipe(Effect.scoped),
1157+
);
1158+
11151159
it.effect("reaches ready when a newer server sends unknown config members", () =>
11161160
Effect.gen(function* () {
11171161
const { factory, sockets } = yield* makeFactory();

‎patches/[email protected]‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ index c586c60..49886d8 100644
157157
});
158158
});
159159
const onStreamRequest = Effect.fnUntraced(function* (rpc, middleware, payload, headers, streamBufferSize, context) {
160-
@@ -143,11 +150,17 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
160+
@@ -143,11 +150,18 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
161161
});
162162
const fiber = Fiber.getCurrent();
163163
const id = generateRequestId();
@@ -173,11 +173,12 @@ index c586c60..49886d8 100644
173173
+ if (!entry) return Effect.void;
174174
entries.delete(id);
175175
- return sendInterrupt(id, Exit.isFailure(exit) ? Array.from(Cause.interruptors(exit.cause)) : [], context);
176-
+ return sendInterrupt(id, Exit.isFailure(exit) ? Array.from(Cause.interruptors(exit.cause)) : [], context, entry.rpc._tag);
176+
+ // Shutting down releases a socket reader blocked offering into this full buffer.
177+
+ return Queue.shutdown(entry.queue).pipe(Effect.andThen(sendInterrupt(id, Exit.isFailure(exit) ? Array.from(Cause.interruptors(exit.cause)) : [], context, entry.rpc._tag)));
177178
});
178179
const queue = yield* Queue.bounded(streamBufferSize);
179180
entries.set(id, {
180-
@@ -155,9 +168,10 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
181+
@@ -155,9 +169,10 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
181182
rpc,
182183
queue,
183184
scope,
@@ -190,7 +191,7 @@ index c586c60..49886d8 100644
190191
message,
191192
context,
192193
discard: false
193-
@@ -172,7 +186,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
194+
@@ -172,7 +187,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
194195
sampled: span.sampled
195196
} : {}),
196197
headers: Headers.merge(fiber.getRef(CurrentHeaders), headers)
@@ -199,7 +200,7 @@ index c586c60..49886d8 100644
199200
captureStackTrace: false
200201
}) : identity, Effect.catchCause(error => Queue.failCause(queue, error)), Effect.interruptible, Effect.forkIn(scope, {
201202
startImmediately: true
202-
@@ -202,7 +216,10 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
203+
@@ -202,7 +217,10 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
203204
});
204205
};
205206
};
@@ -211,7 +212,7 @@ index c586c60..49886d8 100644
211212
const parentFiber = Fiber.getCurrent();
212213
const fiber = options.onFromClient({
213214
message: {
214-
@@ -216,7 +233,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
215+
@@ -216,7 +234,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
215216
fiber.addObserver(() => {
216217
resume(Effect.void);
217218
});
@@ -220,7 +221,7 @@ index c586c60..49886d8 100644
220221
const write = message => {
221222
switch (message._tag) {
222223
case "Chunk":
223-
@@ -224,7 +241,13 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
224+
@@ -224,7 +242,13 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
224225
const requestId = message.requestId;
225226
const entry = entries.get(requestId);
226227
if (!entry || entry._tag !== "Queue") return Effect.void;
@@ -235,7 +236,7 @@ index c586c60..49886d8 100644
235236
message: {
236237
_tag: "Ack",
237238
requestId: message.requestId
238-
@@ -239,11 +262,18 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
239+
@@ -239,11 +263,18 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
239240
const entry = entries.get(requestId);
240241
if (!entry) return Effect.void;
241242
entries.delete(requestId);
@@ -257,7 +258,7 @@ index c586c60..49886d8 100644
257258
}
258259
case "Defect":
259260
{
260-
@@ -598,7 +628,7 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
261+
@@ -598,7 +629,7 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
261262
// `parser` is replaced on every connect, and a stateful serialization
262263
// encodes against the connection it is writing to, so the ping is encoded
263264
// when it is sent rather than once up front.
@@ -266,7 +267,7 @@ index c586c60..49886d8 100644
266267
let currentError;
267268
const broadcast = response => Effect.forEach(clientIds, clientId => writeResponse(clientId, response));
268269
const broadcastError = error => {
269-
@@ -618,8 +648,7 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
270+
@@ -618,8 +649,7 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
270271
body: () => {
271272
const response = responses[i++];
272273
if (response._tag === "Pong") {
@@ -276,7 +277,7 @@ index c586c60..49886d8 100644
276277
}
277278
if (Object.hasOwn(response, "requestId")) {
278279
const requestId = response.requestId;
279-
@@ -664,12 +693,12 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
280+
@@ -664,12 +694,12 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
280281
yield* processData(frames[i]);
281282
}
282283
}
@@ -291,7 +292,7 @@ index c586c60..49886d8 100644
291292
}).pipe(Option.isSome(hooks) ? Effect.ensuring(hooks.value.onDisconnect) : identity, Effect.tapCause(cause => {
292293
const error = Cause.findError(cause);
293294
const hasError = Result.isSuccess(error);
294-
@@ -710,20 +739,28 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
295+
@@ -710,20 +740,28 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
295296
};
296297
}));
297298
const defaultRetryPolicy = /*#__PURE__*/Schedule.min([/*#__PURE__*/Schedule.exponential(500, 1.5), /*#__PURE__*/Schedule.spaced(5000)]);
@@ -325,7 +326,7 @@ index c586c60..49886d8 100644
325326
}).pipe(Effect.delay("5 seconds"), Effect.ignore, Effect.forever, Effect.interruptible, Effect.forkScoped);
326327
return {
327328
timeout: latch.await,
328-
@@ -874,6 +911,11 @@ export const makeProtocolWorker = options => Protocol.make(Effect.fnUntraced(fun
329+
@@ -874,6 +912,11 @@ export const makeProtocolWorker = options => Protocol.make(Effect.fnUntraced(fun
329330
* @since 4.0.0
330331
*/
331332
export const layerProtocolWorker = /*#__PURE__*/flow(makeProtocolWorker, /*#__PURE__*/Layer.effect(Protocol));

0 commit comments

Comments
 (0)