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...
This commit is contained in:
Dylan K. Taylor 2023-12-20 17:22:11 +00:00
parent 90af8cfd69
commit 7a55a6e6b6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -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());
}