refactor(relay): release buffered request bodies on the delivery deadline

Settle the buffered-body wait when the delivery deadline fires so the
stream's buffered chunks are freed immediately, and make the deadline
injectable for tests. Covers the stalled-mid-body path with a test.
This commit is contained in:
Bohdan Triapitsyn
2026-08-12 16:57:46 +03:00
parent 6a2f0b8135
commit 95c5a0a69c
2 changed files with 40 additions and 4 deletions
+8 -3
View File
@@ -96,10 +96,11 @@ const isWsClosePayload = (parsed) => Boolean(parsed && typeof parsed === 'object
* getLocalPort: () => number,
* sendFrame: (plaintextFrame: Uint8Array) => void | Promise<void>,
* getBufferedAmount: () => number,
* bodyDeliveryTimeoutMs?: number,
* }} deps
*/
export const createTunnelHost = ({ connectionId, getLocalPort, sendFrame, getBufferedAmount }) => {
/** @type {Map<number, { kind: 'http', abort: AbortController, body: ReadableStreamDefaultController | null } | { kind: 'ws', socket: WebSocket, opened: boolean }>} */
export const createTunnelHost = ({ connectionId, getLocalPort, sendFrame, getBufferedAmount, bodyDeliveryTimeoutMs = BODY_DELIVERY_TIMEOUT_MS }) => {
/** @type {Map<number, { kind: 'http', abort: AbortController, body: { enqueue(payload: Uint8Array): void, close(): void, error(error: Error): void } | null, noBody: boolean } | { kind: 'ws', socket: WebSocket, opened: boolean }>} */
const streams = new Map();
const assembler = createFragmentAssembler();
let closed = false;
@@ -329,8 +330,12 @@ export const createTunnelHost = ({ connectionId, getLocalPort, sendFrame, getBuf
if (streams.get(streamId) === stream && !completed && !liveStream) {
dropStream(streamId);
void sendAbort(streamId, 'tunnel request body was not delivered in time');
// Settle the buffered-body wait below so the stream's buffered chunks
// and this call frame are released (the post-wait guard sees the
// dropped stream and returns without a second abort).
finishBody(new Error('tunnel request body was not delivered in time'));
}
}, BODY_DELIVERY_TIMEOUT_MS);
}, bodyDeliveryTimeoutMs);
deliveryDeadline.unref?.();
await bodyEnded;