Remove Collectable class, fix memory leak on AsyncTask fatal error

This commit is contained in:
Dylan K. Taylor 2018-11-05 17:26:22 +00:00
parent c201a0e909
commit ed8569a3f4
3 changed files with 18 additions and 45 deletions

View File

@ -1,37 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine;
abstract class Collectable extends \Threaded{
private $isGarbage = false;
public function isGarbage() : bool{
return $this->isGarbage;
}
public function setGarbage(){
$this->isGarbage = true;
}
}

View File

@ -219,7 +219,7 @@ class AsyncPool{
/** @var AsyncTask $task */ /** @var AsyncTask $task */
$task = $queue->bottom(); $task = $queue->bottom();
$task->checkProgressUpdates(); $task->checkProgressUpdates();
if(!$task->isRunning() and $task->isGarbage()){ //make sure the task actually executed before trying to collect if($task->isFinished()){ //make sure the task actually executed before trying to collect
$doGC = true; $doGC = true;
$queue->dequeue(); $queue->dequeue();

View File

@ -23,8 +23,6 @@ declare(strict_types=1);
namespace pocketmine\scheduler; namespace pocketmine\scheduler;
use pocketmine\Collectable;
/** /**
* Class used to run async tasks in other threads. * Class used to run async tasks in other threads.
* *
@ -42,7 +40,7 @@ use pocketmine\Collectable;
* *
* WARNING: Do not call PocketMine-MP API methods from other Threads!! * WARNING: Do not call PocketMine-MP API methods from other Threads!!
*/ */
abstract class AsyncTask extends Collectable{ abstract class AsyncTask extends \Threaded{
/** /**
* @var \ArrayObject|mixed[] object hash => mixed data * @var \ArrayObject|mixed[] object hash => mixed data
* *
@ -63,6 +61,8 @@ abstract class AsyncTask extends Collectable{
private $submitted = false; private $submitted = false;
private $crashed = false; private $crashed = false;
/** @var bool */
private $finished = false;
public function run() : void{ public function run() : void{
$this->result = null; $this->result = null;
@ -76,13 +76,23 @@ abstract class AsyncTask extends Collectable{
} }
} }
$this->setGarbage(); $this->finished = true;
} }
public function isCrashed() : bool{ public function isCrashed() : bool{
return $this->crashed or $this->isTerminated(); return $this->crashed or $this->isTerminated();
} }
/**
* Returns whether this task has finished executing, whether successfully or not. This differs from isRunning()
* because it is not true prior to task execution.
*
* @return bool
*/
public function isFinished() : bool{
return $this->finished or $this->isCrashed();
}
/** /**
* @return mixed * @return mixed
*/ */
@ -131,7 +141,7 @@ abstract class AsyncTask extends Collectable{
* @return mixed * @return mixed
*/ */
public function getFromThreadStore(string $identifier){ public function getFromThreadStore(string $identifier){
if($this->worker === null or $this->isGarbage()){ if($this->worker === null or $this->isFinished()){
throw new \BadMethodCallException("Objects stored in AsyncWorker thread-local storage can only be retrieved during task execution"); throw new \BadMethodCallException("Objects stored in AsyncWorker thread-local storage can only be retrieved during task execution");
} }
return $this->worker->getFromThreadStore($identifier); return $this->worker->getFromThreadStore($identifier);
@ -144,7 +154,7 @@ abstract class AsyncTask extends Collectable{
* @param mixed $value * @param mixed $value
*/ */
public function saveToThreadStore(string $identifier, $value) : void{ public function saveToThreadStore(string $identifier, $value) : void{
if($this->worker === null or $this->isGarbage()){ if($this->worker === null or $this->isFinished()){
throw new \BadMethodCallException("Objects can only be added to AsyncWorker thread-local storage during task execution"); throw new \BadMethodCallException("Objects can only be added to AsyncWorker thread-local storage during task execution");
} }
$this->worker->saveToThreadStore($identifier, $value); $this->worker->saveToThreadStore($identifier, $value);
@ -156,7 +166,7 @@ abstract class AsyncTask extends Collectable{
* @param string $identifier * @param string $identifier
*/ */
public function removeFromThreadStore(string $identifier) : void{ public function removeFromThreadStore(string $identifier) : void{
if($this->worker === null or $this->isGarbage()){ if($this->worker === null or $this->isFinished()){
throw new \BadMethodCallException("Objects can only be removed from AsyncWorker thread-local storage during task execution"); throw new \BadMethodCallException("Objects can only be removed from AsyncWorker thread-local storage during task execution");
} }
$this->worker->removeFromThreadStore($identifier); $this->worker->removeFromThreadStore($identifier);