diff --git a/src/core/main.c b/src/core/main.c index ca19972a9..68be809da 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -515,7 +515,7 @@ static int check_and_kill_existing_instance(const char *pid_file) { // Wait for graceful shutdown to complete before escalating to SIGKILL int timeout = SHUTDOWN_WATCHDOG_TIMEOUT_SECONDS + 30; bool still_running = true; - while (timeout-- > 0 && still_running) { + while (timeout-- > 0 && still_running && running) { if (used_pidfd) { struct pollfd pfd = { .fd = pidfd, .events = POLLIN }; int prc = poll(&pfd, 1, 1000); @@ -537,6 +537,13 @@ static int check_and_kill_existing_instance(const char *pid_file) { } } + if (!running) { + if (used_pidfd && pidfd >= 0) { + close(pidfd); + } + return -1; + } + // If still running, force kill if (timeout <= 0 && still_running && is_lightnvr_process(existing_pid)) { @@ -1368,12 +1375,14 @@ int main(int argc, char *argv[]) { // are still connecting. if (!atomic_load(&running) || atomic_load(&restart_requested)) { log_info("Shutdown/restart requested before detection stream startup; aborting startup loop"); + exit_code = EXIT_SUCCESS; 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"); + exit_code = EXIT_SUCCESS; goto cleanup; } @@ -2091,7 +2100,6 @@ int main(int argc, char *argv[]) { go2rtc_integration_cleanup(); #endif shutdown_onvif_discovery(); - cleanup_detection_resources(); // Shutdown database log_info("Shutting down database...");