From 7e0719cc0a85790217c2027a4dd59903caa1489c Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 21 May 2025 13:09:30 -0400 Subject: [PATCH] fix(core): handle killing child processes of continuous tasks on Windows (#31296) This PR fixes an issue where running `nx e2e ` on Windows does not kill the underlying dev/preview server when the e2e task ends. Repro: 1. `npx create-nx-workspace@latest repro --preset=react-monorepo --e2eTestRunner=playwright --appName=demo` 2. `cd repro && npx nx e2e demo-e2e` This will leave the preview server running on port `4300`, and you have to `netstat -ano | findstr :4300` to find the PID and kill it. https://www.loom.com/share/fcbea53cdff543a98f4d4c8377027ee0 ## Current Behavior Continuous task does not kill the process correctly once discrete task ends. ## Expected Behavior Processes are killed correctly once task is done running. ## Related Issue(s) Fixes #31235 --- .../executors/run-commands/running-tasks.ts | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/packages/nx/src/executors/run-commands/running-tasks.ts b/packages/nx/src/executors/run-commands/running-tasks.ts index b3420c0e90..b755969328 100644 --- a/packages/nx/src/executors/run-commands/running-tasks.ts +++ b/packages/nx/src/executors/run-commands/running-tasks.ts @@ -359,21 +359,15 @@ class RunningNodeProcess implements RunningTask { kill(signal?: NodeJS.Signals): Promise { return new Promise((res, rej) => { - if (process.platform === 'win32') { - if (this.childProcess.kill(signal)) { - res(); + treeKill(this.childProcess.pid, signal, (err) => { + // On Windows, tree-kill (which uses taskkill) may fail when the process or its child process is already terminated. + // Ignore the errors, otherwise we will log them unnecessarily. + if (err && process.platform !== 'win32') { + rej(err); } else { - rej('Unable to kill process'); + res(); } - } else { - treeKill(this.childProcess.pid, signal, (err) => { - if (err) { - rej(err); - } else { - res(); - } - }); - } + }); }); }