help icon indicating copy to clipboard operation
help copied to clipboard

I want to listen on the parent process and accept on the forked child process, how could I do that?

Open imzyxwvu opened this issue 8 years ago • 5 comments

Normally I cloud listen, then fork, the accept. But if I try to use the fd inherited from the parent process which is already listening with uv_tcp_open and uv_listen, listen would be called many times.

imzyxwvu avatar Jul 15 '17 16:07 imzyxwvu

Can you come up with an example? It is really hard to understand what you exactly mean/want from 2 simple sentences

txdv avatar Jul 24 '17 06:07 txdv

What I want to do is:

int main() { int fd = socket(AF_INET, SOCK_STREAM, ...); bind(fd); listen(fd, ...); int pid = fork(); // this should be done with uv_spawn if(pid == 0) { while(1) { fd = accept(); handle(fd); } } }

imzyxwvu avatar Oct 14 '17 15:10 imzyxwvu

I assume this is not possible since uv_spawn() is generally fork() and then execve() right away.

You could try to inherit the listening socket in a child process via UV_INHERIT_STREAM though, but I'm sure it's legal.

neoxic avatar Nov 10 '18 04:11 neoxic

The libuv way of doing things is not to share the same sockets but rather send them around over pipes. In your case, a master process can listen and accept sockets and then pass them over to a child process via an IPC pipe that you set up via uv_spawn().

neoxic avatar Nov 10 '18 05:11 neoxic

Correct. That's how the cluster module in Node.js works.

bnoordhuis avatar Nov 10 '18 11:11 bnoordhuis