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:
Dylan K. Taylor 2017-10-11 16:45:11 +01:00
parent 033cb8bd63
commit f9b1afe4cf
2 changed files with 24 additions and 0 deletions

View File

@ -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");

View File

@ -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(){