From 41efcbbc4ba4436d7140d278b1b56741a1f74105 Mon Sep 17 00:00:00 2001 From: jrd Date: Wed, 15 Apr 2026 22:31:57 +0000 Subject: [PATCH] Recover from stale temp file left by crashed writer If the PHP process writing new cache data crashes (e.g. connection reset by peer), register_shutdown_function may not run, leaving a zero-byte .tmp lock file behind. All subsequent requests then loop waiting for a cache update that never arrives, timing out after 20s (or sooner if the caller disconnects). Detect this by checking whether the .tmp file is older than 30 seconds (well past the ~1.5s a normal fetch takes) and removing it so the next waiter can take the lock and complete the fetch. Co-Authored-By: Claude Sonnet 4.6 --- servers.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/servers.php b/servers.php index 5a0d344..d36040e 100644 --- a/servers.php +++ b/servers.php @@ -103,6 +103,14 @@ function cleanup() { if ($tmp = @fopen($tmpfile, 'x')) break; // we have the temp file, so fetch new data + // If the temp file is stale (e.g. the writer crashed without cleanup), + // remove it and try again immediately + if (file_exists($tmpfile) && filemtime($tmpfile) < time() - 30) { + error_log("Stale lock file detected for $cachefile, removing"); + unlink($tmpfile); + continue; + } + if (time() > $start + 20) { // don't wait forever die("Can't obtain temporary file\n"); }