naught icon indicating copy to clipboard operation
naught copied to clipboard

Server listening on existing socket / file descriptor

Open ryandesign opened this issue 10 years ago • 3 comments

I have a server which listens on an existing socket:

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

This works fine if e.g. node server.js is called directly by the process that sets up that socket.

It also works fine with the cluster module, because although the socket is only available to the master process, all listen() invocations of the workers get passed to the master. So I merely have to have the master tell the workers what fd to listen on; I do that by passing them an environment variable when I fork them.

I'm unclear how to use naught with this kind of situation, since the socket would only be available to naught itself, and would not be available to my script which naught would launch.

ryandesign avatar Oct 28 '14 04:10 ryandesign

If I understand correctly, naught would need to pass the fd to the master when it spawns it in daemon.js:

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

That stdio array would get an additional value in it. For my case it would be something like:

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

Perhaps there could be an additional argument that could be given to naught start to tell it which additional fd(s) to share with the master.

ryandesign avatar Oct 28 '14 14:10 ryandesign

Hi @ryandesign, did you ever figure this out? I'm trying to use naught under systemd socket activation (which uses fd3 and up) and having the same issue.

jgrigg avatar Jun 18 '15 17:06 jgrigg

@jgrigg, I did implement this last year in a fork, for which I have now submitted a pull request.

ryandesign avatar Jun 19 '15 09:06 ryandesign