From fba12f2a138a9612578116d901d8373011b7e8f9 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sat, 25 Apr 2015 17:40:32 +0200 Subject: [PATCH] Be sure that AsyncTask finish executing, fixes #2931 --- src/pocketmine/Server.php | 7 +++++++ src/pocketmine/scheduler/AsyncPool.php | 17 +++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 6143dace7..736a06f84 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -1994,25 +1994,32 @@ class Server{ UPnP::RemovePortForward($this->getPort()); } + $this->getLogger()->debug("Disabling all plugins"); $this->pluginManager->disablePlugins(); foreach($this->players as $player){ $player->close($player->getLeaveMessage(), $this->getProperty("settings.shutdown-message", "Server closed")); } + $this->getLogger()->debug("Unloading all levels"); foreach($this->getLevels() as $level){ $this->unloadLevel($level, true); } + $this->getLogger()->debug("Removing event handlers"); HandlerList::unregisterAll(); + $this->getLogger()->debug("Stopping all tasks"); $this->scheduler->cancelAllTasks(); $this->scheduler->mainThreadHeartbeat(PHP_INT_MAX); + $this->getLogger()->debug("Saving properties"); $this->properties->save(); + $this->getLogger()->debug("Closing console"); $this->console->kill(); + $this->getLogger()->debug("Stopping network interfaces"); foreach($this->network->getInterfaces() as $interface){ $interface->shutdown(); $this->network->unregisterInterface($interface); diff --git a/src/pocketmine/scheduler/AsyncPool.php b/src/pocketmine/scheduler/AsyncPool.php index c91951a25..4bf2e6154 100644 --- a/src/pocketmine/scheduler/AsyncPool.php +++ b/src/pocketmine/scheduler/AsyncPool.php @@ -106,6 +106,9 @@ class AsyncPool{ private function removeTask(AsyncTask $task){ if(isset($this->taskWorkers[$task->getTaskId()])){ + if($task->isRunning() or !$task->isGarbage()){ + return; + } $this->workers[$w = $this->taskWorkers[$task->getTaskId()]]->unstack($task); $this->workerUsage[$w]--; } @@ -119,9 +122,15 @@ class AsyncPool{ } public function removeTasks(){ - foreach($this->tasks as $task){ - $this->removeTask($task); - } + do{ + foreach($this->tasks as $task){ + $this->removeTask($task); + } + + if(count($this->tasks) > 0){ + usleep(25000); + } + }while(count($this->tasks) > 0); for($i = 0; $i < $this->size; ++$i){ $this->workerUsage[$i] = 0; @@ -135,7 +144,7 @@ class AsyncPool{ Timings::$schedulerAsyncTimer->startTiming(); foreach($this->tasks as $task){ - if($task->isGarbage()){ + if($task->isGarbage() and !$task->isRunning()){ $task->onCompletion($this->server);