Repository navigation
Closed as not planned
Labels
child_processIssues and PRs related to the child_process subsystem.
Description
- Version: 15.8.0 (but also 12.21.0, and 16.0.0-pre)
- Platform: Linux madara-laptop-linux 5.4.0-65-generic The README is oriented towards Node #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
- Subsystem: child_process
What steps will reproduce the bug?
- Create a script that will run forever eg. keep-alive.js:
setInterval(() => {}, 10_000); - Create a script that will exec that file and abort it within a few seconds:
// keep-alive-test.js
const { exec } = require('child_process');
const ac = new AbortController();
const { signal } = ac;
const child = child_process.exec(`${process.execPath} ./keep-alive.js`, { signal ), console.log);
setTimeout(() => ac.abort(), 10_000);- Run
node keep-alive-test.js & - Examine running processes with
ps awwx | grep alive | grep -v grep | cat - You will note 3 running processes,
node keep-alive-test.js,sh -c node keep-alive.jsandnode keep-alive.js - After 10 seconds, the abort is invoked.
- Examine running processes again with
ps awwx | grep alive | grep -v grep | cat - You will note 2 running processes,
node keep-alive-test.jsandnode keep-alive.js(sh was terminated). - Both processes will live forever until explicitly terminated by the user (
fg-> CTRL+C, orkillall nodeetc)
How often does it reproduce? Is there a required condition?
Every time.
What is the expected behavior?
Expected behavior for ac.abort() was to abort the child process.
What do you see instead?
Child process does not get aborted, only the shell does.
Additional information
Internally, the abort handler for all child_process methods use child.kill() to terminate the underlying process. However, it is documented that this does not work "as expected" in Linux.
This makes ac.abort() quite useless on execed children, since none of them would actually get aborted at all.
This will probably happen if one uses child_process.spawn() to similarly spawn a shell to invoke a process, though I have not tested.
Metadata
Metadata
Assignees
Labels
child_processIssues and PRs related to the child_process subsystem.
Activity
MadaraUchiha commentedon Feb 25, 2021
cc @Linkgoron @benjamingr
RaisinTen commentedon Feb 26, 2021
@MadaraUchiha Is this the same problem as reported in #37273?
execcallsexecFilewhich callsspawninternally with thesignalhere:node/lib/child_process.js
Lines 254 to 263 in 5c3bc21
and that's being fixed in #37325.
benjamingr commentedon Feb 27, 2021
Fixed in #37273 - thanks for the report :)
benjamingr commentedon Feb 27, 2021
Apparently not 😅
MadaraUchiha commentedon Feb 28, 2021
@RaisinTen I don't see how.
ac.abort()still calls the underlyingchild.kill(), andchild.kill()does not work as expected in Linux, whenchildissh.In fact, the fact that @Linkgoron uses
sleepinstead of thealive-foreverfixture for theexectest indicates that they specifically want to use something that's long enough to not immediately terminate and affect the test, but short-lived enough as to not leak resources in the test.richardlau commentedon Jan 19, 2023
FTR I don't think this is limited to Linux. I've been able to reproduce this on our SmartOS CI instances while investigating nodejs/build#3154.
test: avoid left behind child processes
test: avoid left behind child processes
test: avoid left behind child processes
test: avoid left behind child processes
test: avoid left behind child processes
test: avoid left behind child processes
test: avoid left behind child processes
test: avoid left behind child processes
BlackGlory commentedon Jun 1, 2023
As I recall
execis just an advanced wrapper forspawn, so once you useshell: true, the problem exists.My current solution is to kill the whole process group via
process.kill(-pid)(you also needdetached: true):bnoordhuis commentedon Jun 2, 2023
I think this issue is a case of wrong expectations. Maybe the documentation can be clarified but I don't consider it a node bug, it's just how the UNIX process model works. Either avoid
shell: trueor use @BlackGlory's solution.I'm going to close this but if someone wants to send a documentation pull request, please do.