kriswallsmith / spork

Experimental library for forking PHP
MIT License
586 stars 52 forks source link

Memory usage - perhaps a wee leak #33

Open cliveza opened 9 years ago

cliveza commented 9 years ago

When I run the code below (the run() function) I can see memory of the parent php process continually growing, I get the following when the code starts:

PHP Notice:  unserialize(): Error at offset 0 of 74 bytes in vendor/kriswallsmith/spork/src/Spork/SharedMemory.php on line 59
PHP Warning:  shmop_open(): unable to get shared memory segment information in vendor/kriswallsmith/spork/src/Spork/SharedMemory.php on line 89
PHP Warning:  Invalid argument supplied for foreach() in vendor/kriswallsmith/spork/src/Spork/Fork.php on line 97

And eventually when the machine runs out of memeory I get this:

PHP Warning:  shmop_open(): unable to attach or create shared memory segment in vendor/kriswallsmith/spork/src/Spork/SharedMemory.php on line 89
PHP Fatal error:  Uncaught exception 'Spork\Exception\ProcessControlException' with message 'Not able to create shared memory segment for PID: 14390' in vendor/kriswallsmith/spork/src/Spork/SharedMemory.php:91
Stack trace:
#0 vendor/kriswallsmith/spork/src/Spork/ProcessManager.php(119): Spork\SharedMemory->send(Object(Spork\Util\ExitMessage), false)
#1 [internal function]: Spork\ProcessManager->Spork\{closure}()
#2 {main}
  thrown in vendor/kriswallsmith/spork/src/Spork/SharedMemory.php on line 91
PHP Fatal error:  Uncaught exception 'Spork\Exception\ProcessControlException' with message 'Not able to create shared memory segment for PID: 14395' in vendor/kriswallsmith/spork/src/Spork/SharedMemory.php:91
Stack trace:
#0 kriswallsmith/spork/src/Spork/ProcessManager.php(119): Spork\SharedMemory->send(Object(Spork\Util\ExitMessage), false)
#1 [internal function]: Spork\ProcessManager->Spork\{closure}()
#2 {main}
  thrown in vendor/kriswallsmith/spork/src/Spork/SharedMemory.php on line 91

If I take out the spork code and run the code sequentially then the memory stays constant.

I've tried poking around in the spork library, but haven't had any luck. Any ideas or pointers?


//in constructor
$this->manager = new ProcessManager();

// I run this 
public function run() 
{
    while (self::$enabled) {
        if (count($this->workers) >= $this->workerLimit) {
            $this->cleanUpWorkers();
        }   

        $job    = $this->fetchJobFromQueue();
        $worker = $this->findWorkerForJob($job);

        if ( ! $worker) {
            throw new \Exception("Could not find worker for {$job[0]} job");
        }

        $worker = $this->manager->fork($worker);

        $this->workers[$worker->getPid()] = $worker;
    } 
}

private function cleanUpWorkers()
{
    $this->manager->check();

    foreach($this->workers as $pid => $worker) {
        $worker->wait();

        if ($worker->isExited()) {
            unset($this->workers[$pid]);
        }
    }

    return;
}