[zeromq-dev] Messages lost on fork()

Pete Hayes pete at hayes.id.au
Mon Jan 13 14:31:54 CET 2014


Hi all,

Apologies if this is a dumb question, but I am struggling to understand what’s going on here: I have a small REQ -> ROUTER script that forks, sends a message to the ROUTER and then exits. If I create all of the REQ sockets before the ROUTER socket it works fine, but if I do it the other way it doesn’t work.

To exemplify what I’m talking about, the below script first forks to create a REQ socket and then creates the ROUTER socket. The REQ socket sends “Send 0” to the ROUTER which is received correctly. Then the script forks again and attempts to send a new message via a REQ socket as before, but this message does not get through. FYI I’ve tried changing to TCP sockets too, in case IPC sockets didn’t like the order with which I connected them.

It appears to be something to do with forking process that throws it. If I remove the second create() fn call and instead create a new socket under the current context, it works fine. Thus Fork+REQ => ROUTER works, ROUTER => REQ works, but ROUTER => Fork+REQ does not. Hopefully that makes sense??

I’d really appreciate some help from the ZMQ boffins out there!

Cheers,

Pete.

---

My environment is: FreeBSD 9.2, PHP 5.5.7, ZeroMQ 4 (latest code from GitHub stable repo), PHP ZMQ binding (latest code from GitHub).

My output looks like this:

Create worker 1
Send 1
Create worker 2

The script I’m running from shell:

<?php
function create($id) {
    if (pcntl_fork() == 0) {
        echo "Create worker $id\n";

        $context = new ZMQContext();
        $worker = $context->getSocket(ZMQ::SOCKET_REQ);
        $worker->connect('ipc://sock.ipc');
        $worker->send("Send $id");

        exit;
    }
}

$id = 0;
create(++$id);

$context = new ZMQContext();
$backend = new ZMQSocket($context, ZMQ::SOCKET_ROUTER);
$backend->bind('ipc://sock.ipc');

while (true) {
    $backend->recv();
    $backend->recv();
    echo $backend->recv() . "\n";

    create(++$id);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20140113/ae37e231/attachment.htm>


More information about the zeromq-dev mailing list