[zeromq-dev] random crashes on zmq_ctx_term() when there is a monitored socket
Peter Kleiweg
pkleiweg at xs4all.nl
Sat Nov 8 15:36:57 CET 2014
Pieter Hintjens schreef op de 8e dag van de slachtmaand van het jaar 2014:
> Indeed, this looks like two threads trying to work with the same socket.
>
> Can we reproduce this in C?
Here is the C version.
Tested with 0MQ versions 3.2.4 (using zmq_term instead of
zmq.ctx_term), 4.0.4, and 4.1.0.
Crashes in all tested versions, usually with "Assertion failed:
pfd.revents & POLLIN (signaler.cpp:193)" (line number for 0MQ
version 4.1.0), but sometimes with another message, and on some
occasions, it hangs.
#include <zmq.h>
#include <stdio.h>
char
buffer[1024];
int main ()
{
void
*context,
*socket;
int
i,
n;
for (n = 0; 1; n++) {
printf ("start new context\n");
context = zmq_ctx_new ();
if (! context) {
printf ("%s\n", zmq_strerror (zmq_errno ()));
return 1;
}
printf ("start new socket\n");
socket = zmq_socket (context, ZMQ_REQ);
if (! socket) {
printf ("%s\n", zmq_strerror (zmq_errno ()));
return 1;
}
sprintf (buffer, "inproc://monitor.rep%i", n);
i = zmq_socket_monitor (socket, buffer, ZMQ_EVENT_ALL);
if (i < 0) {
printf ("%s\n", zmq_strerror (zmq_errno ()));
return 1;
}
printf ("start connect\n");
i = zmq_connect (socket, "tcp://10.15.144.101:88661");
if (i < 0) {
printf ("%s\n", zmq_strerror (zmq_errno ()));
return 1;
}
printf ("start disconnect\n");
i = zmq_disconnect (socket, "tcp://10.15.144.101:88661");
if (i < 0) {
printf ("%s\n", zmq_strerror (zmq_errno ()));
return 1;
}
printf ("start close\n");
i = zmq_close (socket);
if (i < 0) {
printf ("%s\n", zmq_strerror (zmq_errno ()));
return 1;
}
printf ("start term\n");
i = zmq_ctx_term (context);
if (i < 0) {
printf ("%s\n", zmq_strerror (zmq_errno ()));
return 1;
}
}
return 0;
}
>
> On Sat, Nov 8, 2014 at 1:41 PM, Peter Kleiweg <pkleiweg at xs4all.nl> wrote:
> >
> > Does anyone know of a solution for this?
> > How do I close a monitor?
> >
> >
> > Peter Kleiweg schreef op de 31e dag van de wijnmaand van het jaar 2014:
> >
> >>
> >> Situation:
> >>
> >> 1. create a context
> >> 2. create socket
> >> 3. put monitor on socket with zmq_socket_monitor()
> >> 4. socket: connect to address
> >> 5. socket: remove connection
> >> 6. terminate the context
> >>
> >> Repeat this often enough, and either the program will hang at
> >> step 6, or crash with an error at step 6. The errors vary. These
> >> are the ones I have seen:
> >>
> >> Bad file descriptor (signaler.cpp:269)
> >> Assertion failed: pfd.revents & POLLIN (signaler.cpp:226)
> >> Invalid argument (mutex.hpp:99)
> >> Resource temporarily unavailable (signaler.cpp:269)
> >> Segmentation fault
> >>
> >> The problem disappears if you have a thread (or goroutine)
> >> actually reading the messages from the monitor, and if you
> >> insert a sleep (0.1 seconds) between steps 5 and 6 as well.
> >>
> >> Problem observed with ZeroMQ 3.2, 4.0 and 4.1, on linux/amd64,
> >> using the Go interfaces zmq3 and zmq4.
> >>
> >> Please see details and code examples here:
> >>
> >> https://github.com/pebbe/zmq4/issues/28
> >>
> >> My guess of what is going on is this: The monitor is running in
> >> its own thread, while zmq_ctx_term() tries to close the
> >> monitoring socket in the main thread. Thus, the socket would be
> >> used in two threads at once, and since sockets aren't
> >> thread-safe, a crash follows.
> >>
> >>
> >>
> >
> >
> >
> > --
> > Peter Kleiweg
> > http://pkleiweg.home.xs4all.nl/
> > _______________________________________________
> > zeromq-dev mailing list
> > zeromq-dev at lists.zeromq.org
> > http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
--
Peter Kleiweg
http://pkleiweg.home.xs4all.nl/
More information about the zeromq-dev
mailing list