diff --git a/index.mjs b/index.mjs index 3c695f5..f0e65fe 100644 --- a/index.mjs +++ b/index.mjs @@ -1055,6 +1055,13 @@ const isIgnorableWsCrash = (err) => { const isBenignAudioStopRace = (err) => String(err?.message ?? err ?? "").includes("AudioSource is closed"); +/** + * @param {Error} err - The error to check. + * @returns {boolean} True if the error is the stream abort race. + */ +const isBenignStreamAbortRace = (err) => + err?.code === "ECONNRESET" && String(err?.message ?? "").includes("aborted"); + let _lastWsCrashLog = 0; let _lastAudioRaceLog = 0; const WS_CRASH_LOG_COOLDOWN = 30_000; @@ -1081,6 +1088,14 @@ process.on("uncaughtException", (err, origin) => { } return; } + if (isBenignStreamAbortRace(err)) { + const now = Date.now(); + if (now - _lastAudioRaceLog > WS_CRASH_LOG_COOLDOWN) { + _lastAudioRaceLog = now; + logger.warn("[Error_Handling] Suppressed benign stream abort race (aborted/ECONNRESET) — loadstream was destroyed mid-transfer."); + } + return; + } logger.error("[Error_Handling] Uncaught Exception/Catch"); logger.error("Error:", err, origin); process.exit(1); @@ -1088,6 +1103,7 @@ process.on("uncaughtException", (err, origin) => { process.on("uncaughtExceptionMonitor", (err, origin) => { if (isIgnorableWsCrash(err)) return; if (isBenignAudioStopRace(err)) return; + if (isBenignStreamAbortRace(err)) return; logger.error("[Error_Handling] Uncaught Exception/Catch (MONITOR)"); logger.error("Error:", err, origin); }); diff --git a/src/FluxerAudioBridge.mjs b/src/FluxerAudioBridge.mjs index c3fb09e..479a19a 100644 --- a/src/FluxerAudioBridge.mjs +++ b/src/FluxerAudioBridge.mjs @@ -45,6 +45,7 @@ export class FluxerAudioBridge extends EventEmitter { _stream = null; _encoder = null; _sourceStream = null; + _sourceReq = null; _playing = false; _stopped = false; _startedAt = 0; @@ -172,6 +173,7 @@ export class FluxerAudioBridge extends EventEmitter { const routed = await this._routeByMagic(loaded.stream); this._sourceStream = loaded.stream; + this._sourceReq = loaded.req || null; if (routed.kind === "webm") { logger.player("[AudioBridge] Playing loadstream webm/opus passthrough: " + (trackInfo.title || "unknown") + @@ -268,6 +270,7 @@ export class FluxerAudioBridge extends EventEmitter { const routed2 = await this._routeByMagic(loaded.stream); this._sourceStream = loaded.stream; + this._sourceReq = loaded.req || null; if (routed2.kind === "webm") { logger.player("[AudioBridge] Route 3c (Lavalink): webm/opus passthrough"); @@ -664,6 +667,9 @@ export class FluxerAudioBridge extends EventEmitter { }); }); + req.on("socket", (socket) => { + socket.on("error", () => {}); + }); req.on("error", reject); req.setTimeout(timeoutMs, () => { req.destroy(); @@ -677,7 +683,7 @@ export class FluxerAudioBridge extends EventEmitter { * @param {string} url * @param {object} [headers] * @param {number} [_redirectCount=0] - * @returns {Promise<{stream: Readable, inputFormat: string|null}>} + * @returns {Promise<{stream: Readable, inputFormat: string|null, req: http.ClientRequest}>} * @private */ _httpRequestStream(url, headers = {}, _redirectCount = 0) { @@ -739,7 +745,11 @@ export class FluxerAudioBridge extends EventEmitter { res.once("data", () => resetIdleTimeout()); logger.player("[AudioBridge] loadstream response: " + res.statusCode + " content-type=" + (res.headers["content-type"] || "?")); - resolve({ stream: res, inputFormat: res.headers["content-type"] || null }); + resolve({ stream: res, inputFormat: res.headers["content-type"] || null, req }); + }); + + req.on("socket", (socket) => { + socket.on("error", () => {}); }); req.on("error", (err) => { @@ -850,9 +860,13 @@ export class FluxerAudioBridge extends EventEmitter { try { s.destroy(); } catch (_) {} } } + if (this._sourceReq) { + try { this._sourceReq.destroy(); } catch (_) {} + } this._stream = null; this._encoder = null; this._sourceStream = null; + this._sourceReq = null; } /** Stop playback, release the voice connection, and remove all listeners. */