Repository navigation
child_process: fix handleless NODE_HANDLE handling - #13235
Conversation
|
BTW thanks to @cjihrig as he came up with a test case and helped debugging the issue. |
|
Removed the test as it doesn't look reliable at all. If somebody has a better idea for a test, it would be great. |
There was a problem hiding this comment.
hmm... I understand the reasoning behind this but I'm wondering if this is the right approach. If the handle is invalid at this point, should we treat it as an error condition?
There was a problem hiding this comment.
It's not necessarily an error. For example, connections can be closed by the client while they wait in a queue to be sent from the master to the worker. I think the correct thing to do is advocate that people check their received handles in the worker prior to using them (#13196).
There was a problem hiding this comment.
Yes, as far as I can tell there are two cases where the sending a handle ends up with no handle on reception:
- As Colin said, if the handle is closed while waiting in the sending queue. This case is detected by the sending process and instead of sending a
NODE_HANDLEmessage, it sends a normal ipc message. In this case, thesendHandleargument of themessagelistener callback is set to null. - When for some reason, the handle cannot be duped on the receiving process. This is the case this PR tries to fix and I think it makes sense that it behaves in the same way as the previous case (setting
sendHandletonull).
I think there's another argument not to treat it as an error. We can send data (the message argument) along with the handle that can be useful on reception even if there's no handle associated.
There was a problem hiding this comment.
Wouldn't a better approach be to send a "please resend the handle" message to the master instead of NODE_HANDLE_ACK?
There was a problem hiding this comment.
It makes sense. Do you have something in mind on how would that work on the sending process?
I think resending the message automatically can lead to problems if the receiving process constantly fails. Maybe using the process.send() callback could be a good idea. So if the message sent is a NODE_HANDLE wait for a NODE_HANDLE_ACK or NODE_HANDLE_NACK to call the callback.
There was a problem hiding this comment.
Right, I said "master" when I should have said "parent process."
You are right that unchecked repeated resending is no good. The cluster module could deal with it by sending the handle to another worker after a few turns but that isn't applicable to the child_process module.
I don't have a great solution in mind but as a strawman: try three times and close + log a warning after that? If it happens frequently enough and bothers users enough they will file bug reports about it and we can try to come up with something better.
We could cheat when the ChildProcess instance is managed by the cluster module and do the send-to-another-worker thing. It would be tight coupling but that doesn't bother me personally. Your call.
There was a problem hiding this comment.
Can you replace "It can happen the handle" with "It is possible that the handle"
a884a07 to
ed6e9fb
Compare
|
PR updated with @bnoordhuis suggestion. The new commit message would look: |
There was a problem hiding this comment.
This can be simplified:
if (target._pendingMessage) {
if (message.cmd === 'NODE_HANDLE_ACK') {
closePendingHandle(target);
} else if (target._pendingMessage.retransmissions++ ===
MAX_HANDLE_RETRANSMISSIONS) {
// If the handle didn't reach the receiving process correctly retry up
// to 3 times. If it still fails, close it and print a warning.
closePendingHandle(target);
process.emitWarning('Handle didn\'t reach the receiving process ' +
'correctly', 'SentHandleNotReceivedWarning');
}
}There was a problem hiding this comment.
You could shorten this to:
target._pendingMessage =
{ callback, message, handle, options, retransmissions: 0 };b4815fb to
1dda284
Compare
|
PR updated with comments from @BridgeAR and @bnoordhuis addressed. PTAL. Thanks! |
|
ping @bnoordhuis @cjihrig :) |
bnoordhuis
left a comment
There was a problem hiding this comment.
Sorry, I remember looking at this last week but I must've forgotten to actually submit my review.
LGTM and sorry for the delay!
There was a problem hiding this comment.
Can you put braces around the body for legibility?
cjihrig
left a comment
There was a problem hiding this comment.
Tests would be great, but LGTM.
There was a problem hiding this comment.
Tiny nit: could you change didn't to did not.
|
All nits addressed.
I agree, I tried something that worked for me locally but wasn't reliable at all. If anyone has suggestions would be great. |
It is possible that `recvmsg()` may return an error on ancillary data reception when receiving a `NODE_HANDLE` message (for example `MSG_CTRUNC`). This would end up, if the handle type was `net.Socket`, on a `message` event with a non null but invalid `sendHandle`. To improve the situation, send a `NODE_HANDLE_NACK` that'll cause the sending process to retransmit the message again. In case the same message is retransmitted 3 times without success, close the handle and print a warning. PR-URL: nodejs#13235 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
|
Landed in 71ca122. Thanks! |
It is possible that `recvmsg()` may return an error on ancillary data reception when receiving a `NODE_HANDLE` message (for example `MSG_CTRUNC`). This would end up, if the handle type was `net.Socket`, on a `message` event with a non null but invalid `sendHandle`. To improve the situation, send a `NODE_HANDLE_NACK` that'll cause the sending process to retransmit the message again. In case the same message is retransmitted 3 times without success, close the handle and print a warning. PR-URL: nodejs#13235 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
It is possible that `recvmsg()` may return an error on ancillary data reception when receiving a `NODE_HANDLE` message (for example `MSG_CTRUNC`). This would end up, if the handle type was `net.Socket`, on a `message` event with a non null but invalid `sendHandle`. To improve the situation, send a `NODE_HANDLE_NACK` that'll cause the sending process to retransmit the message again. In case the same message is retransmitted 3 times without success, close the handle and print a warning. PR-URL: #13235 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
It is possible that `recvmsg()` may return an error on ancillary data reception when receiving a `NODE_HANDLE` message (for example `MSG_CTRUNC`). This would end up, if the handle type was `net.Socket`, on a `message` event with a non null but invalid `sendHandle`. To improve the situation, send a `NODE_HANDLE_NACK` that'll cause the sending process to retransmit the message again. In case the same message is retransmitted 3 times without success, close the handle and print a warning. PR-URL: #13235 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
|
Landed on |
It is possible that `recvmsg()` may return an error on ancillary data reception when receiving a `NODE_HANDLE` message (for example `MSG_CTRUNC`). This would end up, if the handle type was `net.Socket`, on a `message` event with a non null but invalid `sendHandle`. To improve the situation, send a `NODE_HANDLE_NACK` that'll cause the sending process to retransmit the message again. In case the same message is retransmitted 3 times without success, close the handle and print a warning. Fixes: #9706 PR-URL: #13235 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
It is possible that `recvmsg()` may return an error on ancillary data reception when receiving a `NODE_HANDLE` message (for example `MSG_CTRUNC`). This would end up, if the handle type was `net.Socket`, on a `message` event with a non null but invalid `sendHandle`. To improve the situation, send a `NODE_HANDLE_NACK` that'll cause the sending process to retransmit the message again. In case the same message is retransmitted 3 times without success, close the handle and print a warning. Fixes: #9706 PR-URL: #13235 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
It is possible that `recvmsg()` may return an error on ancillary data reception when receiving a `NODE_HANDLE` message (for example `MSG_CTRUNC`). This would end up, if the handle type was `net.Socket`, on a `message` event with a non null but invalid `sendHandle`. To improve the situation, send a `NODE_HANDLE_NACK` that'll cause the sending process to retransmit the message again. In case the same message is retransmitted 3 times without success, close the handle and print a warning. Fixes: #9706 PR-URL: #13235 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
…acOS (#17) ### Context: Node.js IPC and macOS We implement Node.js's private IPC mechanism to send file descriptors across sockets when using the fork server. Node.js's IPC sends `NODE_HANDLE_ACK` before we're allowed to drop the file descriptor. This works around a macOS bug where the kernel may incorrectly close an in-flight file descriptor. That requires us to hold onto file descriptors after sending them over the socket until we get an `ACK` or `NACK`. ### Context: `NACK` In some rare cases (?) the IPC may not receive the file descriptor: nodejs/node#13235 Node.js's IPC may send `NODE_HANDLE_NACK`, which asks us to resend the last file descriptor. We don't currently handle this, and instead kill the worker and respond with a `SpawnFailed` message. ### libuv Issue There's a Linux-only (io_uring) bug with old versions of libuv (libuv/libuv#4323) that could lead to a race condition incorrectly marking the wrong worker as crashed. We trigger the libuv bug because the fork server may hold the file descriptor open to avoid the macOS bug. This happened in CI here: https://github.com/vercel-labs/next-safe-image-optimization/actions/runs/43261551087/job/132024282164 This PR closes the file descriptor earlier in the fork server on non-macOS platforms to avoid hitting the bug. This is not an issue on Node >=22.12.0. --- --- ## Full AI Summary The fork server kept its copy of each worker socket it sent until Node's NODE_HANDLE_ACK, and workers forked meanwhile inherited it. While another copy is open, closing the parent's doesn't remove it from the parent's epoll set, and libuv before 1.49 (Node.js 20) removes it only after closing it. So once the worker exited, the parent kept getting EPOLLHUP for that file descriptor, and delivered it as EOF to the next socket that reused it, failing that worker's request with ERR_WORKER_CRASHED. Linux keeps file descriptors in flight alive, so the copy is only needed on macOS. The libuv fix is in Node.js 22.12.0 (libuv 1.49.1) and 24.0.0 (libuv 1.50.0). No Node.js 20 release has it: the latest, 20.20.2, still has libuv 1.46.0, without a backport. Co-authored-by: Claude <noreply@anthropic.com>
It can happen that
recvmsg()may return an error on ancillary datareception when receiving a
NODE_HANDLEmessage (for exampleMSG_CTRUNC). This would end up, if the handle type wasnet.Socket,on a
messageevent with a non null but invalidsendHandle. Avoidthis by checking the
handlevalidity before performing thehandleConversion.Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
child_process