From b0e19519d006147556ded4374ee52e22a72cfaa0 Mon Sep 17 00:00:00 2001 From: coderstephen Date: Fri, 10 Jul 2015 18:24:58 -0500 Subject: [PATCH] Fix semaphores sometimes not removed after termination --- src/Forking/ForkContext.php | 14 +++++++++++++- src/Forking/Synchronized.php | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Forking/ForkContext.php b/src/Forking/ForkContext.php index c15922f..2bdb4a4 100644 --- a/src/Forking/ForkContext.php +++ b/src/Forking/ForkContext.php @@ -87,7 +87,8 @@ abstract class ForkContext extends Synchronized implements ContextInterface // Get the fatal exception from the process. return $this->parentSocket->read(2)->then(function ($data) { - list($serializedLength) = unpack('S', $data); + $serializedLength = unpack('S', $data); + $serializedLength = $serializedLength[1]; return $this->parentSocket->read($serializedLength); })->then(function ($data) { $previous = unserialize($data); @@ -162,4 +163,15 @@ abstract class ForkContext extends Synchronized implements ContextInterface * {@inheritdoc} */ abstract public function run(); + + public function __destruct() + { + parent::__destruct(); + + // The parent process outlives the child process, so don't destroy the + // semaphore until the parent exits. + if (!$this->isChild) { + $this->semaphore->destroy(); + } + } } diff --git a/src/Forking/Synchronized.php b/src/Forking/Synchronized.php index b05f024..f75a3a8 100644 --- a/src/Forking/Synchronized.php +++ b/src/Forking/Synchronized.php @@ -15,7 +15,7 @@ abstract class Synchronized { private $memoryBlock; private $memoryKey; - private $semaphore; + protected $semaphore; /** * Creates a new synchronized object.