diff --git a/src/scheduler/AsyncPool.php b/src/scheduler/AsyncPool.php index 85a27390a..6125c62ce 100644 --- a/src/scheduler/AsyncPool.php +++ b/src/scheduler/AsyncPool.php @@ -24,7 +24,6 @@ declare(strict_types=1); namespace pocketmine\scheduler; use pmmp\thread\Thread as NativeThread; -use pmmp\thread\ThreadSafeArray; use pocketmine\snooze\SleeperHandler; use pocketmine\thread\log\ThreadSafeLogger; use pocketmine\thread\ThreadSafeClassLoader; @@ -146,7 +145,6 @@ class AsyncPool{ throw new \InvalidArgumentException("Cannot submit the same AsyncTask instance more than once"); } - $task->progressUpdates = new ThreadSafeArray(); $task->setSubmitted(); $this->getWorker($worker)->submit($task); diff --git a/src/scheduler/AsyncTask.php b/src/scheduler/AsyncTask.php index 367957b4e..6d43ac5c8 100644 --- a/src/scheduler/AsyncTask.php +++ b/src/scheduler/AsyncTask.php @@ -68,8 +68,8 @@ abstract class AsyncTask extends Runnable{ */ private static ?\ArrayObject $threadLocalStorage = null; - /** @phpstan-var ThreadSafeArray */ - public ThreadSafeArray $progressUpdates; + /** @phpstan-var ThreadSafeArray|null */ + private ?ThreadSafeArray $progressUpdates = null; private ThreadSafe|string|int|bool|null|float $result = null; private bool $cancelRun = false; @@ -163,15 +163,22 @@ abstract class AsyncTask extends Runnable{ * @param mixed $progress A value that can be safely serialize()'ed. */ public function publishProgress(mixed $progress) : void{ - $this->progressUpdates[] = igbinary_serialize($progress) ?? throw new \InvalidArgumentException("Progress must be serializable"); + $progressUpdates = $this->progressUpdates; + if($progressUpdates === null){ + $progressUpdates = $this->progressUpdates = new ThreadSafeArray(); + } + $progressUpdates[] = igbinary_serialize($progress) ?? throw new \InvalidArgumentException("Progress must be serializable"); } /** * @internal Only call from AsyncPool.php on the main thread */ public function checkProgressUpdates() : void{ - while(($progress = $this->progressUpdates->shift()) !== null){ - $this->onProgressUpdate(igbinary_unserialize($progress)); + $progressUpdates = $this->progressUpdates; + if($progressUpdates !== null){ + while(($progress = $progressUpdates->shift()) !== null){ + $this->onProgressUpdate(igbinary_unserialize($progress)); + } } }