fix(lifecycle): make a concurrent close() wait for the teardown in flight #164

Merged
chrnx merged 1 commits from fix/concurrent-close into develop 2026-10-24 23:20:53 +00:00
Owner

Fixes GitHub #45.

TeardownRegistry.run set a drained flag and returned at once to any later caller. Teardown ran once, but a second close() made while the first was still awaiting its callbacks resolved straight away, so a signal handler and a test hook closing together could move on with a database still open. This reached every close path: app.close() with or without a listen()ed server, and serveDeno/serveBun, which hand over to app.close().

The registry now keeps the promise of the teardown it started and returns it to every caller. Callbacks still run once, and each caller resolves only after they finish, within its own runTeardown budget. The drain window and closeApp needed no change: the health drain already shares one window, and a second server.close() waits for the same 'close' event. close() after a failed listen() (#138) still has no server and only runs teardown; its tests pass unchanged.

Tests: a registry-level test in test/lifecycle.test.ts and an app-level one in test/teardown.test.ts (two concurrent app.close() on a listen()ed app with a slow onShutdown). Both failed on develop with the second caller resolving before 'torn down'.

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

Fixes GitHub #45. TeardownRegistry.run set a `drained` flag and returned at once to any later caller. Teardown ran once, but a second close() made while the first was still awaiting its callbacks resolved straight away, so a signal handler and a test hook closing together could move on with a database still open. This reached every close path: app.close() with or without a listen()ed server, and serveDeno/serveBun, which hand over to app.close(). The registry now keeps the promise of the teardown it started and returns it to every caller. Callbacks still run once, and each caller resolves only after they finish, within its own runTeardown budget. The drain window and closeApp needed no change: the health drain already shares one window, and a second server.close() waits for the same 'close' event. close() after a failed listen() (#138) still has no server and only runs teardown; its tests pass unchanged. Tests: a registry-level test in test/lifecycle.test.ts and an app-level one in test/teardown.test.ts (two concurrent app.close() on a listen()ed app with a slow onShutdown). Both failed on develop with the second caller resolving before 'torn down'. Verified: lint (0 errors), format:check, typecheck, complexity:check, npm test (1162 passed), test:deno (63 passed), test:bun (52 passed), test:edge (PASS).
chrnx added 1 commit 2026-10-24 23:18:27 +00:00
fix(lifecycle): make a concurrent close() wait for the teardown in flight
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (deno) (pull_request) Successful in 32s
CI / runtimes (edge) (pull_request) Successful in 41s
212d2fa5c4
TeardownRegistry.run set a `drained` flag and returned at once to any
later caller. That kept teardown from running twice, but a second
close() made while the first was still awaiting its callbacks resolved
straight away. A signal handler and a test hook closing together could
then move on with a database the first call had not closed yet. This
reached every close path: app.close() with or without a listen()ed
server, and serveDeno/serveBun, which hand over to app.close().

The registry now keeps the promise of the teardown it started and
returns it to every caller, so the callbacks still run once and each
caller resolves only after they finish, within its own runTeardown
budget. The drain window and closeApp needed no change: the health
drain already shares one window, and a second server.close() waits for
the same 'close' event. close() after a failed listen() still has no
server and only runs teardown.

GitHub #45.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed fix/concurrent-close from e6f6f90cc2 to 212d2fa5c4 2026-10-24 23:18:27 +00:00 Compare
chrnx merged commit c7558ccbeb into develop 2026-10-24 23:20:53 +00:00
Sign in to join this conversation.