Closed
Description
Affected URL(s)
https://nodejs.org/docs/latest-v16.x/api/child_process.html#subprocessstdout
Description of the problem
The
subprocess.stdout
property can be null if the child process could not be successfully spawned.
However, it can be undefined
in some cases:
$ docker run --rm -it node:16 /bin/bash
root@c12d92a299db:/# ulimit -n 17
root@c12d92a299db:/# node -e 'p=require("child_process").spawn("uname");p.stdout.on("data",console.log)'
[eval]:1
p=require("child_process").spawn("uname");p.stdout.on("data",console.log)
^
TypeError: Cannot read properties of undefined (reading 'on')
at [eval]:1:52
at Script.runInThisContext (node:vm:129:12)
at Object.runInThisContext (node:vm:305:38)
at node:internal/process/execution:76:19
at [eval]-wrapper:6:22
at evalScript (node:internal/process/execution:75:60)
at node:internal/main/eval_string:27:3
root@c12d92a299db:/# node -e 'console.log(require("child_process").spawn("uname"))'
<ref *1> ChildProcess {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
_closesNeeded: 1,
_closesGot: 0,
connected: false,
signalCode: null,
exitCode: null,
killed: false,
spawnfile: 'uname',
_handle: Process {
onexit: [Function (anonymous)],
[Symbol(owner_symbol)]: [Circular *1]
},
spawnargs: [ 'uname' ],
[Symbol(kCapture)]: false
}
node:events:505
throw er; // Unhandled 'error' event
^
Error: spawn uname EMFILE
at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
at onErrorNT (node:internal/child_process:478:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)
at onErrorNT (node:internal/child_process:478:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -24,
code: 'EMFILE',
syscall: 'spawn uname',
path: 'uname',
spawnargs: []
}
I think this should be documented.
Activity
cola119 commentedon Jul 20, 2022
This is because
ChildProcess.prototype.spawn
returns the error beforeChildProcess.stdout
is initialized tonull
at L457.node/lib/internal/child_process.js
Lines 388 to 401 in 3473d1b
Which is better to document it can be null and undefined, or to fix to initialize
ChildProcess.stdout
tonull
as an initial value at the top of the function?unarist commentedon Jul 21, 2022
I think it would be better if
stdout
has same value when the spawn can't create pipes, for consistency and maybe ease to handle.I'm okay for documenting the behavior, though.