]> git.meshlink.io Git - meshlink/commitdiff
Handle io_t's being deleted out of order while going through loop->ios.
authorGuus Sliepen <guus@meshlink.io>
Fri, 8 Aug 2014 09:59:39 +0000 (11:59 +0200)
committerGuus Sliepen <guus@meshlink.io>
Fri, 8 Aug 2014 09:59:39 +0000 (11:59 +0200)
src/event.c
src/event.h

index d1a0e2568ff1e9a928b89d510b996482f1ae55cb..b6def622023e0ade5f513af787225f1fba0c37f2 100644 (file)
@@ -81,6 +81,8 @@ void io_del(event_loop_t *loop, io_t *io) {
        if(!io->cb)
                return;
 
+       loop->deletion = true;
+
        io_set(loop, io, 0);
 
        splay_unlink_node(&loop->ios, &io->node);
@@ -115,6 +117,8 @@ void timeout_del(event_loop_t *loop, timeout_t *timeout) {
        if(!timeout->cb)
                return;
 
+       loop->deletion = true;
+
        splay_unlink_node(&loop->timeouts, &timeout->node);
        timeout->cb = 0;
        timeout->tv = (struct timeval){0, 0};
@@ -167,6 +171,8 @@ void signal_del(event_loop_t *loop, signal_t *sig) {
        if(!sig->cb)
                return;
 
+       loop->deletion = true;
+
        splay_unlink_node(&loop->signals, &sig->node);
        sig->cb = NULL;
 }
@@ -217,11 +223,21 @@ bool event_loop_run(event_loop_t *loop) {
                if(!n)
                        continue;
 
+               // Normally, splay_each allows the current node to be deleted. However,
+               // it can be that one io callback triggers the deletion of another io,
+               // so we have to detect this and break the loop.
+
+               loop->deletion = false;
+
                for splay_each(io_t, io, &loop->ios) {
-                       if(FD_ISSET(io->fd, &writable))
+                       if(FD_ISSET(io->fd, &writable) && io->cb)
                                io->cb(loop, io->data, IO_WRITE);
-                       else if(FD_ISSET(io->fd, &readable))
+                       if(loop->deletion)
+                               break;
+                       if(FD_ISSET(io->fd, &readable) && io->cb)
                                io->cb(loop, io->data, IO_READ);
+                       if(loop->deletion)
+                               break;
                }
        }
 
index c90e50679a171971bde932e51ed7f866d9ceae9e..6e77fbdf8ec30a0102793e214db4a2326997771a 100644 (file)
@@ -60,6 +60,7 @@ struct event_loop_t {
 
        volatile bool running;
        struct timeval now;
+       bool deletion;
        
        splay_tree_t ios;
        splay_tree_t timeouts;