fix(lifecycle): make a concurrent close() wait for the teardown in flight
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>
This commit is contained in:
4 files changed
+57
-5
No files matched your search
@@ -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']);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user