MainLogger: fixed potential deadlock during syncFlushBuffer()

the notify() to flush the buffer might arrive in between the writeLogStream() and synchronized() calls in the thread body, resulting in a deadlock if the logger thread managed to call wait() before the main thread did.
This commit is contained in:
Dylan K. Taylor 2021-03-28 18:10:23 +01:00
parent b29f83ee99
commit 4abf4aecad
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -331,10 +331,11 @@ class MainLogger extends \AttachableThreadedLogger{
* @return void
*/
public function syncFlushBuffer(){
$this->syncFlush = true;
$this->synchronized(function() : void{
$this->syncFlush = true;
$this->notify(); //write immediately
});
$this->synchronized(function() : void{
while($this->syncFlush){
$this->wait(); //block until it's all been written to disk
}
@ -371,7 +372,7 @@ class MainLogger extends \AttachableThreadedLogger{
while(!$this->shutdown){
$this->writeLogStream($logResource);
$this->synchronized(function() : void{
if(!$this->shutdown){
if(!$this->shutdown && !$this->syncFlush){
$this->wait();
}
});