Description
Calling child_process.spawn()
with for example a non-existing path does not throw an error.
I believe that any synchronously available error happening during child_process.spawn()
should throw an error right away so that handling of common errors is straight-forward, encouraged, and not subject to race conditions. Exposing those errors synchronously that are known synchronously will make it easier for programmers to write NodeJS programs with robust child process management.
child_process.spawn()
runs the system calls required for creating a new process from an executable file in the context of the current event loop. This might block the event loop for a little bit. "Blocking the event loop for a little bit" might be considered as a downside (discussed here: #21234) but the upside is that errors could be handled synchronously! That's good, we only need to do it :-).
Naturally, there are some expected errors that can be thrown by the system call used for starting a process from an executable (exec()
-like calls on Unix), such as:
- the path provided to the executable is invalid:
ENOENT
on Linux - lack of privileges to run the executable:
EACCES
on Linux
NodeJS' child_process.spawn
uses uv_spawn()
under the hood which should indeed synchronously report about these errors, see see http://docs.libuv.org/en/v1.x/process.html#c.uv_spawn. Quotes:
If the process is successfully spawned, this function will return 0. Otherwise, the negative error code corresponding to the reason it couldn’t spawn is returned.
Possible reasons for failing to spawn would include (but not be limited to) the file to execute not existing, not having permissions to use the setuid or setgid specified, or not having enough memory to allocate for the new process.
The specific proposal is to throw an error immediately when uv_spawn()
fails.
Let me know what you think!
Related discussions: