0

I cannot reliably get a forked child process to send back a message to the parent that exceeds 219262 bytes.

The issue is only on Linux. In Windows, it works as expected. And this issue seems have been introduced between Node versions 1.0.1 and 1.0.2 - works fine on Node versions prior to 1.0.1 but not after.

(the maxBuffer option is not relevent for child_process.fork, it only applies to child_process.exec and child_process.execFile)

Below is the failing sample. Executing "node parent" on the command line will fail to output the child's "messageToParent" if it exceeds 219262 bytes on Linux.

parent.js is:

var cp = require('child_process');
var child = cp.fork('./child', [], {});

console.log('>>>PARENT ---> SENDING MESSAGE TO CHILD');
child.send({});

child.on('message', function(msg) {
  console.log('>>>PARENT ---> MESSAGE RECEIVED FROM CHILD = ' + JSON.stringify(msg));
});
child.on('error', function(err) {
  console.log('>>>PARENT ---> ERROR FROM CHILD. err = '+ err);
});
child.on('exit', function(code, signal) {
  console.log('>>>PARENT ---> EXIT FROM CHILD. code='+code+' signal = '+ signal);
});
child.on('close', function(code, signal) {
  console.log('>>>PARENT ---> CLOSE FROM CHILD. code='+code+' signal = '+signal);
});
child.on('disconnect', function() {
  console.log('>>>PARENT ---> DISCONNECT FROM CHILD');
});

child.js is

process.on('message', function(messageFromParent) {

  console.log('>>>>>>CHILD ---> RECEIVED MESSAGE FROM PARENT');
  var messageToParent = "It would be too long to post on stackoverflow, but if I make this string longer than 219262 bytes, it fails to return to the parent in Linux. There is no such issue in Windows";                                                                                                  
  var ret = process.send(messageToParent);
  console.log('>>>>>>CHILD ---> SENDING MESSAGE TO PARENT process.send returned ' + ret);
  process.exit(0);
});

process.on('uncaughtException', function(err) {
  process.send({ output: {ERROR:err} });
  process.exit(-1);
});

1 Answer 1

2

Posting an answer in case anyone else stumbles into this issue (https://github.com/nodejs/node/issues/36268)

The above child.js works perfectly in Node versions prior to 1.0.1 since child_process.fork() used to be synchronous. So "process.send(messageToParent)", followed by "process.exit(0)" will always return messageToParent to parent.js. In later versions of Node, however, process.send() is async. Therefore, the child must exit via process.exit() within a process.send callback, else a race condition is created between V8 javascript thread and IPC pipe.

Also - in Windows, the default IPC pipe buffer is large enough that the message is always returned to parent prior to child exiting. This is not the case in Linux. This explains why the above code works in Windows even with later versions of Node where process.send() is async.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.