php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68585 Run PDO is unusable in the Phreads
Submitted: 2014-12-10 08:30 UTC Modified: 2017-04-02 15:29 UTC
Votes:2
Avg. Score:2.5 ± 1.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: hewei986532 at sina dot com Assigned:
Status: Not a bug Package: pthreads (PECL)
PHP Version: 5.4.35 OS: centos
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hewei986532 at sina dot com
New email:
PHP Version: OS:

 

 [2014-12-10 08:30 UTC] hewei986532 at sina dot com
Description:
------------
It throw a error when execute a PDO's instance in the 'run' of Pthreads's method.

Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' 

Test script:
---------------
class Async extends Thread
{

    /**
     * Provide a passthrough to call_user_func_array
     * */
    public function __construct($method, $params)
    {
        $this->method = $method;
        $this->params = $params;
        $this->result = null;
        $this->joined = false;
    }

    /**
     * The smallest thread in the world
     * */
    public function run()
    {
        $this->result=call_user_func_array($this->method, $this->params);
    }

    /**
     * Static method to create your threads from functions ...
     * */
    public static function call($method, $params)
    {
        $thread = new Async($method, $params);
        if ($thread->start()) {
            return $thread;
        } /** else throw Nastyness * */
    } 

}

class test
{

    public $dbh;

    public function __construct()
    {
        $dsn = 'mysql:dbname=mysql;host=127.0.0.1';
        $user = 'root';
        $password = '123456';
        $this->dbh = new PDO($dsn, $user, $password);
    }

    public function world()
    {
        $sql = 'SELECT * FROM user ';
        foreach ($this->dbh->query($sql) as $row) {
            print $row['User'] . "\t";
        }
    }

}

$cc = new test;

/* here's us calling file_get_contents in a thread of it's own */
$future = Async::call(array($cc, 'world'), array());

Expected result:
----------------
error

Actual result:
--------------
no error

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-02 23:19 UTC] derkontrollfreak+9hy5l at gmail dot com
That's not a bug. Arrays and objects that don't extend Threaded are stored serialized. Example:

<?php
class Test extends Threaded
{
    public function __sleep()
    {
        echo "No! I'm not tired!\n";
        return [];
    }
}

$thread = new Thread;
$thread->test = new Test;
$thread->start();
// prints "No! I'm not tired!"
?>

This is also explained in the introduction: http://php.net/manual/intro.pthreads
 [2015-07-02 23:21 UTC] derkontrollfreak+9hy5l at gmail dot com
My last comment actually has a bug on its own.

Here's the fixed example:
<?php
class Test
{
    public function __sleep()
    {
        echo "No! I'm not tired!\n";
        return [];
    }
}

$thread = new Thread;
$thread->test = new Test;
$thread->start();
// prints "No! I'm not tired!"
?>
 [2017-04-02 15:29 UTC] tpunt@php.net
-Status: Open +Status: Not a bug
 [2017-04-02 15:29 UTC] tpunt@php.net
This is not a bug, but well-known behaviour.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 00:01:36 2025 UTC