Browse Source

fix close sockpair repeatedly

ithewei 4 năm trước cách đây
mục cha
commit
9363712e6b
3 tập tin đã thay đổi với 20 bổ sung10 xóa
  1. 18 5
      event/hloop.c
  2. 1 0
      event/hloop.h
  3. 1 5
      event/nio.c

+ 18 - 5
event/hloop.c

@@ -283,6 +283,17 @@ static void hloop_cleanup(hloop_t* loop) {
         loop->pendings[i] = NULL;
     }
 
+    // sockpair
+    if (loop->sockpair[0] != -1 && loop->sockpair[1] != -1) {
+        if (!hio_exists(loop, loop->sockpair[0])) {
+            closesocket(loop->sockpair[0]);
+        }
+        if (!hio_exists(loop, loop->sockpair[1])) {
+            closesocket(loop->sockpair[1]);
+        }
+        loop->sockpair[0] = loop->sockpair[1] = -1;
+    }
+
     // ios
     printd("cleanup ios...\n");
     for (int i = 0; i < loop->ios.maxsize; ++i) {
@@ -326,11 +337,6 @@ static void hloop_cleanup(hloop_t* loop) {
 
     // custom_events
     hmutex_lock(&loop->custom_events_mutex);
-    if (loop->sockpair[0] != -1 && loop->sockpair[1] != -1) {
-        closesocket(loop->sockpair[0]);
-        closesocket(loop->sockpair[1]);
-        loop->sockpair[0] = loop->sockpair[1] = -1;
-    }
     event_queue_cleanup(&loop->custom_events);
     hmutex_unlock(&loop->custom_events_mutex);
     hmutex_destroy(&loop->custom_events_mutex);
@@ -641,6 +647,13 @@ void hio_attach(hloop_t* loop, hio_t* io) {
     }
 }
 
+bool hio_exists(hloop_t* loop, int fd) {
+    if (fd >= loop->ios.maxsize) {
+        return false;
+    }
+    return loop->ios.ptr[fd] != NULL;
+}
+
 int hio_add(hio_t* io, hio_cb cb, int events) {
     printd("hio_add fd=%d io->events=%d events=%d\n", io->fd, io->events, events);
 #ifdef OS_WIN

+ 1 - 0
event/hloop.h

@@ -225,6 +225,7 @@ void on_accpet(hio_t* io) {
  */
 HV_EXPORT void hio_detach(/*hloop_t* loop,*/ hio_t* io);
 HV_EXPORT void hio_attach(hloop_t* loop, hio_t* io);
+HV_EXPORT bool hio_exists(hloop_t* loop, int fd);
 
 // hio_t fields
 // NOTE: fd cannot be used as unique identifier, so we provide an id.

+ 1 - 5
event/nio.c

@@ -495,11 +495,7 @@ try_write:
                 hloop_post_event(io->loop, &ev);
             }
         }
-        if (io->write_cb) {
-            // printd("write_cb------\n");
-            io->write_cb(io, buf, nwrite);
-            // printd("write_cb======\n");
-        }
+        hio_write_cb(io, buf, nwrite);
 
         if (nwrite == len) {
             //goto write_done;