From 7a55a6e6b6c10f5033491603443c16f434d0deed Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 20 Dec 2023 17:22:11 +0000 Subject: [PATCH] ServerKiller: harden against spurious wakeups If awakened by spurious wakeup, the thread would immediately exit without doing anything, rendering it useless. Not sure how it took so long for this to be found... --- src/utils/ServerKiller.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/utils/ServerKiller.php b/src/utils/ServerKiller.php index 98129f48f..514a265ba 100644 --- a/src/utils/ServerKiller.php +++ b/src/utils/ServerKiller.php @@ -24,7 +24,8 @@ declare(strict_types=1); namespace pocketmine\utils; use pocketmine\thread\Thread; -use function time; +use function hrtime; +use function intdiv; class ServerKiller extends Thread{ private bool $stopped = false; @@ -34,13 +35,15 @@ class ServerKiller extends Thread{ ){} protected function onRun() : void{ - $start = time(); - $this->synchronized(function() : void{ - if(!$this->stopped){ - $this->wait($this->time * 1000000); + $start = hrtime(true); + $remaining = $this->time * 1_000_000; + $this->synchronized(function() use (&$remaining, $start) : void{ + while(!$this->stopped && $remaining > 0){ + $this->wait($remaining); + $remaining -= intdiv(hrtime(true) - $start, 1000); } }); - if(time() - $start >= $this->time){ + if($remaining <= 0){ echo "\nTook too long to stop, server was killed forcefully!\n"; @Process::kill(Process::pid()); }