fix(http): end an idle stream as soon as the client leaves (GitHub #40)
Both stream adapters waited for the source's pending next() after the client had disconnected. A source idle between events, such as an SSE feed with nothing to send, may never settle that next(), so on Node pipeStream never reached its finally: stream:close did not fire and the closer stayed registered, which graceful shutdown then still treated as a live stream. Through app.fetch, the ReadableStream's cancel awaited return(), which an async generator only runs once its pending next() settles, so cancelling the body did not settle either, with or without compression wrapping it. Each pull is now raced against the disconnect: the response's close event on Node, and the stream's cancel on the fetch path. The source's return() is called without being waited on, through a small releaseIterator helper that also guards a return() that throws synchronously, which on Node escaped from the close listener as an uncaught exception. The WebSocket abort listener already released its iterator this way and now uses the same helper. The new cases in stream-lifecycle-parity drive both adapters, with and without compression, against an async generator, a return() that never settles and a return() that throws. All twelve need the fix except the uncompressed fetch case with a generator, which already passed. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+122
-8
No files matched your search
@@ -13,6 +13,7 @@ import { describe, expect, it } from 'vitest';
|
||||
import { Bus } from '../src/bus';
|
||||
import { createHttpServer } from '../src/http';
|
||||
import { buildFetch } from '../src/http/web';
|
||||
import { resolveCompression } from '../src/http/compression';
|
||||
import type { RouteDef } from '../src/http/types';
|
||||
|
||||
const events = (bus: Bus): string[] => {
|
||||
@@ -159,3 +160,82 @@ describe('stream lifecycle events, on every adapter', () => {
|
||||
expect(seen).toEqual(['stream:open', 'stream:close']);
|
||||
});
|
||||
});
|
||||
|
||||
// GitHub #40. A source idle between events (an SSE feed waiting on something that may never happen)
|
||||
// has a `next()` that does not settle. Both adapters waited on it after the client left: Node kept
|
||||
// `pipeStream` running and its closer registered, so shutdown still saw a live stream, and the
|
||||
// fetch adapter's `cancel` waited on `return()`, which an async generator only runs once its pending
|
||||
// `next()` settles. The source's own `return()` must not be able to hold that up either.
|
||||
describe('a client leaving an idle stream', () => {
|
||||
const hang = new Promise<never>(() => {});
|
||||
|
||||
/** Yields once, then waits forever; `return()` does whatever `onReturn` does. */
|
||||
const idle = (onReturn: () => Promise<IteratorResult<unknown>>): AsyncIterable<unknown> => ({
|
||||
[Symbol.asyncIterator]: () => {
|
||||
let sent = false;
|
||||
return {
|
||||
next: () => (sent ? hang : ((sent = true), Promise.resolve({ value: { t: 1 }, done: false }))),
|
||||
return: onReturn,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const SOURCES: Record<string, () => AsyncIterable<unknown>> = {
|
||||
'an async generator': async function* () {
|
||||
yield { t: 1 };
|
||||
await hang;
|
||||
},
|
||||
'a return() that never settles': () => idle(() => hang),
|
||||
'a return() that throws': () =>
|
||||
idle(() => {
|
||||
throw new Error('return failed');
|
||||
}),
|
||||
};
|
||||
|
||||
/** Fails with a message instead of the test timing out. */
|
||||
const within = <T>(work: Promise<T>, what: string): Promise<T> =>
|
||||
Promise.race([work, new Promise<never>((_, reject) => setTimeout(() => reject(new Error(`${what}: hung`)), 1000))]);
|
||||
|
||||
for (const [source, make] of Object.entries(SOURCES))
|
||||
for (const compression of [undefined, resolveCompression(true)]) {
|
||||
const label = `${source}${compression ? ', compressed' : ''}`;
|
||||
|
||||
it(`node: ${label} — finishes and unregisters on disconnect`, async () => {
|
||||
const bus = new Bus();
|
||||
const closed = new Promise<void>((r) => bus.on('stream:close', () => r()));
|
||||
const streams = new Set<() => void>();
|
||||
const server = createHttpServer(route(make), [], bus, undefined, { streams, compression });
|
||||
await new Promise<void>((r) => server.listen(0, r));
|
||||
const { port } = server.address() as { port: number };
|
||||
const ac = new AbortController();
|
||||
|
||||
try {
|
||||
const res = await fetch(`http://127.0.0.1:${port}/feed`, {
|
||||
signal: ac.signal,
|
||||
headers: { 'accept-encoding': 'gzip' },
|
||||
});
|
||||
await res.body!.getReader().read();
|
||||
ac.abort();
|
||||
|
||||
await within(closed, 'stream:close');
|
||||
expect(streams.size).toBe(0); // nothing left for a graceful shutdown to wait on
|
||||
} finally {
|
||||
server.closeAllConnections();
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
|
||||
it(`fetch: ${label} — cancel settles at once`, async () => {
|
||||
const bus = new Bus();
|
||||
const seen = events(bus);
|
||||
const res = await buildFetch(route(make), { bus, compression })(
|
||||
new Request('http://x/feed', { headers: { 'accept-encoding': 'gzip' } }),
|
||||
);
|
||||
const reader = res.body!.getReader();
|
||||
await reader.read();
|
||||
|
||||
await within(reader.cancel(), 'cancel()');
|
||||
expect(seen).toEqual(['stream:open', 'stream:close']);
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user