changeset 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 2b9e75756825
children d97ec3ded2ae
files mrjunejune/main.c seobeo/os/s_linux_edge.c seobeo/os/s_macos_edge.c seobeo/s_linux_network.c seobeo/s_web.c seobeo/seobeo.h
diffstat 6 files changed, 46 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/mrjunejune/main.c	Fri Oct 03 06:50:33 2025 -0700
+++ b/mrjunejune/main.c	Fri Oct 03 09:55:51 2025 -0700
@@ -14,5 +14,5 @@
 
 int main(void)
 {
-  Seobeo_Web_StartBasicHTTPServer("mrjunejune/pages", "6969", SEOBEO_MODE_FORK, 2);
+  Seobeo_Web_StartBasicHTTPServer("mrjunejune/pages", "6969", SEOBEO_MODE_EDGE, 2);
 }
--- a/seobeo/os/s_linux_edge.c	Fri Oct 03 06:50:33 2025 -0700
+++ b/seobeo/os/s_linux_edge.c	Fri Oct 03 09:55:51 2025 -0700
@@ -23,11 +23,9 @@
         epoll_ctl(args->evfd, EPOLL_CTL_ADD,
                   cli->socket, &ev);
       } else {
-        // client ready
-        Seobeo_Web_HandleClientRequest(h, args->cache);
+        Seobeo_Web_HandleClientRequest(h, args->cache); // This frees
         epoll_ctl(args->evfd, EPOLL_CTL_DEL,
                   h->socket, NULL);
-        Seobeo_Handle_Destroy(h);
       }
     }
   }
--- 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++)
   {
--- a/seobeo/s_linux_network.c	Fri Oct 03 06:50:33 2025 -0700
+++ b/seobeo/s_linux_network.c	Fri Oct 03 09:55:51 2025 -0700
@@ -106,6 +106,7 @@
   p_handle->file = NULL;
   p_handle->text_copy = NULL;
   p_handle->file_name = NULL;
+  p_handle->destroyed = false;
 
   return p_handle;
 }
@@ -147,14 +148,23 @@
 
 void Seobeo_Handle_Destroy(Seobeo_PHandle p_handle)
 {
-  close(p_handle->socket);
-  free(p_handle->host);
-  free(p_handle->port);
-  free(p_handle->read_buffer);
-  free(p_handle->write_buffer);
-  free(p_handle->file);
-  free(p_handle->text_copy);
-  free(p_handle->file_name);
+  if (!p_handle) return;
+
+  bool expected = false;
+  if (!atomic_compare_exchange_strong(&p_handle->destroyed, &expected, true))
+  {
+    // Already destroyed by another thread
+    return;
+  }
+
+  if (p_handle->host) free(p_handle->host);
+  if (p_handle->port) free(p_handle->port);
+  if (p_handle->socket) close(p_handle->socket);
+  if (p_handle->read_buffer) free(p_handle->read_buffer);
+  if (p_handle->write_buffer) free(p_handle->write_buffer);
+  if (p_handle->text_copy) free(p_handle->text_copy);
+  if (p_handle->file_name) free(p_handle->file_name);
+
   free(p_handle);
 }
 
--- a/seobeo/s_web.c	Fri Oct 03 06:50:33 2025 -0700
+++ b/seobeo/s_web.c	Fri Oct 03 09:55:51 2025 -0700
@@ -139,9 +139,12 @@
   Seobeo_Handle_Flush(p_cli_handle);
 
 clean_up:
-  Seobeo_Handle_Destroy(p_cli_handle);
-  Dowa_Arena_Free(p_response_arena);
-  Dowa_HashMap_Free(p_req_map); // TODO: Maybe initilized hashmap within the Arena?  
+  if (p_cli_handle)
+    Seobeo_Handle_Destroy(p_cli_handle);
+  if (p_response_arena)
+    Dowa_Arena_Free(p_response_arena);
+  if (p_req_map)
+    Dowa_HashMap_Free(p_req_map); // TODO: Maybe initilized hashmap within the Arena?
 }
 
 int Seobeo_Web_ParseClientHeader(Seobeo_PHandle p_handle, Dowa_PHashMap map)
@@ -286,7 +289,8 @@
         Seobeo_Stream_Handle_Accept(p_server_handle);
       if (!cli) continue;
 
-      if (fork() == 0) {
+      if (fork() == 0)
+      {
         Seobeo_Web_HandleClientRequest(cli,
                                        p_html_cache);
         _exit(0);
--- a/seobeo/seobeo.h	Fri Oct 03 06:50:33 2025 -0700
+++ b/seobeo/seobeo.h	Fri Oct 03 09:55:51 2025 -0700
@@ -19,6 +19,8 @@
 #include <signal.h>
 #include <fcntl.h>
 #include <pthread.h>
+#include <stdatomic.h>
+#include <stdbool.h>
 
 #include "dowa/dowa.h"
 
@@ -52,6 +54,8 @@
   void    *file;
   void    *text_copy;
   char    *file_name;
+
+  atomic_bool destroyed;
 } Sebeo_Handle, *Seobeo_PHandle;
 
 typedef struct {