From a15de3d263a5759ca4f07aa7a2b9e6d494551150 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 23 Mar 2026 11:40:11 +0100 Subject: Process management improvements (#881) This PR improves process lifecycle handling and resilience across several areas: - **Reclaim stale shared-memory entries instead of exiting** (`zenserver.cpp`): When a zenserver instance fails to attach as a sponsor to an existing process (e.g. because the PID was reused by an unrelated process), the server now clears the stale shared-memory entry and proceeds with normal startup instead of calling `std::exit(1)`. - **Wait for child process exit in `Kill()` and `Terminate()` on Unix** (`process.cpp`): After sending `SIGTERM` in `Kill()`, the code now waits up to 5s for graceful shutdown (escalating to `SIGKILL` on timeout), matching the Windows behavior. `Terminate()` also waits after `SIGKILL` so the child is properly reaped and doesn't linger as a zombie clogging up the process table. - **Fix sysctl buffer race in macOS `FindProcess`** (`process.cpp`): The macOS process enumeration now retries the `sysctl` call (up to 3 attempts with 25% buffer padding) to handle the race where the process list changes between the sizing call and the data-fetching call. Also flattens the nesting and fixes the guard/free scoping. - **Terminate stale processes before integration tests** (`zenserver-test.cpp`, `test.lua`): The integration test runner now accepts a `--kill-stale-processes` flag (passed automatically by `test.lua`) that scans for and terminates any leftover `zenserver`, `zenserver-test`, and `zentest-appstub` processes from previous test runs, logging the executable name and PID of each. This addresses flaky test failures caused by stale processes from prior runs holding ports or other resources. --- src/zencore/thread.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/zencore/thread.cpp') diff --git a/src/zencore/thread.cpp b/src/zencore/thread.cpp index f74791333..067e66c0d 100644 --- a/src/zencore/thread.cpp +++ b/src/zencore/thread.cpp @@ -503,6 +503,18 @@ NamedMutex::~NamedMutex() if (m_MutexHandle) { int Inner = int(intptr_t(m_MutexHandle)); + + // Remove the backing file before releasing the lock so that new callers + // of Create()/Exists() won't find a stale entry. Other processes that + // already have the file open still hold valid fds (unlink only removes + // the directory entry; the inode lives until the last fd is closed). + std::error_code Ec; + std::filesystem::path Name = PathFromHandle((void*)(intptr_t(Inner)), Ec); + if (!Ec) + { + unlink(Name.c_str()); + } + flock(Inner, LOCK_UN); close(Inner); } -- cgit v1.2.3