From 4b93641cf413fe0f375ba71d3234d69ca13cced6 Mon Sep 17 00:00:00 2001 From: Mika Kuns Date: Wed, 20 May 2026 14:09:11 +0200 Subject: [PATCH] fix(server): register onClose hook before app.listen Fastify forbids addHook after the instance is listening, so the sweep-timer cleanup hook from 1.5.0 threw on every `serve` startup and crashed the daemon. Register the hook first, then start listening, and assign the timer through a ref. --- node/src/server.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/node/src/server.ts b/node/src/server.ts index 15cc318..a58605d 100644 --- a/node/src/server.ts +++ b/node/src/server.ts @@ -156,10 +156,11 @@ export async function startServer( ): Promise<{ app: FastifyInstance; store: MailboxStore; sweepTimer: NodeJS.Timeout | null }> { const store = new MailboxStore(cfg.dbPath); const app = await buildServer(cfg, store); - await app.listen({ host: cfg.bind, port: cfg.port }); - const sweepTimer = startSweep(store, cfg, app.log); + const timerRef: { current: NodeJS.Timeout | null } = { current: null }; app.addHook("onClose", async () => { - if (sweepTimer) clearInterval(sweepTimer); + if (timerRef.current) clearInterval(timerRef.current); }); - return { app, store, sweepTimer }; + await app.listen({ host: cfg.bind, port: cfg.port }); + timerRef.current = startSweep(store, cfg, app.log); + return { app, store, sweepTimer: timerRef.current }; }