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 }; }