簡體   English   中英

執行 child_process.fork 時 Linux 中的 NodeJS 錯誤?

[英]NodeJS bug in Linux when executing child_process.fork?

我無法可靠地讓分叉子進程向父進程發送超過 219262 字節的消息。

該問題僅在Linux上。 在 Windows 中,它按預期工作。 這個問題似乎是在 Node 版本 1.0.11.0.2 之間引入的——在 1.0.1 之前的 Node 版本上運行良好,但在之后的版本就不行了。

maxBuffer選項與 child_process.fork 無關,僅適用於 child_process.exec 和 child_process.execFile)

下面是失敗的樣本。 如果在 Linux 上超過 219262 字節,則在命令行上執行“節點父節點”將無法 output 子節點的“messageToParent”

parent.js 是:

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 是

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);
});

發布答案以防其他人偶然發現此問題( https://github.com/nodejs/node/issues/36268

上面的 child.js 在 1.0.1 之前的 Node 版本中可以完美運行,因為child_process.fork( ) 曾經是同步的。 所以"process.send(messageToParent)"后跟"process.exit(0)"將始終將messageToParent返回給 parent.js。 然而,在更高版本的 Node 中, process.send()是異步的。 因此,子進程必須在 process.send 回調中通過process.exit()退出,否則在 V8 javascript 線程和 IPC pipe 之間會產生競爭條件。

此外 - 在 Windows 中,默認 IPC pipe 緩沖區足夠大,以至於消息總是在子退出之前返回給父級。 在 Linux 中情況並非如此。 這解釋了為什么上述代碼在 Windows 中有效,即使在更高版本的 Node 中 process.send() 是異步的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM