From 25007d11b9d46b5d9c9a358752e8ff0fb25b35cd Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 24 Oct 2026 15:55:17 -0500 Subject: [PATCH] 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 --- CHANGELOG.md | 6 ++++++ src/http/shutdown.ts | 6 ++++-- test/close-deadline.test.ts | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/close-deadline.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index b15cdb2..7440d97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -260,6 +260,12 @@ npm treats versions as semver and semver forbids leading zeros. ### Fixed +- **A graceful close that throws synchronously no longer rejects `close()`** (GitHub #47). On Deno + and Bun, `closeWithDeadline` called the graceful path directly, so a throw before it returned a + promise escaped as a rejection of `close()`, while the deadline timer stayed armed and later + forced the server and logged a timeout nobody was waiting for. A synchronous throw is now handled + like a rejection: `close()` resolves, and the timer is cleared. + - **`listen()` rejects when the port is taken** (#138). A bind failure such as `EADDRINUSE` or `EACCES` arrives as the server's `error` event, which `listen()` did not handle: the promise never settled, and the error surfaced as an unhandled one. It now rejects with that error. The diff --git a/src/http/shutdown.ts b/src/http/shutdown.ts index 3bb4af2..9871bfb 100644 --- a/src/http/shutdown.ts +++ b/src/http/shutdown.ts @@ -44,7 +44,9 @@ export function closeWithDeadline( }; // A graceful path that rejects still has to end the shutdown: the deadline covers a slow - // close, not a broken one, and leaving close() pending forever is the worse failure. - void graceful().then(finish, finish); + // close, not a broken one, and leaving close() pending forever is the worse failure. Called + // inside `then` so a graceful path that throws before returning its promise counts as a + // rejection too, rather than rejecting close() and leaving the timer to force a closed server. + void Promise.resolve().then(graceful).then(finish, finish); }); } diff --git a/test/close-deadline.test.ts b/test/close-deadline.test.ts new file mode 100644 index 0000000..6741eaa --- /dev/null +++ b/test/close-deadline.test.ts @@ -0,0 +1,19 @@ +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 => { + 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(); + }); +});