Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion src/Civ13/ServerHeartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
46 changes: 46 additions & 0 deletions tests/local/heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,52 @@ 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);
$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;
$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');
check($leaked === [], 'Heartbeat read leaked a warning to the global handler: '.implode('; ', $leaked));
} 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}");
}
Expand Down
Loading