From 3154ed2455edb5061d79674104bb29e9cac64b8e Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 26 Nov 2026 03:46:20 -0500 Subject: [PATCH 1/3] Apply suggested fix to src/core/main.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/core/main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/core/main.c b/src/core/main.c index db02bbd51..30ce3e73d 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1315,7 +1315,17 @@ int main(int argc, char *argv[]) { // Start detection-based streams after the web server is listening so the UI // can come up promptly and show stream-starting placeholders while cameras // are still connecting. + if (!atomic_load(&running) || atomic_load(&restart_requested)) { + log_info("Shutdown/restart requested before detection stream startup; aborting startup loop"); + goto cleanup; + } + for (int i = 0; i < g_config.max_streams; i++) { + if (!atomic_load(&running) || atomic_load(&restart_requested)) { + log_info("Shutdown/restart requested during detection stream startup; aborting remaining stream startup"); + goto cleanup; + } + if (config.streams[i].name[0] != '\0' && config.streams[i].enabled && config.streams[i].detection_based_recording && is_detection_recording_scheduled(&config.streams[i])) { From f36dc47626c82b33ce3758bba07aad02eefee683 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 26 Nov 2026 03:46:22 -0500 Subject: [PATCH 2/3] Apply suggested fix to src/core/main.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/core/main.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index 30ce3e73d..3865c83a6 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "core/version.h" #include "core/config.h" @@ -126,6 +127,37 @@ _Atomic bool restart_requested = false; _Static_assert(ATOMIC_BOOL_LOCK_FREE == 2, "signal handlers require lock-free atomic bool"); +static bool is_process_alive_or_non_zombie(pid_t pid) { + if (kill(pid, 0) != 0) { + return false; + } + + char stat_path[64]; + snprintf(stat_path, sizeof(stat_path), "/proc/%ld/stat", (long)pid); + + FILE *fp = fopen(stat_path, "r"); + if (fp == NULL) { + // If procfs is unavailable or raced, keep legacy behavior. + return true; + } + + char buf[512]; + if (fgets(buf, sizeof(buf), fp) == NULL) { + fclose(fp); + return true; + } + fclose(fp); + + // /proc//stat format: pid (comm) state ... + // Find the closing ')' of comm, then read the following state char. + char *rp = strrchr(buf, ')'); + if (rp == NULL || rp[1] != ' ') { + return true; + } + char state = rp[2]; + return state != 'Z'; +} + static int open_pidfd_for_pid(pid_t pid) { #ifdef SYS_pidfd_open return (int)syscall(SYS_pidfd_open, pid, 0); @@ -482,12 +514,31 @@ static int check_and_kill_existing_instance(const char *pid_file) { if (term_rc == 0) { // Wait for graceful shutdown to complete before escalating to SIGKILL int timeout = SHUTDOWN_WATCHDOG_TIMEOUT_SECONDS + 30; - while (timeout-- > 0 && kill(existing_pid, 0) == 0) { - sleep(1); + bool still_running = true; + while (timeout-- > 0 && still_running) { + if (used_pidfd) { + struct pollfd pfd = { .fd = pidfd, .events = POLLIN }; + int prc = poll(&pfd, 1, 1000); + if (prc > 0) { + // pidfd became readable/hup/err => process exited. + still_running = false; + } else if (prc == 0) { + still_running = true; // 1s tick elapsed + } else { + // On poll failure, fall back to pid-based check. + still_running = is_process_alive_or_non_zombie(existing_pid); + sleep(1); + } + } else { + still_running = is_process_alive_or_non_zombie(existing_pid); + if (still_running) { + sleep(1); + } + } } // If still running, force kill - if (timeout <= 0 && kill(existing_pid, 0) == 0 && + if (timeout <= 0 && still_running && is_lightnvr_process(existing_pid)) { log_warn("Process didn't terminate gracefully within timeout, using SIGKILL"); if (used_pidfd) { From 7dc98479d8dd9171c46361e6958aeb0a6c1c9ae6 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 26 Nov 2026 03:50:19 -0500 Subject: [PATCH 3/3] Check prior process exit and abort interrupted detection startup --- src/core/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index 3865c83a6..ca19972a9 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -519,13 +519,13 @@ static int check_and_kill_existing_instance(const char *pid_file) { if (used_pidfd) { struct pollfd pfd = { .fd = pidfd, .events = POLLIN }; int prc = poll(&pfd, 1, 1000); - if (prc > 0) { - // pidfd became readable/hup/err => process exited. + if (prc > 0 && (pfd.revents & (POLLIN | POLLHUP))) { + // pidfd became readable: the original process exited. still_running = false; } else if (prc == 0) { still_running = true; // 1s tick elapsed } else { - // On poll failure, fall back to pid-based check. + // On poll failure, fall back to a process-state check. still_running = is_process_alive_or_non_zombie(existing_pid); sleep(1); }