Passing file descriptors between parent and child process

384 views
Skip to first unread message

Ryan Schmidt

unread,
Nov 2, 2014, 11:22:52 PM11/2/14
to nod...@googlegroups.com
Hi,

My server is started by another process that hands it a socket on file descriptor 11. I start my server this way:

http.createServer(app).listen({fd: 11});

Now I want to modify this to enable zero-downtime deployments, so I would like to have naught start my server. The question is: how can I get naught to pass fd 11 to my script so that I can listen on it? Or, how do I "connect" naught's fd 11 with my script's fd 11?

I have tried modifying naught's spawn invocation so that it reads:

var stdio = [process.stdin, stdoutValue, stderrValue, 'ipc'];
stdio[11] = 11;
master = spawn(process.execPath, nodeArgs.concat([path.join(__dirname, "master.js"), workerCount, script]).concat(argv), {
env: process.env,
stdio: stdio,
cwd: process.cwd(),
});

Basically I have added "stdio[11] = 11;" However this doesn't seem to be working. My server is failing to listen, getting the error "Error: listen ENOTSOCK". The node documentation says a positive integer in the stdio array "is interpreted as a file descriptor that is is currently open in the parent process. It is shared with the child process". I'm wondering if the problem is that the file descriptor, though available to my parent process, is not open yet. If so, how should I open it? Just fs.open?

Is there some way for naught (or any process) to know which fds are being made available to it, and pass them on to the child without needing to be told what they are? Is it possible for a process to automatically pass all fds to a child without having to enumerate them?


I have also filed this issue as a naught ticket but haven't heard back yet:

https://github.com/andrewrk/naught/issues/65

Ryan Schmidt

unread,
Nov 4, 2014, 11:02:21 PM11/4/14
to nod...@googlegroups.com
Does anyone have any thoughts on the below?

Or is there a better zero-downtime deployment module I should use instead of naught that has already solved this problem of how to pass along a socket on a file descriptor?

Ryan Graham

unread,
Nov 7, 2014, 2:29:05 PM11/7/14
to nodejs
FWIW, I can't see anything wrong with your logic.

Modifying naught to pass fd 11 to your app's cluster master looks like it should allow the cluster workers to listen with {fd: 11} and have that bubble up to the master.

~Ryan

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/1ABA4CB3-BD6B-4B05-8C1C-D22CFA66CBCF%40ryandesign.com.
For more options, visit https://groups.google.com/d/optout.



--

Tim Kuijsten

unread,
Nov 7, 2014, 2:29:40 PM11/7/14
to nod...@googlegroups.com
Have you tried wrapping it in a socket using new Socket({ fd: 11 })?

-Tim

Ryan Schmidt schreef op 05-11-14 om 04:18:

Ryan Schmidt

unread,
Nov 8, 2014, 6:42:36 PM11/8/14
to nod...@googlegroups.com
Thanks for confirming that. Now that I've double checked everything, it does in fact work; it was my error.

The first time I tested, I had another process in between naught and the one that creates the socket. I believed that the file descriptors would be passed along, but they weren't.

In my second test in which I directly called naught from the process that creates the socket, the file descriptor number had changed, so although fd 11 was being passed through, that's not where the socket was.

I'll follow up with the developer of naught on a robust solution.
Reply all
Reply to author
Forward
0 new messages