TaskScheduler: don't catch unexpected exceptions

this means that errors in scheduled tasks which are uncaught will now cause a server crash.
This commit is contained in:
Dylan K. Taylor 2018-11-04 23:22:30 +00:00
parent 1634dd62e3
commit e26af3fa1b

View File

@ -30,8 +30,6 @@ namespace pocketmine\scheduler;
use pocketmine\utils\ReversePriorityQueue; use pocketmine\utils\ReversePriorityQueue;
class TaskScheduler{ class TaskScheduler{
/** @var \Logger */
private $logger;
/** @var string|null */ /** @var string|null */
private $owner; private $owner;
@ -54,9 +52,11 @@ class TaskScheduler{
/** @var int */ /** @var int */
protected $currentTick = 0; protected $currentTick = 0;
/**
* @param \Logger $logger @deprecated
* @param null|string $owner
*/
public function __construct(\Logger $logger, ?string $owner = null){ public function __construct(\Logger $logger, ?string $owner = null){
$this->logger = $logger;
$this->owner = $owner; $this->owner = $owner;
$this->queue = new ReversePriorityQueue(); $this->queue = new ReversePriorityQueue();
} }
@ -108,11 +108,9 @@ class TaskScheduler{
if(isset($this->tasks[$taskId])){ if(isset($this->tasks[$taskId])){
try{ try{
$this->tasks[$taskId]->cancel(); $this->tasks[$taskId]->cancel();
}catch(\Throwable $e){ }finally{
$this->logger->critical("Task " . $this->tasks[$taskId]->getTaskName() . " threw an exception when trying to cancel: " . $e->getMessage()); unset($this->tasks[$taskId]);
$this->logger->logException($e);
} }
unset($this->tasks[$taskId]);
} }
} }
@ -198,26 +196,14 @@ class TaskScheduler{
unset($this->tasks[$task->getTaskId()]); unset($this->tasks[$task->getTaskId()]);
continue; continue;
} }
$crashed = false; $task->run($this->currentTick);
try{
$task->run($this->currentTick);
}catch(\Throwable $e){
$crashed = true;
$this->logger->critical("Could not execute task " . $task->getTaskName() . ": " . $e->getMessage());
$this->logger->logException($e);
}
if($task->isRepeating()){ if($task->isRepeating()){
if($crashed){ $task->setNextRun($this->currentTick + $task->getPeriod());
$this->logger->debug("Dropping repeating task " . $task->getTaskName() . " due to exceptions thrown while running"); $this->queue->insert($task, $this->currentTick + $task->getPeriod());
}else{ }else{
$task->setNextRun($this->currentTick + $task->getPeriod()); $task->remove();
$this->queue->insert($task, $this->currentTick + $task->getPeriod()); unset($this->tasks[$task->getTaskId()]);
continue;
}
} }
$task->remove();
unset($this->tasks[$task->getTaskId()]);
} }
} }