From c08a57c030bc3110b433e1d889c6c5fd6877aba5 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Thu, 13 Nov 2025 09:19:20 -0800 Subject: [PATCH] [ScanDaemon] unlock pid file first before unlink socket Fix a potential issue that can cause new clang scan daemon failed to start when the previous daemon timed out at the same time. It is possible for the timed out daemon still hold pid file when being shutdown, when the new clang daemon is starting up and failed to lock the pid file. To the new clang daemon, this is indistinguishable from losing the race to start a new daemon with another clang invocation, thus the new starting request will be dropped and no daemon will be started. (cherry picked from commit 559509872887b185933e4bfa2da3e2fed8c99e53) --- clang/tools/driver/cc1depscan_main.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clang/tools/driver/cc1depscan_main.cpp b/clang/tools/driver/cc1depscan_main.cpp index 66034443f1caf..bc98974289c3c 100644 --- a/clang/tools/driver/cc1depscan_main.cpp +++ b/clang/tools/driver/cc1depscan_main.cpp @@ -659,6 +659,9 @@ struct ScanServer { /// jobs to finish. void shutdown() { ShutDown.store(true); + // Unlock pidfile first so another daemon can spin up when it can't find + // the socket. + ::flock(PidFD, LOCK_UN); cc1depscand::unlinkBoundSocket(BasePath); // Clean up the pidfile when we're done. if (PidFD != -1)