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.