AsyncTask: Fix retrieval of null data from the thread-local storage (#6176)

This commit is contained in:
TheNewHEROBRINE 2023-12-06 14:40:09 +01:00 committed by GitHub
parent bd65948453
commit 2420dee8be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -28,6 +28,7 @@ use pmmp\thread\Thread as NativeThread;
use pmmp\thread\ThreadSafe; use pmmp\thread\ThreadSafe;
use pmmp\thread\ThreadSafeArray; use pmmp\thread\ThreadSafeArray;
use pocketmine\thread\NonThreadSafeValue; use pocketmine\thread\NonThreadSafeValue;
use function array_key_exists;
use function assert; use function assert;
use function igbinary_serialize; use function igbinary_serialize;
use function igbinary_unserialize; use function igbinary_unserialize;
@ -230,7 +231,7 @@ abstract class AsyncTask extends Runnable{
*/ */
protected function fetchLocal(string $key){ protected function fetchLocal(string $key){
$id = spl_object_id($this); $id = spl_object_id($this);
if(!isset(self::$threadLocalStorage[$id][$key])){ if(!isset(self::$threadLocalStorage[$id]) || !array_key_exists($key, self::$threadLocalStorage[$id])){
throw new \InvalidArgumentException("No matching thread-local data found on this thread"); throw new \InvalidArgumentException("No matching thread-local data found on this thread");
} }

View File

@ -121,4 +121,23 @@ class AsyncPoolTest extends TestCase{
usleep(50 * 1000); usleep(50 * 1000);
} }
} }
public function testNullComplexDataFetch() : void{
$this->pool->submitTask(new class extends AsyncTask{
public function __construct(){
$this->storeLocal("null", null);
}
public function onRun() : void{
//dummy
}
public function onCompletion() : void{
AsyncPoolTest::assertNull($this->fetchLocal("null"));
}
});
while($this->pool->collectTasks()){
usleep(50 * 1000);
}
}
} }