From e95c3f536e7407e7ade6933a62880e4c3cf772c5 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Wed, 3 Jul 2024 09:15:05 +0200 Subject: [PATCH] chore: Make sure we cleanup all event listeners from the spawned process instance after it is closed --- lib/exec.js | 3 ++- lib/subprocess.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/exec.js b/lib/exec.js index 86aaeac..9329e95 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -99,10 +99,11 @@ async function exec (cmd, args = [], originalOpts = /** @type {T} */({})) { // if the process ends, either resolve or reject the promise based on the // exit code of the process. either way, attach stdout, stderr, and code. // Also clean up the timer if it exists - proc.on('close', (code) => { + proc.once('close', (code) => { if (timer) { clearTimeout(timer); } + proc.removeAllListeners(); const {stdout, stderr} = getStdio(isBuffer); if (code === 0) { resolve(/** @type {BufferProp extends true ? TeenProcessExecBufferResult : TeenProcessExecStringResult} */({stdout, stderr, code})); diff --git a/lib/subprocess.js b/lib/subprocess.js index a266e10..133204f 100644 --- a/lib/subprocess.js +++ b/lib/subprocess.js @@ -197,7 +197,7 @@ class SubProcess extends EventEmitter { // when the proc exits, we might still have a buffer of lines we were // waiting on more chunks to complete. Go ahead and emit those, then // re-emit the exit so a listener can handle the possibly-unexpected exit - this.proc.on('exit', (code, signal) => { + this.proc.once('exit', (code, signal) => { this.emit('exit', code, signal); // in addition to the bare exit event, also emit one of three other @@ -213,6 +213,7 @@ class SubProcess extends EventEmitter { // finally clean up the proc and make sure to reset our exit // expectations + this.proc?.removeAllListeners(); this.proc = null; this.expectingExit = false; });