Skip to content

Commit a83b50b

Browse files
d-csclaude
andcommitted
fix(mollifier): guard drainer shutdown registration against listener stacking
Worker.init() is called per request from entry.server.tsx, so the process.once SIGTERM/SIGINT pair added in 98c1520 would stack a fresh listener every request under dev hot-reload (process.once only removes after firing). Gate registration on a process-global flag, matching the existing __worker__ pattern. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f557a18 commit a83b50b

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

apps/webapp/app/services/worker.server.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ let workerQueue: ZodWorker<typeof workerCatalog>;
107107

108108
declare global {
109109
var __worker__: ZodWorker<typeof workerCatalog>;
110+
var __mollifierShutdownRegistered__: boolean | undefined;
110111
}
111112

112113
// this is needed because in development we don't want to restart
@@ -132,18 +133,22 @@ export async function init() {
132133

133134
try {
134135
const drainer = getMollifierDrainer();
135-
if (drainer) {
136+
if (drainer && !global.__mollifierShutdownRegistered__) {
136137
// The drainer owns a polling loop and a Redis client; let it drain
137138
// in-flight pops on shutdown rather than tearing the process down
138-
// mid-handler. Idempotent — `drainer.stop()` short-circuits if already
139-
// stopped, so registering on both signals is safe.
139+
// mid-handler. `init()` is called per request from entry.server.tsx,
140+
// and `process.once()` only removes its listener after it fires — so
141+
// without a process-global guard, dev hot-reloads would stack a fresh
142+
// listener pair every request. Mirrors the `__worker__` singleton
143+
// pattern above.
140144
const stopDrainer = () => {
141145
drainer.stop().catch((error) => {
142146
logger.error("Failed to stop mollifier drainer", { error });
143147
});
144148
};
145149
process.once("SIGTERM", stopDrainer);
146150
process.once("SIGINT", stopDrainer);
151+
global.__mollifierShutdownRegistered__ = true;
147152
}
148153
} catch (error) {
149154
logger.error("Failed to initialise mollifier drainer", { error });

0 commit comments

Comments
 (0)