fix(app): arm the shutdown timer before finish(), and make the deadline an app option
Closes #20. finish() called clearTimeout(timer) from above the timer's own declaration. It worked because server.close(cb) always defers, so the assignment won the race — correct by timing, and a ReferenceError waiting in the shutdown path for whoever added a synchronous call site. The issue proposed hoisting a `let timer`, but prefer-const rejects that and is right to. Swapping the two blocks fixes it without a suppression and lands somewhere better: the surviving forward reference is the timer callback reaching finish(), and setTimeout not firing synchronously is the event loop rather than a library's current behaviour. Closes #19. The 10s deadline is now settable via createApp({ shutdownTimeoutMs }), with the per-call close({ timeoutMs }) still winning. close() is usually reached from a signal handler, a test helper or a shutdown hook you do not own, where passing it per call is not available. Nothing changes for anyone passing neither. The precedence test uses a 30s app-level default, so it can only pass if the per-call value took priority. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+77
-17
No files matched your search
@@ -439,6 +439,28 @@ describe('devGraph endpoint', () => {
|
||||
});
|
||||
|
||||
describe('graceful shutdown', () => {
|
||||
// A module whose only route never responds, so shutdown always has something in flight to give
|
||||
// up on — the deadline is the only thing that can end close().
|
||||
const hangModule = () => {
|
||||
@Route('/')
|
||||
class Ctl {
|
||||
@Get('/hang') hang() {
|
||||
return new Promise(() => {});
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [Ctl] })
|
||||
class M {}
|
||||
return M;
|
||||
};
|
||||
|
||||
const listenHanging = async (app: ReturnType<typeof createApp>): Promise<number> => {
|
||||
const server = await app.listen(0);
|
||||
const port = (server.address() as any).port;
|
||||
fetch(`http://127.0.0.1:${port}/hang`).catch(() => {});
|
||||
await new Promise((r) => setTimeout(r, 50)); // let the request reach the handler
|
||||
return port;
|
||||
};
|
||||
|
||||
it('app.close() stops accepting and resolves, even with an SSE stream open', async () => {
|
||||
@Route('/')
|
||||
class Ctl {
|
||||
@@ -485,6 +507,28 @@ describe('graceful shutdown', () => {
|
||||
await app.close({ timeoutMs: 300 });
|
||||
expect(Date.now() - start).toBeLessThan(600);
|
||||
});
|
||||
|
||||
// The app-level default exists for the case where close() is reached from a signal handler or a
|
||||
// test helper nobody local owns, so it has to work with close() called bare.
|
||||
it('createApp({ shutdownTimeoutMs }) applies when close() is called with no options', async () => {
|
||||
const app = createApp({ modules: [hangModule()], shutdownTimeoutMs: 300 });
|
||||
await listenHanging(app);
|
||||
|
||||
const start = Date.now();
|
||||
await app.close();
|
||||
expect(Date.now() - start).toBeLessThan(600);
|
||||
});
|
||||
|
||||
// The interesting half of the precedence rule: an app-level default long enough to fail this
|
||||
// test if it won, so the assertion can only pass if the per-call value took priority.
|
||||
it('close({ timeoutMs }) overrides the app-level default', async () => {
|
||||
const app = createApp({ modules: [hangModule()], shutdownTimeoutMs: 30_000 });
|
||||
await listenHanging(app);
|
||||
|
||||
const start = Date.now();
|
||||
await app.close({ timeoutMs: 300 });
|
||||
expect(Date.now() - start).toBeLessThan(600);
|
||||
});
|
||||
});
|
||||
|
||||
describe('overrides', () => {
|
||||
|
||||
Reference in new issue
Block a user