mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 00:33:59 +00:00
Fixed race condition causing exception log messages to sometimes not get written to disk when kill() is used
Synchronize with the logger thread when logging an exception or killing. This forces the main thread to wait for disk write to complete when logging exceptions or killing the process.
This commit is contained in:
parent
033cb8bd63
commit
f9b1afe4cf
@ -365,6 +365,10 @@ namespace pocketmine {
|
||||
}
|
||||
|
||||
function kill($pid){
|
||||
global $logger;
|
||||
if($logger instanceof MainLogger){
|
||||
$logger->syncFlushBuffer();
|
||||
}
|
||||
switch(Utils::getOS()){
|
||||
case "win":
|
||||
exec("taskkill.exe /F /PID " . ((int) $pid) . " > NUL");
|
||||
|
@ -39,6 +39,8 @@ class MainLogger extends \AttachableThreadedLogger{
|
||||
protected $logDebug;
|
||||
/** @var MainLogger */
|
||||
public static $logger = null;
|
||||
/** @var bool */
|
||||
private $syncFlush = false;
|
||||
|
||||
/**
|
||||
* @param string $logFile
|
||||
@ -162,6 +164,8 @@ class MainLogger extends \AttachableThreadedLogger{
|
||||
foreach(\pocketmine\getTrace(0, $trace) as $i => $line){
|
||||
$this->debug($line, true);
|
||||
}
|
||||
|
||||
$this->syncFlushBuffer();
|
||||
}
|
||||
|
||||
public function log($level, $message){
|
||||
@ -228,6 +232,17 @@ class MainLogger extends \AttachableThreadedLogger{
|
||||
$this->logStream[] = date("Y-m-d", $now) . " " . $cleanMessage . PHP_EOL;
|
||||
}
|
||||
|
||||
public function syncFlushBuffer(){
|
||||
$this->syncFlush = true;
|
||||
$this->synchronized(function(){
|
||||
$this->notify(); //write immediately
|
||||
|
||||
while($this->syncFlush){
|
||||
$this->wait(); //block until it's all been written to disk
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $logResource
|
||||
*/
|
||||
@ -236,6 +251,11 @@ class MainLogger extends \AttachableThreadedLogger{
|
||||
$chunk = $this->logStream->shift();
|
||||
fwrite($logResource, $chunk);
|
||||
}
|
||||
|
||||
if($this->syncFlush){
|
||||
$this->syncFlush = false;
|
||||
$this->notify(); //if this was due to a sync flush, tell the caller to stop waiting
|
||||
}
|
||||
}
|
||||
|
||||
public function run(){
|
||||
|
Loading…
x
Reference in New Issue
Block a user