1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

"Cannot serialize Server instance" when i schedule an AsyncTask.

Discussion in 'Development' started by friscowz, Oct 27, 2017.

  1. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    [16:11:57] [Server thread/CRITICAL]: Could not pass event 'pocketmine\event\player\PlayerJoinEvent' to 'MyriadHC v1': Cannot serialize Server instance on friscowz\hc\listener\CheatListener
    [16:11:57] [Server thread/CRITICAL]: BadMethodCallException: "Cannot serialize Server instance" (EXCEPTION) in "src/pocketmine/Server" at line 2591

    this is what i get everytime i schedule my AsyncTask...
    debugs :
    [16:11:57] [Server thread/CRITICAL]: Could not pass event 'pocketmine\event\player\PlayerJoinEvent' to 'MyriadHC v1': Cannot serialize Server instance on friscowz\hc\listener\CheatListener
    [16:11:57] [Server thread/CRITICAL]: BadMethodCallException: "Cannot serialize Server instance" (EXCEPTION) in "src/pocketmine/Server" at line 2591
    [16:11:57] [Server thread/DEBUG]: #0 HardCoreFaction/src/friscowz/hc/task/async/HideBlocksTask(91): pocketmine\Server->__sleep()
    [16:11:57] [Server thread/DEBUG]: #1 HardCoreFaction/src/friscowz/hc/task/async/HideBlocksTask(27): friscowz\hc\task\async\HideBlocksTask->setPlayer(friscowz\hc\MDPlayer object)
    [16:11:57] [Server thread/DEBUG]: #2 HardCoreFaction/src/friscowz/hc/MDPlayer(704): friscowz\hc\task\async\HideBlocksTask->__construct(friscowz\hc\MDPlayer object)
    [16:11:57] [Server thread/DEBUG]: #3 HardCoreFaction/src/friscowz/hc/listener/CheatListener(96): friscowz\hc\MDPlayer->checkBlocks()
    [16:11:57] [Server thread/DEBUG]: #4 src/pocketmine/plugin/MethodEventExecutor(38): friscowz\hc\listener\CheatListener->onJoin(pocketmine\event\player\PlayerJoinEvent object)
    [16:11:57] [Server thread/DEBUG]: #5 src/pocketmine/plugin/RegisteredListener(98): pocketmine\plugin\MethodEventExecutor->execute(friscowz\hc\listener\CheatListener object, pocketmine\event\player\PlayerJoinEvent object)
    [16:11:57] [Server thread/DEBUG]: #6 src/pocketmine/plugin/PluginManager(721): pocketmine\plugin\RegisteredListener->callEvent(pocketmine\event\player\PlayerJoinEvent object)
    [16:11:57] [Server thread/DEBUG]: #7 src/pocketmine/Player(920): pocketmine\plugin\PluginManager->callEvent(pocketmine\event\player\PlayerJoinEvent object)
    [16:11:57] [Server thread/DEBUG]: #8 src/pocketmine/Player(868): pocketmine\Player->doFirstSpawn()
    [16:11:57] [Server thread/DEBUG]: #9 src/pocketmine/level/Level(2393): pocketmine\Player->sendChunk(integer 7, integer 5, pocketmine\network\mcpe\protocol\BatchPacket object)
    [16:11:57] [Server thread/DEBUG]: #10 src/pocketmine/level/Level(2434): pocketmine\level\Level->sendChunkFromCache(integer 7, integer 5)
    [16:11:57] [Server thread/DEBUG]: #11 src/pocketmine/level/format/io/ChunkRequestTask(91): pocketmine\level\Level->chunkRequestCallback(integer 7, integer 5, pocketmine\network\mcpe\protocol\BatchPacket object)
    [16:11:57] [Server thread/DEBUG]: #12 src/pocketmine/scheduler/AsyncPool(164): pocketmine\level\format\io\ChunkRequestTask->onCompletion(pocketmine\Server object)
    [16:11:57] [Server thread/DEBUG]: #13 src/pocketmine/scheduler/ServerScheduler(342): pocketmine\scheduler\AsyncPool->collectTasks()
    [16:11:57] [Server thread/DEBUG]: #14 src/pocketmine/Server(2506): pocketmine\scheduler\ServerScheduler->mainThreadHeartbeat(integer 293)
    [16:11:57] [Server thread/DEBUG]: #15 src/pocketmine/Server(2253): pocketmine\Server->tick()
    [16:11:57] [Server thread/DEBUG]: #16 src/pocketmine/Server(2132): pocketmine\Server->tickProcessor()
    [16:11:57] [Server thread/DEBUG]: #17 src/pocketmine/Server(1714): pocketmine\Server->start()
    [16:11:57] [Server thread/DEBUG]: #18 src/pocketmine/PocketMine(558): pocketmine\Server->__construct(BaseClassLoader object, pocketmine\utils\MainLogger object, string phar:///root/hcf/PocketMine-MP.phar/, string /root/hcf/, string /root/hcf/plugins/)
    [16:11:57] [Server thread/DEBUG]: #19 /root/hcf/PocketMine-MP.phar(1): require(string phar:///root/hcf/PocketMine-MP.phar/src/pocketmine/PocketMine.php)
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    you're trying to pass objects into a thread. Bad idea. What are you trying to do?

    btw please show your code (or parts of it, preferably the relevant parts)
     
  3. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    You can see it on the logs i jsut did
    public function __construct(MDPlayer $player){
    //etc
    }
     
  4. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Well, if you’re doing what i think you are doing: FORGET IT
    That’s not how threads work. What are you planning to do in the AsyncTask?
     
    jasonwynn10 likes this.
  5. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    UpdateBlockPacket for a player.
     
  6. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    changed the object (MDPlayer var) to a String:
    PHP:
    /**
    * HideBlocksTask constructor.
    * @param string $player
    */
    public function __construct(string $player)
    {
        
    $this->setPlayer($player);
    }


    /**
    * @return MDPlayer
    */
    public function getPlayer() : MDPlayer
    {
        return 
    Myriad::getInstance()->getServer()->getPlayer($this->player);
    }

    /**
    * @param string $player
    */
    public function setPlayer(string $player)
    {
        
    $this->player $player;
    }

     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You can never get a Server instnace or a Player instance on another thread.
    Therefore you can't send packets to players on other threads.
    That's all.
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Or in other words any object holding Server instance ;)
     
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    To be precise, nothing holds a Server instance. Since objects in PHP are passed as reference (not that the variable itself is passed by reference, but the object is passed by reference), a variable or a class property can only hold a reference to an object's memory address, but it can never hold the object instance itself. The object instance is held by PHP itself.
     
    Muqsit likes this.
  10. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    k ima just make a static function on my Main File and execute it on the AsyncTask
     
  11. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    what are you planning to do in that static function?
     
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Note that "on another thread" doesn't mean whether the code is in your HideBlocksTask.php. Any code that is called from onRun() of the AsyncTask is executed on another thread, wherever the code is written.
     
  13. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    Yea that's what i got... ALRIGHT How can i do it then ?
     
  14. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    I wanna UpdateBlockPacket for a player using AsyncTask
     
  15. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Just simply put: You can not!
     
  16. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
     
  17. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    If you want to know the cause, it's because Server is a heavy object. Serializing it means you are serializing everything. That includes all the plugins, levels, players...

     
  18. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Serializing it has no problem (if it can really be serialized). The problem is that it is useless even if you send it to another thread, because all passed objects are cloned (i.e. the changes won't reflect on the main thread), and all resources are dead (i.e. PHP can't send packets from another thread under any circumstances).
     
    jasonwynn10 and Jack Noordhuis like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.