Fixed the dead cycle caused by socket closed.

This commit is contained in:
李铭昕 2019-11-12 11:39:48 +08:00
parent 639f2b7a9a
commit 37cacf16a8
2 changed files with 27 additions and 0 deletions

View File

@ -18,6 +18,7 @@ use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
use Hyperf\Process\Event\AfterProcessHandle;
use Hyperf\Process\Event\BeforeProcessHandle;
use Hyperf\Process\Event\PipeMessage;
use Hyperf\Process\Exception\SocketClosedException;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Swoole\Coroutine\Channel;
@ -137,6 +138,9 @@ abstract class AbstractProcess implements ProcessInterface
/** @var \Swoole\Coroutine\Socket $sock */
$sock = $this->process->exportSocket();
$recv = $sock->recv($this->recvLength, $this->recvTimeout);
if ($recv === '') {
throw new SocketClosedException('Socket is closed.');
}
if ($this->event && $recv !== false && $data = unserialize($recv)) {
$this->event->dispatch(new PipeMessage($data));
}
@ -146,6 +150,10 @@ abstract class AbstractProcess implements ProcessInterface
$formatter = $this->container->get(FormatterInterface::class);
$logger->error($formatter->format($exception));
}
if ($exception instanceof SocketClosedException) {
throw $exception;
}
}
}
$quit->close();

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Process\Exception;
use RuntimeException;
class SocketClosedException extends RuntimeException
{
}