All pocketmine\thread\Thread now log uncaught exceptions and fatal errors by default

This commit is contained in:
Dylan K. Taylor 2023-08-08 14:56:54 +01:00
parent 2e58387a43
commit 2559d1719f
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 15 additions and 12 deletions

View File

@ -85,6 +85,7 @@ class RakLibServer extends Thread{
gc_enable(); gc_enable();
ini_set("display_errors", '1'); ini_set("display_errors", '1');
ini_set("display_startup_errors", '1'); ini_set("display_startup_errors", '1');
\GlobalLogger::set($this->logger);
$socket = new ServerSocket($this->address->deserialize()); $socket = new ServerSocket($this->address->deserialize());
$manager = new Server( $manager = new Server(
@ -107,11 +108,6 @@ class RakLibServer extends Thread{
$manager->waitShutdown(); $manager->waitShutdown();
} }
protected function onUncaughtException(\Throwable $e) : void{
parent::onUncaughtException($e);
$this->logger->logException($e);
}
public function getThreadName() : string{ public function getThreadName() : string{
return "RakLib"; return "RakLib";
} }

View File

@ -69,11 +69,6 @@ class AsyncWorker extends Worker{
$this->saveToThreadStore(self::TLS_KEY_NOTIFIER, $this->sleeperEntry->createNotifier()); $this->saveToThreadStore(self::TLS_KEY_NOTIFIER, $this->sleeperEntry->createNotifier());
} }
protected function onUncaughtException(\Throwable $e) : void{
parent::onUncaughtException($e);
$this->logger->logException($e);
}
public function getLogger() : ThreadSafeLogger{ public function getLogger() : ThreadSafeLogger{
return $this->logger; return $this->logger;
} }

View File

@ -115,6 +115,7 @@ trait CommonThreadPartsTrait{
protected function onUncaughtException(\Throwable $e) : void{ protected function onUncaughtException(\Throwable $e) : void{
$this->synchronized(function() use ($e) : void{ $this->synchronized(function() use ($e) : void{
$this->crashInfo = ThreadCrashInfo::fromThrowable($e, $this->getThreadName()); $this->crashInfo = ThreadCrashInfo::fromThrowable($e, $this->getThreadName());
\GlobalLogger::get()->logException($e);
}); });
} }
@ -128,11 +129,22 @@ trait CommonThreadPartsTrait{
$last = error_get_last(); $last = error_get_last();
if($last !== null){ if($last !== null){
//fatal error //fatal error
$this->crashInfo = ThreadCrashInfo::fromLastErrorInfo($last, $this->getThreadName()); $crashInfo = ThreadCrashInfo::fromLastErrorInfo($last, $this->getThreadName());
}else{ }else{
//probably misused exit() //probably misused exit()
$this->crashInfo = ThreadCrashInfo::fromThrowable(new \RuntimeException("Thread crashed without an error - perhaps exit() was called?"), $this->getThreadName()); $crashInfo = ThreadCrashInfo::fromThrowable(new \RuntimeException("Thread crashed without an error - perhaps exit() was called?"), $this->getThreadName());
} }
$this->crashInfo = $crashInfo;
$lines = [];
//mimic exception printed format
$lines[] = "Fatal error: " . $crashInfo->makePrettyMessage();
$lines[] = "--- Stack trace ---";
foreach($crashInfo->getTrace() as $frame){
$lines[] = " " . $frame->getPrintableFrame();
}
$lines[] = "--- End of fatal error information ---";
\GlobalLogger::get()->critical(implode("\n", $lines));
} }
}); });
} }