[Libevent-users] Patch for signal handling

David Sze dsze at alumni.uwaterloo.ca
Thu Dec 14 12:31:35 EST 2006


Hi,

I have an application that repeatedly forks many short-lived child
processes and waits for SIGCHLD notification.  Frequently, the
application ends up blocking in the read() call in evsignal_cb().

I think there's a condition where more than one signal is received
and evsignal_handler() is called multiple times before evsignal_cb()
gets called.  When evsignal_cb() finally runs, it drains all the
notifications from the socketpair, so the subsequent calls to
evsignal_cb() block in read().

Attached is a patch that fixes the problem in my application, but
I'm not a libevent expert so corrections would be welcome.


-------------- next part --------------
--- libevent-1.2a/signal.c.orig	2006-10-27 23:28:57.000000000 -0400
+++ libevent-1.2a/signal.c	2006-12-14 11:19:27.000000000 -0500
@@ -71,7 +71,7 @@
 	ssize_t n;
 
 	n = read(fd, signals, sizeof(signals));
-	if (n == -1)
+	if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
 		event_err(1, "%s: read", __func__);
 	event_add(ev, NULL);
 }
@@ -102,6 +102,7 @@
 	FD_CLOSEONEXEC(ev_signal_pair[1]);
 
 	fcntl(ev_signal_pair[0], F_SETFL, O_NONBLOCK);
+	fcntl(ev_signal_pair[1], F_SETFL, O_NONBLOCK);
 
 	event_set(&ev_signal, ev_signal_pair[1], EV_READ,
 	    evsignal_cb, &ev_signal);


More information about the Libevent-users mailing list