Dev User hace 1 mes
padre
commit
d047be7cee
Se han modificado 3 ficheros con 31 adiciones y 10 borrados
  1. 2 3
      .gitignore
  2. 18 4
      src/api/api_server.cpp
  3. 11 3
      src/tasks/tasks.cpp

+ 2 - 3
.gitignore

@@ -5,8 +5,7 @@
 /out/build/x64-Debug
 /.vs
 /build
-*.o
-*.a
-ngs
 data/database.db
 data/metrics.db
+data/log
+

+ 18 - 4
src/api/api_server.cpp

@@ -46,7 +46,18 @@ using ylib::network::http::websocket_message;
 std::atomic<bool> g_running{true};
 ylib::network::http::center* g_center = nullptr;
 
-void on_signal(int) { g_running = false; }
+void on_signal(int) {
+    g_running.store(false, std::memory_order_relaxed);
+}
+
+void install_signal_handlers() {
+    struct sigaction sa {};
+    sa.sa_handler = on_signal;
+    sigemptyset(&sa.sa_mask);
+    sa.sa_flags = 0;  // interrupt blocking sleeps (do not set SA_RESTART)
+    sigaction(SIGINT, &sa, nullptr);
+    sigaction(SIGTERM, &sa, nullptr);
+}
 
 ylib::json parse_body(request* req) {
     if (!req) {
@@ -1571,8 +1582,7 @@ bool run(const std::string& listen_addr, uint16_t listen_port) {
     log_info("ngs apiserver starting listen=" + listen_addr + ":" +
              std::to_string(listen_port));
 
-    std::signal(SIGINT, on_signal);
-    std::signal(SIGTERM, on_signal);
+    install_signal_handlers();
 
     auto* center = new ylib::network::http::center();
     g_center = center;
@@ -1620,15 +1630,19 @@ bool run(const std::string& listen_addr, uint16_t listen_port) {
         return false;
     }
 
+    // HPSocket may touch signal state during start; reinstall our handlers.
+    install_signal_handlers();
+
     std::cout << "NGS API Server listening on http://" << host.domain << ":"
               << listen_port << "\n";
     std::cout << "Panel: http://127.0.0.1:" << listen_port << "/\n";
     std::cout << "Panel dir: " << panel_www_dir() << "\n";
     std::cout << "Docs: API.md\n";
+    std::cout.flush();
     log_info("ngs apiserver listening port=" + std::to_string(listen_port) +
              " panel=" + panel_www_dir());
 
-    while (g_running) {
+    while (g_running.load(std::memory_order_relaxed)) {
         std::this_thread::sleep_for(std::chrono::milliseconds(200));
     }
 

+ 11 - 3
src/tasks/tasks.cpp

@@ -383,8 +383,12 @@ void stop() {
         delete worker();
         worker() = nullptr;
     }
-    std::lock_guard<std::mutex> lock(mu());
-    persist_unlocked();
+    {
+        // Must not call log_* while holding mu(): log_write -> on_log_line
+        // re-enters this mutex and deadlocks (CTRL+C hang).
+        std::lock_guard<std::mutex> lock(mu());
+        persist_unlocked();
+    }
     log_info("task manager stopped");
 }
 
@@ -513,7 +517,11 @@ void clear_finished() {
 }
 
 void on_log_line(const std::string& line) {
-    std::lock_guard<std::mutex> lock(mu());
+    // try_lock: logging may be invoked while a tasks API already holds mu().
+    std::unique_lock<std::mutex> lock(mu(), std::try_to_lock);
+    if (!lock.owns_lock()) {
+        return;
+    }
     if (current_id().empty()) {
         return;
     }