Skip to content

Commit

Permalink
Explicitly kill process children on exit
Browse files Browse the repository at this point in the history
Summary:
when testing `yarn dev` I noticed sometimes `vite` processes would be left dangling, which would cause future `yarn dev` invocations to not run on port 3000 (it searchs up for an unused one), which breaks the main server command.

It seems we need to explicitly kill the subprocesses we spawned on exit. I was under the impression this was not necessary unless the process we spawn uses `detatched` (which we do not), but I still noticed this behavior in practice. Killing the children seems to fix this.

Reviewed By: nsblake

Differential Revision: D68807606

fbshipit-source-id: 5fa30f5272512b93b306d2c51480c4bc43c7e445
  • Loading branch information
evangrayk authored and facebook-github-bot committed Jan 29, 2025
1 parent 62325f1 commit 078e639
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion addons/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ class MultiRunner {
info.status = undefined;
void this.spawnProcess(i);
}

killAll() {
for (const info of this.infos) {
const handle = info.handle;
if (handle) {
handle.kill();
handle.catch(() => {});
}
}
}
}

async function main() {
Expand Down Expand Up @@ -371,6 +381,14 @@ async function main() {

const runner = new MultiRunner(configs);

const cleanupAndExit = () => {
runner.killAll();
process.exit(0);
};
process.on('SIGINT', cleanupAndExit);
process.on('SIGTERM', cleanupAndExit);
process.on('exit', cleanupAndExit);

if (launchDir != null && kind === 'browser') {
Promise.all([clientReady.promise, serverReady.promise]).then(() => {
onUserInput(input => {
Expand All @@ -379,7 +397,7 @@ async function main() {
}

if (input.toLowerCase() === 'q' || input.charCodeAt(0) === 3) {
process.exit(0);
cleanupAndExit();
}
});
});
Expand Down

0 comments on commit 078e639

Please sign in to comment.