diff --git a/io/io_close.c b/io/io_close.c index f1dac4e..f728cd6 100644 --- a/io/io_close.c +++ b/io/io_close.c @@ -11,6 +11,8 @@ extern void io_dontwantread_really(int64 d,io_entry* e); extern void io_dontwantwrite_really(int64 d,io_entry* e); +long first_deferred=-1; + void io_close(int64 d) { io_entry* e; if ((e=iarray_get(&io_fds,d))) { @@ -27,6 +29,7 @@ void io_close(int64 d) { #endif e->mmapped=0; } +#if 1 if (e->next_read!=-1 || e->next_write!=-1) { /* There are still outstanding events. If we close the fd, between * now and when those events are handled, another accept() or @@ -35,10 +38,15 @@ void io_close(int64 d) { * So we don't actually close the fd now, but we will mark it as * closed. */ // fprintf(stderr,"io_close(%d) DEFERRED!\n",d); - e->closed=1; + if (e->closed==0) { + e->closed=1; + e->next_defer=first_deferred; + first_deferred=d; + } return; } else e->closed=0; +#endif } // fprintf(stderr,"io_close(%d)\n",d); close(d); diff --git a/io/io_waituntil2.c b/io/io_waituntil2.c index da38237..3c39f97 100644 --- a/io/io_waituntil2.c +++ b/io/io_waituntil2.c @@ -119,6 +119,19 @@ int64 io_waituntil2(int64 milliseconds) { struct pollfd* p; #endif long i,j,r; + if (first_deferred!=-1) { + while (first_deferred!=-1) { + io_entry* e=iarray_get(&io_fds,first_deferred); + if (e) { + if (e->closed) { + e->closed=0; + close(first_deferred); + } + first_deferred=e->next_defer; + } else + first_deferred=-1; // can't happen + } + } if (!io_wanted_fds) return 0; #ifdef HAVE_EPOLL if (io_waitmode==EPOLL) {