Skip to content

Commit

Permalink
fix(stdlib): prevent spawned zombie processes
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkdev98 committed Aug 29, 2023
1 parent 896a29f commit e30cbbd
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/stdlib/src/node.js
Expand Up @@ -63,8 +63,22 @@ export function spawn(command, args, opts = {}) {
return new Promise((resolve, reject) => {
const sp = cpSpawn(command, args, { stdio: "inherit", ...opts });

sp.once("error", reject);
const exitHandler = (signal) => {
sp.kill(signal);
};

process.once("exit", exitHandler);

sp.once("error", (...args) => {
process.removeListener("exit", exitHandler);

// eslint-disable-next-line prefer-promise-reject-errors
return reject(...args);
});

sp.once("exit", (code) => {
process.removeListener("exit", exitHandler);

resolve({ exitCode: code ?? 0 });
});
});
Expand Down

0 comments on commit e30cbbd

Please sign in to comment.