diff seobeo/os/s_macos_edge.c @ 16:fb2cff495a60

[Seobeo] Fixed the problem with edge server.
author June Park <parkjune1995@gmail.com>
date Fri, 03 Oct 2025 09:55:51 -0700
parents 114cad94008f
children d97ec3ded2ae
line wrap: on
line diff
--- a/seobeo/os/s_macos_edge.c	Fri Oct 03 06:50:33 2025 -0700
+++ b/seobeo/os/s_macos_edge.c	Fri Oct 03 09:55:51 2025 -0700
@@ -18,19 +18,21 @@
         struct kevent kev = {
           .ident  = cli->socket,
           .filter = EVFILT_READ,
-          .flags  = EV_ADD,
+          .flags  = EV_ADD | EV_ONESHOT,
           .udata  = cli
         };
         kevent(args->evfd, &kev, 1, NULL, 0, NULL);
       } else {
-        Seobeo_Web_HandleClientRequest(h, args->cache);
-        struct kevent kev = {
-          .ident  = h->socket,
-          .filter = EVFILT_READ,
-          .flags  = EV_DELETE,
-        };
-        kevent(args->evfd, &kev, 1, NULL, 0, NULL);
-        Seobeo_Handle_Destroy(h);
+        if (h != args->srv) {
+          struct kevent kev = {
+            .ident  = h->socket,
+            .filter = EVFILT_READ,
+            .flags  = EV_DELETE,
+          };
+          kevent(args->evfd, &kev, 1, NULL, 0, NULL); // Remove from kqueue first
+        
+          Seobeo_Web_HandleClientRequest(h, args->cache); // this frees 
+        }
       }
     }
   }
@@ -56,11 +58,12 @@
   pthread_attr_setstacksize(&attr, 100 * 1024 * 1024); // 100 MB
 
   pthread_t  threads[thread_count];
-  WorkerArgs args = { p_server_handle, p_html_cache, kq };
   for (int i = 0; i < thread_count; i++)
   {
+    WorkerArgs *args = malloc(sizeof(WorkerArgs));
+    *args = (WorkerArgs){ p_server_handle, p_html_cache, kq };
     pthread_create(&threads[i], NULL,
-                   Seobeo_Web_Edge_Worker, &args);
+                   Seobeo_Web_Edge_Worker, args);
   }
   for (int i = 0; i < thread_count; i++)
   {