3

I'm using Symfony 3 and php pthreads in my project. I need to access the entity manager from a Thread. I read some way how to do this on StackOverflow and the issue tracker of pthreads.

class BaseBackgroundThread extends \Thread
{
    private $container;
    private $env;
    private $debug;

    public function __construct($env, $debug)
    {
        $this->env = $env;
        $this->debug = $debug;
    }

    public function run()
    {
        require 'app/autoload.php';
        include_once 'var/bootstrap.php.cache';

        $kernel = new \AppKernel($this->env, $this->debug);
        $kernel->loadClassCache();
        $kernel->boot();

        $this->container = $kernel->getContainer();
    }

    public function start(int $options = null)
    {
        return parent::start(PTHREADS_INHERIT_NONE);
    }
}

But there is one problem. I get a segmentation fault (core dumped) error when i'm calling $kernel->getContainer().

Update:

I realized that $kernel->getContainer() doesn't cause crash. But $this->container = $kernel->getContainer() causes crash.

Update 2:
$this->container = $kernel->getContainer() causes crash
self::$container = $kernel->getContainer() causes crash too
$container = $kernel->getContainer() doesn't cause crash.

Update 3:

I tried to run it on php version PHP 5.6.27RC1 (cli) (built: Oct 12 2016 02:24:27)

And got this error.

Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' in /root/TheBuggestBot/src/TheBuggestBot/CoreBundle/BackgroundThread/BaseBackgroundThread.php:27 zend_mm_heap corrupted

But in this (https://github.com/krakjoe/pthreads/issues/369) discussion said that this code works.

P.S. Issue on github -> https://github.com/krakjoe/pthreads/issues/629

Dm3Ch
  • 621
  • 1
  • 10
  • 26
  • 1
    Can you even serialize the container? There is a high chance of hitting reference cycle which would lead to stack overflow. What is the motivation behind this? – Jovan Perovic Oct 10 '16 at 11:14
  • I tried to use `getContainer` without serialize, but i've got same error. I tried to serialize container because I read in other discussions that it is only way to make thread using a normal size of memory(People writes that without serialize objects not collected by GC and occupy a lot of memory) – Dm3Ch Oct 10 '16 at 11:20
  • Dummy question, but are you sure you are running this on PHP thread safe version? Also, which version of PHP is this? – Jovan Perovic Oct 10 '16 at 13:15
  • 1
    Yes, i'm sure. PHP version: PHP 7.0.5-4+donate.sury.org~xenial+1 (cli) ( ZTS ) It was installed from special PPA where php compiled with ZTS enabled. – Dm3Ch Oct 10 '16 at 13:34
  • In that case, what I would try first is to test the code on some `5.6` and see if it breaks. From what you have described, seems like `$this` is the issue. It is almost, like object gets destroyed prematurely, so no assignment to class member could occurs (basically `NPE` but much nastier) – Jovan Perovic Oct 11 '16 at 09:32
  • @JovanPerovic I have added update 3 section with results. – Dm3Ch Oct 11 '16 at 23:43

0 Answers0