Repository navigation
Description
This may be multiple issues in node or just discrepancies between what the docs suggest and how it actually operates.
When using the detached option to start a long-running process, ... If the parent's stdio is inherited, the child will remain attached to the controlling terminal.
conosle.log() causes early exit
This fails by exiting immediately (with success error code)
fail-on-spawn.js:
'use strict';
var spawn = require('child_process').spawn;
console.log('hello');
var child = spawn(
'ping'
, [ '-c', '3', 'google.com' ]
, { detached: true, stdio: 'inherit' }
);
child.unref();
process.on('exit', function () {
console.log('goodbye');
});node fail-on-spawn.js
hello
goodbye
echo $?
0Detaches and stdin is lost
If we change nothing but removing the initial console.log('hello') (or even just put it after spawn is called) then we get more desirable results.
fails-to-foreground.js:
'use strict';
var spawn = require('child_process').spawn;
// just comment that out there don't ya know
//console.log('hello');
var child = spawn(
'ping'
, [ '-c', '3', 'google.com' ]
, { detached: true, stdio: 'inherit' }
);
child.unref();
process.on('exit', function () {
console.log('goodbye');
});node fails-to-foreground.js
goodbye
aj@brunchfast ~> PING google.com (216.58.217.46): 56 data bytes
64 bytes from 216.58.217.46: icmp_seq=0 ttl=55 time=21.414 ms
64 bytes from 216.58.217.46: icmp_seq=1 ttl=55 time=21.091 ms
64 bytes from 216.58.217.46: icmp_seq=2 ttl=55 time=21.613 ms
--- google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 21.091/21.373/21.613/0.215 msAs you can see, as soon as node exits process.stdin goes back to bash rather than being handled by ping.
runs in foreground when not detached
(this works as documented, I'm using it as an example as how I expect the above to work)
From the description of the documentation, I would expect the user experience to be just like this: node has control, then gives control to the next program to be opened - except that in this example the child process is not detached and hence node cannot exit.
foreground-without-detach.js:
'use strict';
var fs = require('fs');
var child_process = require('child_process');
var file = '/tmp/node-editor-test';
fs.writeFile(file, "Node Editor", function () {
var child = child_process.spawn('vim', [file], {stdio: 'inherit'});
// would like to add detached: true and child.unref(), but that will kill this
});If we do try to detach it it fails dramatically since this program actually depends on stdin whereas ping does not.
More examples of unexpected(?) behavior
'use strict';
var fs = require('fs');
var child_process = require('child_process');
var file = '/tmp/node-editor-test';
fs.writeFile(file, "Node Editor", function () {
var child = child_process.spawn('vim', [file], {stdio: 'inherit'});
child.unref();
});(both die)
or
'use strict';
var fs = require('fs');
var child_process = require('child_process');
var file = '/tmp/node-editor-test';
fs.writeFile(file, "Node Editor", function () {
var child = child_process.spawn('vim', [file], { detached: true, stdio: 'inherit'});
process.exit(0);
});(this one above is neat because both vim and terminal get control of stdin and it creates all sorts of mess)
or
'use strict';
var fs = require('fs');
var child_process = require('child_process');
var file = '/tmp/node-editor-test';
fs.writeFile(file, "Node Editor", function () {
var child = child_process.spawn('vim', [file], { detached: true, stdio: 'inherit'});
child.unref();
});(both die)
- Version: v4.3.2 (also tried in v5.2 or so)
- Platform: Darwin brunchfast.local 13.0.0 Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64
- Subsystem: child_process
Activity
bnoordhuis commentedon Oct 12, 2016
Your friendly neighborhood bug triager checking in. For me, on OS X and Linux with nodejs/node@master, the first two examples behave the same: ping runs in the background. It matches the output from the second example.
The other examples basically do what I would expect them to do under the UNIX process and tty model. It may not be the behavior you want but they are not bugs in node.js.
The documentation has been rewritten since this issue was filed so I think we're good on that front. If you think it's still unclear, please file a new issue or pull request.
{ detached: true }translates to a setsid(2) system call. That creates a new session with no controlling terminal.coolaj86 commentedon Oct 12, 2016
Thanks Ben.