Skip to content

Commit

Permalink
child_process: check for closed pipes after creating socket
Browse files Browse the repository at this point in the history
After the socket is created, we trigger an empty read to make sure we
catch stdio events like closing the stdin file descriptor. Otherwise
those events will go undetected until the next stream write.

Fixes: nodejs#25131
  • Loading branch information
seangoedecke committed May 18, 2021
1 parent 36bb31b commit e6f5a00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ ChildProcess.prototype.spawn = function(options) {
stream.socket = createSocket(this.pid !== 0 ?
stream.handle : null, i > 0);

// Trigger an empty read to check for a closed pipe
// or the close event will go undetected until next write
stream.socket.read(0);

if (i > 0 && this.pid !== 0) {
this._closesNeeded++;
stream.socket.on('close', () => {
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-child-process-spawn-close-stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
// Refs: https://github.com/nodejs/node/issues/25131
const common = require('../common');
const cp = require('child_process');
const fs = require('fs');

if (process.argv[2] === 'child') {
fs.closeSync(0);
} else {
const child = cp.spawn(process.execPath, [__filename, 'child'], {
stdio: ['pipe', 'inherit', 'inherit']
});

child.stdin.on('close', common.mustCall(() => {
process.exit(0);
}));

child.on('close', common.mustNotCall(() => {}));
}

0 comments on commit e6f5a00

Please sign in to comment.