From 7487825fd3c4bfef0c515f5c8425d48d6bb9bef6 Mon Sep 17 00:00:00 2001 From: Amateur Audio Dude <168192910+AmateurAudioDude@users.noreply.github.com> Date: Sun, 7 Sep 2025 02:36:08 +1000 Subject: [PATCH] prevent listener stacking on error Fix error ```MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added to [process].``` --- server/stream/index.js | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/server/stream/index.js b/server/stream/index.js index d8bc45f..5bf121f 100644 --- a/server/stream/index.js +++ b/server/stream/index.js @@ -237,6 +237,17 @@ checkFFmpeg().then((ffmpegPath) => { commandDef.args.splice(commandDef.recArgs.indexOf('pipe:1'), 0, '-af', 'volume=2.5'); } + let currentRec = null; + + process.on('exit', () => { + if (currentRec) currentRec.kill('SIGINT'); + }); + + process.on('SIGINT', () => { + if (currentRec) currentRec.kill('SIGINT'); + process.exit(); + }); + function startRec() { if (!serverConfig.audio.ffmpeg) { // Spawn rec @@ -244,6 +255,7 @@ checkFFmpeg().then((ffmpegPath) => { //const rec = spawn(commandDef.command, { shell: true, stdio: ['ignore', 'pipe', 'pipe'] }); const rec = spawn('rec', commandDef.recArgs, { stdio: ['ignore', 'pipe', 'pipe'] }); + currentRec = rec; audioServer.waitUntilReady.then(() => { audioServer.Server.StdIn = rec.stdout; @@ -251,15 +263,6 @@ checkFFmpeg().then((ffmpegPath) => { connectMessage(`${consoleLogTitle} Connected rec \u2192 FFmpeg \u2192 Server.StdIn${serverConfig.audio.audioBoost && serverConfig.audio.ffmpeg ? ' (audio boost)' : ''}`); }); - process.on('exit', () => { - rec.kill('SIGINT'); - }); - - process.on('SIGINT', () => { - rec.kill('SIGINT'); - process.exit(); - }); - rec.stderr.on('data', (data) => { logFfmpeg(`[rec stderr]: ${data}`); }); @@ -317,6 +320,17 @@ checkFFmpeg().then((ffmpegPath) => { commandDef.args.splice(commandDef.args.indexOf('pipe:1'), 0, '-af', 'volume=2.5'); } + let currentArecord = null; + + process.on('exit', () => { + if (currentArecord) currentArecord.kill('SIGINT'); + }); + + process.on('SIGINT', () => { + if (currentArecord) currentArecord.kill('SIGINT'); + process.exit(); + }); + function startArecord() { if (!serverConfig.audio.ffmpeg) { // Spawn the arecord loop @@ -324,6 +338,7 @@ checkFFmpeg().then((ffmpegPath) => { //const arecord = spawn(commandDef.command, { shell: true, stdio: ['ignore', 'pipe', 'pipe'] }); const arecord = spawn('arecord', commandDef.arecordArgs, { stdio: ['ignore', 'pipe', 'pipe'] }); + currentArecord = arecord; audioServer.waitUntilReady.then(() => { audioServer.Server.StdIn = arecord.stdout; @@ -331,15 +346,6 @@ checkFFmpeg().then((ffmpegPath) => { connectMessage(`${consoleLogTitle} Connected arecord \u2192 FFmpeg \u2192 Server.StdIn${serverConfig.audio.audioBoost && serverConfig.audio.ffmpeg ? ' (audio boost)' : ''}`); }); - process.on('exit', () => { - arecord.kill('SIGINT'); - }); - - process.on('SIGINT', () => { - arecord.kill('SIGINT'); - process.exit(); - }); - arecord.stderr.on('data', (data) => { logFfmpeg(`[arecord stderr]: ${data}`); });