Skip to content

Commit 8a3d0c8

Browse files
committed
child_process: fix handle delivery
node.js and libuv depend on the fact that none of the supported systems ever emit more than one SCM_RIGHTS message from a recvmsg() syscall. SCM_RIGHTS messages are never coalesced. SCM_RIGHTS and normal messages however _are_ coalesced. That is, recvmsg() might return this: recvmsg(); // { "message-with-fd", "message", "message" } The operating system implicitly breaks pending messages along SCM_RIGHTS boundaries. Most Unices break before such messages but Linux also breaks _after_ them. When the sender looks like this: sendmsg("message"); sendmsg("message-with-fd"); sendmsg("message"); Then on most Unices the receiver sees messages arriving like this: recvmsg(); // { "message" } recvmsg(); // { "message-with-fd", "message" } The bug fix in commit 9352c19 assumes this behavior. On Linux however, those messages can also come in like this: recvmsg(); // { "message", "message-with-fd" } recvmsg(); // { "message" } In other words, it's incorrect to assume that the file descriptor is always attached to the first message. This commit makes node wise up. This is a back-port of commit 21bd456 from the v0.10 branch. The test has been dropped as it's not compatible with the v0.8 process model. Fixes #5330. Conflicts: lib/child_process.js
1 parent bf16141 commit 8a3d0c8

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/child_process.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,13 @@ function setupChannel(target, channel) {
292292
var json = jsonBuffer.slice(start, i);
293293
var message = JSON.parse(json);
294294

295-
handleMessage(target, message, recvHandle);
295+
// There will be at most one NODE_HANDLE message in every chunk we
296+
// read because SCM_RIGHTS messages don't get coalesced. Make sure
297+
// that we deliver the handle with the right message however.
298+
if (message && message.cmd === 'NODE_HANDLE')
299+
handleMessage(target, message, recvHandle);
300+
else
301+
handleMessage(target, message, undefined);
296302

297303
start = i + 1;
298304
}

0 commit comments

Comments
 (0)