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

Jon Dyte jon at totient.co.uk
Wed Oct 10 20:03:18 CEST 2012


On 10/10/12 09:52, John Khvatov wrote:
> 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?
>

It's not going to safe to call any zmq functions in the child after 
fork  on zmq objects that were started/created in the parent,
This is because there are also additional internal zmq threads running 
in the parent which will not be running in the child and which also
take part in the shutdown.

jon



More information about the zeromq-dev mailing list