Files
core/test/close-deadline.test.ts
T
chrnx 51fa7c1a88
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 18s
CI / runtimes (deno) (pull_request) Successful in 33s
fix(shutdown): treat a graceful close that throws synchronously as a rejection
closeWithDeadline called graceful() directly inside the Promise
executor. A graceful path that threw before returning its promise
escaped the executor, so the close() that serveDeno and serveBun
return rejected on the spot. The deadline timer had already been armed
and nothing cleared it, so it later fired anyway: it logged a timeout
and forced a server whose close() had already given up.

graceful() now runs inside Promise.resolve().then(), so a synchronous
throw becomes a rejection and takes the same path one already took: the
shutdown ends, close() resolves, and the timer is cleared.

GitHub #47.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-24 18:20:56 -05:00

20 lines
754 B
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { closeWithDeadline } from '../src/http/shutdown';
describe('closeWithDeadline', () => {
it('treats a graceful close that throws synchronously like one that rejects (GitHub #47)', async () => {
const force = vi.fn();
const onTimeout = vi.fn();
const graceful = (): Promise<void> => {
throw new Error('sync boom');
};
await expect(closeWithDeadline(graceful, force, 20, onTimeout)).resolves.toBeUndefined();
// Past the deadline: a settled close must not leave its timer behind to force anything later.
await new Promise((resolve) => setTimeout(resolve, 40));
expect(onTimeout).not.toHaveBeenCalled();
expect(force).not.toHaveBeenCalled();
});
});