Skip to content

Commit

Permalink
chore: Make sure we cleanup all event listeners from the spawned proc…
Browse files Browse the repository at this point in the history
…ess instance after it is closed
  • Loading branch information
mykola-mokhnach committed Jul 3, 2024
1 parent 47e1fa4 commit e95c3f5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends true ? TeenProcessExecBufferResult : TeenProcessExecStringResult} */({stdout, stderr, code}));
Expand Down
3 changes: 2 additions & 1 deletion lib/subprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
});
Expand Down

0 comments on commit e95c3f5

Please sign in to comment.