diff --git a/.changeset/fuzzy-ghosts-admire.md b/.changeset/fuzzy-ghosts-admire.md new file mode 100644 index 0000000000..56982d015f --- /dev/null +++ b/.changeset/fuzzy-ghosts-admire.md @@ -0,0 +1,5 @@ +--- +"trigger.dev": patch +--- + +fix(dev): stop max listeners exceeded warning messages when running more than 10 runs concurrently diff --git a/packages/cli-v3/src/dev/devSupervisor.ts b/packages/cli-v3/src/dev/devSupervisor.ts index 98214d7a00..67da7e5945 100644 --- a/packages/cli-v3/src/dev/devSupervisor.ts +++ b/packages/cli-v3/src/dev/devSupervisor.ts @@ -138,11 +138,26 @@ class DevSupervisor implements WorkerRuntime { //start an SSE connection for presence this.disconnectPresence = await this.#startPresenceConnection(); + // Handle SIGTERM to gracefully stop all run controllers + process.on("SIGTERM", this.#handleSigterm); + //start dequeuing await this.#dequeueRuns(); } + #handleSigterm = async () => { + logger.debug("[DevSupervisor] Received SIGTERM, stopping all run controllers"); + + const stopPromises = Array.from(this.runControllers.values()).map((controller) => + controller.stop() + ); + + await Promise.allSettled(stopPromises); + }; + async shutdown(): Promise { + process.off("SIGTERM", this.#handleSigterm); + this.disconnectPresence?.(); try { this.socket?.close(); diff --git a/packages/cli-v3/src/entryPoints/dev-run-controller.ts b/packages/cli-v3/src/entryPoints/dev-run-controller.ts index 12876b3240..e5578567b4 100644 --- a/packages/cli-v3/src/entryPoints/dev-run-controller.ts +++ b/packages/cli-v3/src/entryPoints/dev-run-controller.ts @@ -122,14 +122,8 @@ export class DevRunController { logger.debug("[DevRunController] Failed to poll for snapshot", { error }); }, }); - - process.on("SIGTERM", this.sigterm.bind(this)); } - private async sigterm() { - logger.debug("[DevRunController] Received SIGTERM, stopping worker"); - await this.stop(); - } // This should only be used when we're already executing a run. Attempt number changes are not allowed. private updateRunPhase(run: Run, snapshot: Snapshot) { @@ -856,8 +850,6 @@ export class DevRunController { async stop() { logger.debug("[DevRunController] Shutting down"); - process.off("SIGTERM", this.sigterm); - if (this.taskRunProcess && !this.taskRunProcess.isBeingKilled) { try { const version = this.opts.worker.serverWorker?.version || "unknown";