[zeromq-dev] zmq_close() has no effect after fork()

John Khvatov ivaxer at gmail.com
Wed Oct 10 10:52:13 CEST 2012


On Oct 10, 2012, at 12:44 AM, Jon Dyte wrote:

> On 09/10/12 09:47, John Khvatov wrote:
>> Hello Bennie,
>> 
>> On Oct 9, 2012, at 11:13 AM, Bennie Kloosteman wrote:
>> 
>>> Why not create the context and socket after you fork ?
>> I don't want to use ZMQ in child process. I want to release all resources used by ZMQ before master exiting.
>> 
>> It needed for restart multi-worker application: old master exits, old workers still serve existing clients, new master runs and start new workers for new clients. It fails because new master can't bind to address which is still used after I call zmq_close()/zmq_term().
>> 
>> The question is how to release ZMQ resources (unbind TCP port) after fork()?
>> 
> 
> John
> 
> I think the child process needs to close all the file descriptors that 
> it does not need, though there would still be a race, if you were to 
> start a new master quickly enough.

Yep. But ZMQ does not allow to call zmq_close()/zmq_term() in child process. It is wrong:

        pid_t pid = fork();
        
        if (pid == 0) {
                zmq_close(socket);
                zmq_term(context);

                sleep(3600);
        }
        
        else {
                zmq_close(socket);
                zmq_term(context);
        }

Is there another way to close ZMQ socket in child?


> If you exec a new process rather than just fork, the problem may also be 
> alleviated.

I'll think about it. Thank you.

-- 
WBR, John Khvatov




More information about the zeromq-dev mailing list