From 9877a50407258b5cc90a57f2e4a0d8f9a5a66628 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 04:20:06 +0000 Subject: [PATCH 1/2] Stop heartbeat reads alerting staff when the game rewrites serverdata.txt The game deletes and rewrites serverdata.txt, so the file can vanish between the heartbeat's existence check and its read. The global error handler reported that @-silenced warning to staff as a "Fatal error". - ServerHeartbeat::read() keeps the read's warning local and treats a file that vanished mid-read as missing (the 120 s missing window). - bot.php: the global error handler honors @, checked before the async reporter because a fiber does not inherit the caller's @ state. Real errors stay in error_reporting() under @ and are still reported. - tests/local/heartbeat.php: stream-wrapper race fixture that fails on the old reader. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016WTP2UD3jGNHLyTAzYH8ro --- bot.php | 12 +++++++++- src/Civ13/ServerHeartbeat.php | 19 +++++++++++++++- tests/local/heartbeat.php | 42 +++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/bot.php b/bot.php index 46dad3f4..d91b8a91 100644 --- a/bot.php +++ b/bot.php @@ -603,7 +603,17 @@ function loadEnv(string $filePath, bool $explicit = false): array $civ13->sendMessage($channel, $civ13::createBuilder()->setContent($msg)->addFileFromContent('error.txt', $msg.PHP_EOL.$backtrace)); } }); -set_error_handler($global_error_handler); +// Honor the @ operator: a silenced warning (for example a file the game deleted between an +// existence check and the read) is already handled by its caller and is not a staff alert. +// PHP 8 keeps real errors in error_reporting() under @, so those are still reported. Check it +// here, not inside the async handler: a fiber does not inherit the caller's @ state. +set_error_handler(static function (int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) use ($global_error_handler): bool { + if (error_reporting() & $errno) { + $global_error_handler($errno, $errstr, $errfile, $errline); + } + + return true; +}); use React\Socket\SocketServer; use React\Http\HttpServer; diff --git a/src/Civ13/ServerHeartbeat.php b/src/Civ13/ServerHeartbeat.php index 2bb7b63a..a06a5a72 100644 --- a/src/Civ13/ServerHeartbeat.php +++ b/src/Civ13/ServerHeartbeat.php @@ -109,6 +109,23 @@ private function read(): string|false|null return false; } - return @file_get_contents($this->path, false, null, 0, 65537); + // The game deletes and rewrites the file, so it can vanish or be locked between the checks + // above and the read. Keep that warning away from the bot's global error handler (which + // reports even @-silenced warnings to staff) and treat a vanished file as a missing one. + set_error_handler(static fn (): bool => true); + try { + $data = file_get_contents($this->path, false, null, 0, 65537); + } catch (\Throwable) { + $data = false; + } finally { + restore_error_handler(); + } + if ($data === false) { + clearstatcache(true, $this->path); + + return file_exists($this->path) ? false : null; + } + + return $data; } } diff --git a/tests/local/heartbeat.php b/tests/local/heartbeat.php index bf40567c..cf2ab9bc 100644 --- a/tests/local/heartbeat.php +++ b/tests/local/heartbeat.php @@ -107,6 +107,48 @@ function record(string $realtime, string $players = '', bool $approved = false): if (is_file($root.'/serverdata.txt')) { unlink($root.'/serverdata.txt'); } rmdir($root); } +// The game deletes and rewrites serverdata.txt, so the file can vanish between the existence +// check and the read. The bot's global error handler reports even @-silenced warnings, so the +// read must neither raise a warning nor throw, and a vanished file must count as missing. +final class VanishingHeartbeatStream +{ + public static bool $present = true; + public static int $opens = 0; + public mixed $context = null; + + public function url_stat(string $path, int $flags): array|false + { + return self::$present ? ['mode' => 0100644, 'size' => 10] + array_fill(0, 13, 0) : false; + } + + public function stream_open(string $path, string $mode, int $options, ?string &$opened): bool + { + self::$opens++; + self::$present = false; // Deleted by the game's rewrite after the existence checks passed. + if ($options & STREAM_REPORT_ERRORS) { + trigger_error('Failed to open stream: No such file or directory', E_USER_WARNING); + } + + return false; + } +} +stream_wrapper_register('vanish', VanishingHeartbeatStream::class); +set_error_handler(static function (int $severity, string $message): never { + throw new RuntimeException('Heartbeat read leaked a warning to the global handler: '.$message); +}); +try { + $now = 0.0; + $vanishing = new ServerHeartbeat('vanish://region/serverdata.txt', $clock); + $status = $vanishing->poll(); + check(VanishingHeartbeatStream::$opens === 1, 'Race fixture must reach the read'); + check($status['state'] === 'unknown' && $status['reason'] === ServerHeartbeat::MALFORMED, 'A file that vanishes mid-read starts the missing window without an error'); + $now = ServerHeartbeat::STALE_SECONDS; + check($vanishing->poll()['reason'] === ServerHeartbeat::MISSING, 'A file that stays gone reads as missing, never as a fatal error'); +} finally { + restore_error_handler(); + stream_wrapper_unregister('vanish'); +} + foreach ([[0, '0s'], [45.9, '45s'], [60, '1m'], [25901, '7h 11m'], [3600, '1h 0m'], [93780, '1d 2h 3m'], [86400, '1d 0h 0m'], [-5, '0s']] as [$seconds, $expected]) { check(ServerHeartbeat::formatAge($seconds) === $expected, "Heartbeat age {$seconds}s must read {$expected}"); } From 19d786cf3c942ac0c4a522b950ed5975fc99e5f9 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 04:25:39 +0000 Subject: [PATCH 2/2] Record leaked heartbeat read warnings instead of throwing in the race fixture Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016WTP2UD3jGNHLyTAzYH8ro --- tests/local/heartbeat.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/local/heartbeat.php b/tests/local/heartbeat.php index cf2ab9bc..d460e504 100644 --- a/tests/local/heartbeat.php +++ b/tests/local/heartbeat.php @@ -133,8 +133,11 @@ public function stream_open(string $path, string $mode, int $options, ?string &$ } } stream_wrapper_register('vanish', VanishingHeartbeatStream::class); -set_error_handler(static function (int $severity, string $message): never { - throw new RuntimeException('Heartbeat read leaked a warning to the global handler: '.$message); +$leaked = []; +set_error_handler(static function (int $severity, string $message) use (&$leaked): bool { + $leaked[] = $message; // Record rather than throw: read() catches \Throwable and could hide it. + + return true; }); try { $now = 0.0; @@ -144,6 +147,7 @@ public function stream_open(string $path, string $mode, int $options, ?string &$ check($status['state'] === 'unknown' && $status['reason'] === ServerHeartbeat::MALFORMED, 'A file that vanishes mid-read starts the missing window without an error'); $now = ServerHeartbeat::STALE_SECONDS; check($vanishing->poll()['reason'] === ServerHeartbeat::MISSING, 'A file that stays gone reads as missing, never as a fatal error'); + check($leaked === [], 'Heartbeat read leaked a warning to the global handler: '.implode('; ', $leaked)); } finally { restore_error_handler(); stream_wrapper_unregister('vanish');