fix(shutdown): treat a graceful close that throws synchronously as a rejection #166

Merged
chrnx merged 1 commits from fix/close-deadline-sync-throw into develop 2026-10-24 23:23:26 +00:00
Owner

Fixes GitHub #47.

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 path a rejection already took: the shutdown ends, close() resolves, and the timer is cleared.

Note on the expected behaviour: the issue body says the throw should not skip "the deadline and the forced close". This change follows the issue's own suggested fix and the existing contract in the code comment (a graceful path that rejects ends the shutdown at once, without forcing). If a sync throw should instead wait out the deadline and force, that also changes what an async rejection does, and belongs in its own change.

Test: test/close-deadline.test.ts. On develop it fails with "promise rejected 'Error: sync boom' instead of resolving". It also checks that neither onTimeout nor force runs after the deadline has passed.

Verified: lint (0 errors), format:check, typecheck, complexity:check, npm test (1162 passed, 107 files), test:deno (63 passed), test:bun (52 passed), test:edge (3/3 PASS).

Fixes GitHub #47. 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 path a rejection already took: the shutdown ends, close() resolves, and the timer is cleared. Note on the expected behaviour: the issue body says the throw should not skip "the deadline and the forced close". This change follows the issue's own suggested fix and the existing contract in the code comment (a graceful path that rejects ends the shutdown at once, without forcing). If a sync throw should instead wait out the deadline and force, that also changes what an async rejection does, and belongs in its own change. Test: test/close-deadline.test.ts. On develop it fails with "promise rejected 'Error: sync boom' instead of resolving". It also checks that neither onTimeout nor force runs after the deadline has passed. Verified: lint (0 errors), format:check, typecheck, complexity:check, npm test (1162 passed, 107 files), test:deno (63 passed), test:bun (52 passed), test:edge (3/3 PASS).
chrnx added 1 commit 2026-10-24 23:20:59 +00:00
fix(shutdown): treat a graceful close that throws synchronously as a rejection
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
51fa7c1a88
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>
chrnx force-pushed fix/close-deadline-sync-throw from 25007d11b9 to 51fa7c1a88 2026-10-24 23:20:59 +00:00 Compare
chrnx merged commit 1a24d4e1b6 into develop 2026-10-24 23:23:26 +00:00
Sign in to join this conversation.