diff --git a/CHANGELOG.md b/CHANGELOG.md index fa28d7a..e6b92b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -274,6 +274,12 @@ npm treats versions as semver and semver forbids leading zeros. `application/problem+json` and `application/vnd.api+json`, which used to arrive as text, now arrive parsed, and `application/json-patch+json`, which matched the old substring, still does. +- **Two concurrent `close()` calls both wait for teardown** (GitHub #45). The teardown registry + marked itself drained and returned at once to any second caller, so a `close()` from a signal + handler and another from a test hook ran teardown once, but only the first waited for it: the + second resolved while a database could still be open. Every caller now gets the promise of the + teardown already in flight, and it still runs once. + - **`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/lifecycle.ts b/src/lifecycle.ts index 8987220..9834a8a 100644 --- a/src/lifecycle.ts +++ b/src/lifecycle.ts @@ -25,7 +25,7 @@ export type TeardownFn = () => void | Promise; */ export class TeardownRegistry { private readonly callbacks: TeardownFn[] = []; - private drained = false; + private running: Promise | undefined; /** Register `fn` to run on shutdown. Later registrations run first — see {@link run}. */ add(fn: TeardownFn): void { @@ -53,12 +53,16 @@ export class TeardownRegistry { * was written, and one exists now. * * Never throws, and never runs twice — `close()` is called twice by more real applications than - * anyone expects, and a second call must not re-close what the first one closed. + * anyone expects, and a second call must not re-close what the first one closed. A second call + * made while the first is still running gets the same promise rather than an early return: a + * caller that awaits `close()` must not move on with a connection the first call has yet to close. */ - async run(logger: Logger): Promise { - if (this.drained) return; - this.drained = true; + run(logger: Logger): Promise { + this.running ??= this.drain(logger); + return this.running; + } + private async drain(logger: Logger): Promise { for (let index = this.callbacks.length - 1; index >= 0; index--) { try { await this.callbacks[index](); diff --git a/test/lifecycle.test.ts b/test/lifecycle.test.ts index 975e891..d9497f6 100644 --- a/test/lifecycle.test.ts +++ b/test/lifecycle.test.ts @@ -70,6 +70,23 @@ describe('TeardownRegistry', () => { expect(runs).toBe(1); }); + it('makes a concurrent second close() wait for the teardown the first one started (GitHub #45)', async () => { + const order: string[] = []; + const registry = new TeardownRegistry(); + registry.add(async () => { + await new Promise((resolve) => setTimeout(resolve, 20)); + order.push('torn down'); + }); + + const logger = silentLogger(); + await Promise.all([ + registry.run(logger).then(() => order.push('first returned')), + registry.run(logger).then(() => order.push('second returned')), + ]); + + expect(order).toEqual(['torn down', 'first returned', 'second returned']); + }); + it('resolves when nothing is registered, and reports an empty size', async () => { const registry = new TeardownRegistry(); expect(registry.size).toBe(0); diff --git a/test/teardown.test.ts b/test/teardown.test.ts index 512c6f4..2bcd2fd 100644 --- a/test/teardown.test.ts +++ b/test/teardown.test.ts @@ -224,4 +224,29 @@ describe('shutdown teardown', () => { expect(runs).toBe(1); }); + + it('makes two concurrent close() calls both wait for the one teardown (GitHub #45)', async () => { + const order: string[] = []; + let runs = 0; + const plugin: Plugin = { + name: 'slow-teardown', + mount(api) { + api.onShutdown(async () => { + runs++; + await new Promise((r) => setTimeout(r, 20)); + order.push('torn down'); + }); + }, + }; + + @Module({ mountpoint: '/', controllers: [Ctl] }) + class M {} + const app = createApp({ modules: [M], plugins: [plugin], logger: silentLogger() }); + await app.listen(0); + // A signal handler and a test hook closing at once, say. + await Promise.all([app.close().then(() => order.push('first')), app.close().then(() => order.push('second'))]); + + expect(runs).toBe(1); + expect(order).toEqual(['torn down', 'first', 'second']); + }); });