#49010 [Opn]: spl_object_hash not unique for Objects used in concurrent (forked) environment
| From: | benjamin at josefus dot net | Date: | Tue, 21 Jul 2009 21:12:14 +0000 |
| Subject: | #49010 [Opn]: spl_object_hash not unique for Objects used in concurrent (forked) environment | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-140034@lists.php.net to get a copy of this message | ||
ID: 49010
User updated by: benjamin at josefus dot net
Reported By: benjamin at josefus dot net
Status: Open
Bug Type: SPL related
Operating System: Ubuntu 9.04, Kernel2.6.27
PHP Version: 5.3SVN-2009-07-21 (snap)
New Comment:
This is the result with spl_object_hash() invoked first off (as it
should be):
not yet shared, initialize SHM 0000000016c4f93000000000506bf64a
Worker: --> Wait!, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 0, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 0, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 1, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 1, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 2, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 2, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 3, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 3, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 4, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 4, ID: 0000000016c4f93000000000506bf64a
Worker: --> Wait!, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 5, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 5, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 6, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 6, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 7, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 7, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 8, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 8, ID: 0000000016c4f93000000000506bf64a
Producer: --> Producing 9, ID: 0000000016c4f93000000000506bf64a
Worker: --> Working on 9, ID: 0000000016c4f93000000000506bf64a
This one happens when spl_object_hash() has not been invoked first
off:
not yet shared, initialize SHM 0000000051f9a9a50000000056018292
not yet shared, initialize SHM 00000000187ccfd60000000067032181
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Producer: --> Producing 0, ID: 0000000051f9a9a50000000056018292
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Producer: --> Producing 1, ID: 0000000051f9a9a50000000056018292
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Producer: --> Producing 2, ID: 0000000051f9a9a50000000056018292
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Producer: --> Producing 3, ID: 0000000051f9a9a50000000056018292
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Producer: --> Producing 4, ID: 0000000051f9a9a50000000056018292
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Producer: --> Producing 5, ID: 0000000051f9a9a50000000056018292
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Producer: --> Producing 6, ID: 0000000051f9a9a50000000056018292
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Producer: --> Producing 7, ID: 0000000051f9a9a50000000056018292
Worker: --> Wait!, ID: 00000000187ccfd60000000067032181
Producer: --> Producing 8, ID: 0000000051f9a9a50000000056018292
Producer: --> Producing 9, ID: 0000000051f9a9a50000000056018292
Previous Comments:
------------------------------------------------------------------------
[2009-07-21 20:54:56] benjamin at josefus dot net
Description:
------------
I am working on a Task Management (java-like Threads) where each Task
implements a pcntl_fork(), which works with shared Objects using IPC and
Semaphore to access concurrently and synchronized.
The Synchronisation should happen by unique Key per Object and SHM
Segment (delivered by spl_object_hash). I am using sem_acquire(),
sem_release(), shm_attach(), shm_release(), shm_[put|get]_var() and so
on.
When providing two concurrent Tasks with the same Object to be shared,
spl_object_hash delivers two different Hashes, when invoked in each
Task.
Interestingly, if i call spl_object_hash for the shared Object before
it is assigned to both "Producer" and "Worker" Task, the Hash IS unique,
as it should be!
In short:
- new Object
- fork -> spl_object_hash(Object) is NOT unique
- new Object
- call spl_object_hash(Object) first off
- fork -> spl_object_hash(Object) IS unique and correct
System Notes:
PHP 5.3.0 (cli) (built: Jul 21 2009 22:19:17)
Configure Command './configure' '--enable-pcntl' '--enable-shmop'
'--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm'
'--with-apxs2=/usr/bin/apxs2' '--disable-short-tags' '--with-openssl'
'--with-zlib' '--enable-bcmath' '--with-bz2=/bin/bzip2'
'--enable-calendar' '--with-curl' '--with-curlwrappers'
'--enable-exif'
'--enable-ftp' '--with-gd' '--with-jpeg-dir=/usr/lib'
'--with-png-dir=/usr/lib' '--with-xpm-dir=/usr/lib' '--with-t1lib'
'--enable-gd-native-ttf' '--enable-gd-jis-conv' '--with-gettext'
'--with-imap' '--with-imap-ssl' '--with-ldap'
'--with-ldap-sasl'
'--enable-mbstring' '--with-mcrypt' '--with-mhash'
'--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql'
'--with-pspell' '--with-readline' '--with-snmp'
'--enable-soap'
'--enable-sockets' '--without-sqlite' '--enable-sqlite-utf8'
'--with-tidy' '--enable-wddx' '--with-xmlrpc' '--with-xsl'
'--enable-zip' '--with-pear' '--with-kerberos'
'--with-pgsql'
'--with-pdo-pgsql'
Reproduce code:
---------------
$shared = new MySharedObj();
spl_object_hash($shared); //toggle this line to for in/correct result
/**
* $shared is referenced member variable in each task
*/
$producer = new ProducerTask($shared);
$worker = new WorkerTask($shared);
$producer->start();
$worker->start(); //outputs different Hash for $shared provided by
spl_object_hash
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49010&edit=1