Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Assigning array to Threaded object results in Volatile object (despite array cast) #917

Open
LuckyFellow opened this issue Jan 22, 2019 · 1 comment

Comments

@LuckyFellow
Copy link

Environment

  • PHP: 7.2.13
  • pthreads: 3.2.0
  • OS: linux, docker, debian stretch

Summary

Trying to assign an array to a Threaded object inside of a Threaded object ($this->store["test"][]), results in automatic creation of a Volatile object. The volatile object gets destroyed, as expected, when leaving the thread context.

Is this expected behaviour?
How must I apply type casting to produce the expected output / What do I need to do differently?
Can I expect my workaround to function properly?

Reproducing Code

<?php
$store = new Threaded();
$store["test"] = new Threaded();
$thread = new class($store) extends Thread {
        public $store;
        public function __construct(Threaded $store)
        {
                $this->store = $store;
        }
        public function run()
        {
                $this->store["test"][] = (array)[1,2,3]; // this results in Volatile object

// Uncomment the follwing lines and the expected output is produced.
//                $tmp = $this->store["test"];
//                $tmp[] = (array)[1,2,3];

        }
};
$thread->start() && $thread->join();
var_dump($store);

?>

Expected Output

object(Threaded)#1 (1) {
  ["test"]=>
  object(Threaded)#2 (1) {
    [0]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      [2]=>
      int(3)
    }
  }
}

Actual Output

object(Threaded)#1 (1) {
  ["test"]=>
  object(Threaded)#2 (0) {
  }
}
PHP Fatal error:  Uncaught RuntimeException: pthreads detected an attempt to connect to an object which has already been destroyed in /app/test.php:21
Stack trace:
#0 /app/test.php(21): var_dump(Object(Threaded))
#1 {main}
  thrown in /app/test.php on line 21

@dktapps
Copy link
Contributor

dktapps commented Jan 22, 2019

on a side note, this casting behaviour is a serious pest and I'd love to see it gone... @sirsnyder what do you think of scrapping this and adding something like Threaded::fromArray() / Volatile::fromArray() instead?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants