fix(gateway): crash safety on screen-share input timeout + proper error serialization

Root cause of "langsung left": YouTube bot-block/403 on u_c1tRmj7E4 (live
stream, LOGIN_REQUIRED) made yt-dlp timeout in resolveInputWithRetry (12s).
The timeout handler did cleanup() (removing once() listeners) THEN
tee.destroy(new Error(...)) — the PassThrough emitted 'error' with NO
listener left → unhandled stream 'error' event → uncaughtException →
gracefulShutdown → bot left voice.

Fix:
- resolveInputWithRetry: tee.destroy() silently after cleanup (error carried
  in the rejection only); add permanent no-op tee.on('error') safety.
- prepareStream: output.on('error') no-op so ffmpeg spawn failure before
  playStream attaches a demux listener never crashes the gateway.
- bootstrap: serialize uncaughtException/ClientError/DB errors with
  {err, errorMsg, stack} (pino only serializes the 'err' magic key — the old
  {error: err} key printed {} so crashes were invisible).
This commit is contained in:
asepharyana
2026-08-12 16:59:41 +07:00
parent 354e378e74
commit c5898f7cf0
3 changed files with 30 additions and 8 deletions
+16 -3
View File
@@ -222,7 +222,10 @@ export async function initializeDiscordGateway() {
await initializeDatabase();
logger.info("PostgreSQL database initialized");
} catch (err) {
logger.error({ error: err }, "Failed to initialize database");
logger.error(
{ err, errorMsg: err instanceof Error ? err.message : String(err) },
"Failed to initialize database",
);
throw new DatabaseError(
`Database initialization failed: ${err instanceof Error ? err.message : String(err)}`,
);
@@ -267,7 +270,10 @@ export async function initializeDiscordGateway() {
});
client.on("error", (err) => {
logger.error({ error: err }, "Client error");
logger.error(
{ err, errorMsg: err instanceof Error ? err.message : String(err) },
"Client error",
);
});
process.on("SIGINT", () => {
@@ -298,7 +304,14 @@ export async function initializeDiscordGateway() {
);
return;
}
logger.error({ error: err }, "Uncaught exception");
logger.error(
{
err,
errorMsg: err instanceof Error ? err.message : String(err),
stack: err?.stack,
},
"Uncaught exception",
);
gracefulShutdown("uncaughtException");
});