From 914afd19bdbf124bbae5a73f6d33c0e83fe8fe09 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Wed, 23 Sep 2026 11:36:50 +0000 Subject: [PATCH] gateway: guard child-process spawn errors and skip a pointless disconnect() stall Two more unhandled-EventEmitter crash/latency bugs in the same spot as the last one: spawn() itself can fail asynchronously (bad CONNECTOR_BIN path, no exec permission, or EMFILE/ENFILE under load) and emits an unhandled 'error' on the child process object - just as fatal to the whole gateway as the stdin EPIPE case, and more likely to hit exactly when the most connections are already open. Now caught and turned into a normal error event. disconnect() attached its exit listener with child.once("exit", ...), which never fires if the connector already exited before disconnect() was called - 'exit' fires exactly once, and by then it's already happened. That always fell through to the 3s hard-kill timeout for a process that was already gone, needlessly stalling explicit disconnects and same-socket reconnects after the connector died on its own. A childGone flag, set by both the new error handler and the existing exit handler, lets disconnect() skip that wait entirely when there's nothing left to wait for. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- gateway/src/ts3/connection.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/gateway/src/ts3/connection.ts b/gateway/src/ts3/connection.ts index 0f1cf37..39f562e 100644 --- a/gateway/src/ts3/connection.ts +++ b/gateway/src/ts3/connection.ts @@ -237,6 +237,11 @@ const CONNECTOR_BIN = export class Ts3Connection { private listeners = new Set<(event: Ts3ConnectionEvent) => void>(); private child?: ChildProcessWithoutNullStreams; + // Set once the child is confirmed gone - either it exited, or it never + // managed to spawn at all. disconnect() uses this to skip waiting on an + // "exit" event that already fired (and so will never fire again) before + // disconnect() got a chance to listen for it. + private childGone = false; constructor(private options: Ts3ConnectOptions) {} @@ -277,6 +282,18 @@ export class Ts3Connection { // isolated write failure instead of a process-wide crash. this.child.stdin.on("error", () => {}); + // spawn() itself can fail asynchronously - wrong CONNECTOR_BIN path, no + // exec permission, or (under load, with many connections already open) + // EMFILE/ENFILE from running out of file descriptors. That failure surfaces + // as an unhandled 'error' event on the child process object, which is just + // as fatal to the whole gateway process as the stdin case above - and far + // more likely to hit at the worst possible time, since a file-descriptor + // limit is reached exactly when the most other connections are active. + this.child.on("error", (err) => { + this.childGone = true; + this.emit({ type: "error", message: `Connector failed to start: ${err.message}` }); + }); + createInterface({ input: this.child.stdout }).on("line", (line) => { try { interface RawClientInfo { @@ -583,6 +600,7 @@ export class Ts3Connection { }); this.child.on("exit", (code) => { + this.childGone = true; if (code !== 0) { this.emit({ type: "error", message: `Connector exited with code ${code}` }); } @@ -927,7 +945,11 @@ export class Ts3Connection { } async disconnect(message = ""): Promise { - if (this.child && !this.child.killed) { + // childGone means the "exit" event already fired - which happens exactly + // once, so a .once("exit", ...) listener attached only now (below) would + // never see it, and this would always fall through to the 3s hard-kill + // timeout for a process that's already gone. Skip straight past it. + if (this.child && !this.child.killed && !this.childGone) { const child = this.child; const sanitized = message.replace(/[\r\n]+/g, " ").trim(); child.stdin.write(`disconnect ${sanitized}\n`);