From 071727ffef3d297605bea26099d35563081bd23a Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 26 Nov 2026 02:48:59 -0500 Subject: [PATCH 1/4] 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 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index 8b892bbff..327690995 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -2001,15 +2001,15 @@ int main(int argc, char *argv[]) { // Check if restart was requested if (restart_requested) { if (container_mode) { - // In container mode, just exit cleanly with success code - // The container orchestrator (Docker/Kubernetes) will restart the container - log_info("Restart requested in container mode - exiting cleanly for orchestrator restart"); + // In container mode, exit with a dedicated non-zero code so both + // always-restart and on-failure restart policies can restart us. + const int RESTART_EXIT_CODE = 75; + log_info("Restart requested in container mode - exiting with restart code for orchestrator restart"); shutdown_logger(); - // Exit with code 0 so the container orchestrator restarts us - // Note: In Kubernetes, the pod will be restarted by the deployment controller - // In Docker with --restart policy, Docker will restart the container - return EXIT_SUCCESS; + // Exit non-zero to reliably trigger restart under failure-based policies. + // (Docker: on-failure, Kubernetes: OnFailure) + return RESTART_EXIT_CODE; } else if (saved_argv != NULL) { // In native mode, use execv to restart the process log_info("Restart requested in native mode, re-executing LightNVR..."); From 05c34c508519800c62c19ca39ae708964f9152f3 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 26 Nov 2026 02:49:01 -0500 Subject: [PATCH 2/4] 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, 8 insertions(+), 2 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index 327690995..e2d80976a 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1581,7 +1581,10 @@ int main(int argc, char *argv[]) { } else if (saved_argv != NULL) { // Give the system a moment to release resources // The parent is now dead, wait for it to fully exit - usleep(2000000); // 2 seconds + struct timespec initial_delay = {2, 0}; // 2 seconds + while (nanosleep(&initial_delay, &initial_delay) == -1 && errno == EINTR) { + // Retry with remaining time if interrupted. + } // Wait for the parent to terminate before re-exec. Do not // probe a numeric PID after this child has been reparented. @@ -1590,7 +1593,10 @@ int main(int argc, char *argv[]) { // Parent is gone break; } - usleep(500000); // 500ms + struct timespec poll_delay = {0, 500000000L}; // 500ms + while (nanosleep(&poll_delay, &poll_delay) == -1 && errno == EINTR) { + // Retry with remaining time if interrupted. + } } // Get the executable path From 5a9640fb3f5bb4b266f1eee8b7d4b5b7fbd80825 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 26 Nov 2026 02:49:02 -0500 Subject: [PATCH 3/4] 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 | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index e2d80976a..aaf626cab 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #include #include @@ -125,6 +127,27 @@ _Atomic bool restart_requested = false; _Static_assert(ATOMIC_BOOL_LOCK_FREE == 2, "signal handlers require lock-free atomic bool"); +static int open_pidfd_for_pid(pid_t pid) { +#ifdef SYS_pidfd_open + return (int)syscall(SYS_pidfd_open, pid, 0); +#else + (void)pid; + errno = ENOSYS; + return -1; +#endif +} + +static int send_signal_via_pidfd(int pidfd, int sig) { +#ifdef SYS_pidfd_send_signal + return (int)syscall(SYS_pidfd_send_signal, pidfd, sig, NULL, 0); +#else + (void)pidfd; + (void)sig; + errno = ENOSYS; + return -1; +#endif +} + // Store original argc/argv for restart functionality static int saved_argc = 0; static char **saved_argv = NULL; @@ -433,8 +456,13 @@ static int check_and_kill_existing_instance(const char *pid_file) { // In a non-interactive environment, we can automatically kill it log_info("Attempting to terminate previous instance (PID: %d) with SIGTERM", existing_pid); - // Send SIGTERM to let it clean up - if (kill(existing_pid, SIGTERM) == 0) { + // Send SIGTERM to let it clean up. Prefer pidfd to avoid PID-reuse races. + int pidfd = open_pidfd_for_pid(existing_pid); + bool used_pidfd = (pidfd >= 0); + + int term_rc = used_pidfd ? send_signal_via_pidfd(pidfd, SIGTERM) + : kill(existing_pid, SIGTERM); + 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) { @@ -445,10 +473,18 @@ static int check_and_kill_existing_instance(const char *pid_file) { if (timeout <= 0 && kill(existing_pid, 0) == 0 && is_lightnvr_process(existing_pid)) { log_warn("Process didn't terminate gracefully within timeout, using SIGKILL"); - kill(existing_pid, SIGKILL); + if (used_pidfd) { + (void)send_signal_via_pidfd(pidfd, SIGKILL); + } else { + kill(existing_pid, SIGKILL); + } sleep(1); // Give it a moment } + if (used_pidfd) { + close(pidfd); + } + // Wait for PID file to be released timeout = 5; // 5 seconds while (timeout-- > 0) { @@ -483,6 +519,9 @@ static int check_and_kill_existing_instance(const char *pid_file) { log_error("Previous instance did not release PID file lock within timeout; refusing to remove PID file"); return -1; } else { + if (used_pidfd) { + close(pidfd); + } log_error("Failed to terminate previous instance: %s", strerror(errno)); return -1; } From 92f23e788e0c1620334d57e5a58a73ebbd97ce53 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 26 Nov 2026 03:32:09 -0500 Subject: [PATCH 4/4] Protect prior instance signaling and restart semantics --- src/core/main.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index aaf626cab..db02bbd51 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -459,9 +458,27 @@ static int check_and_kill_existing_instance(const char *pid_file) { // Send SIGTERM to let it clean up. Prefer pidfd to avoid PID-reuse races. int pidfd = open_pidfd_for_pid(existing_pid); bool used_pidfd = (pidfd >= 0); - - int term_rc = used_pidfd ? send_signal_via_pidfd(pidfd, SIGTERM) - : kill(existing_pid, SIGTERM); + if (used_pidfd && !is_lightnvr_process(existing_pid)) { + close(pidfd); + log_error("Previous instance identity changed before SIGTERM"); + return -1; + } + int term_rc = -1; + if (used_pidfd) { + term_rc = send_signal_via_pidfd(pidfd, SIGTERM); + if (term_rc != 0 && errno == ENOSYS) { + close(pidfd); + pidfd = -1; + used_pidfd = false; + } + } + if (!used_pidfd) { + if (!is_lightnvr_process(existing_pid)) { + log_error("Previous instance identity changed before SIGTERM"); + return -1; + } + term_rc = kill(existing_pid, SIGTERM); + } if (term_rc == 0) { // Wait for graceful shutdown to complete before escalating to SIGKILL int timeout = SHUTDOWN_WATCHDOG_TIMEOUT_SECONDS + 30; @@ -519,10 +536,11 @@ static int check_and_kill_existing_instance(const char *pid_file) { log_error("Previous instance did not release PID file lock within timeout; refusing to remove PID file"); return -1; } else { + int signal_error = errno; if (used_pidfd) { close(pidfd); } - log_error("Failed to terminate previous instance: %s", strerror(errno)); + log_error("Failed to terminate previous instance: %s", strerror(signal_error)); return -1; } } else if (errno == ESRCH) {