Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the bug that CoroutineServer process exit when input variables exceeded #6525

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions CHANGELOG-3.1.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# v3.1.10 - TBD

## Fixed

- [#6525](https://github.com/hyperf/hyperf/pull/6525) Fixed the bug that `CoroutineServer` process exit when input variables exceeded.

# v3.1.9 - 2024-02-18

## Fixed
Expand Down
23 changes: 22 additions & 1 deletion src/exception-handler/src/Listener/ErrorExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@
namespace Hyperf\ExceptionHandler\Listener;

use ErrorException;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication;
use Psr\Container\ContainerInterface;

class ErrorExceptionHandler implements ListenerInterface
{
protected ?StdoutLoggerInterface $logger = null;

public function __construct(ContainerInterface $container)
{
if ($container->has(StdoutLoggerInterface::class)) {
$this->logger = $container->get(StdoutLoggerInterface::class);
}
}

public function listen(): array
{
return [
Expand All @@ -26,8 +37,18 @@ public function listen(): array

public function process(object $event): void
{
set_error_handler(static function ($level, $message, $file = '', $line = 0): bool {
$logger = $this->logger;
set_error_handler(static function ($level, $message, $file = '', $line = 0) use ($logger): bool {
if (error_reporting() & $level) {
if ($line === 0) {
if ($logger) {
$logger->error($message);
} else {
echo $message . PHP_EOL;
}
return true;
}

throw new ErrorException($message, 0, $level, $file, $line);
}

Expand Down
10 changes: 9 additions & 1 deletion src/exception-handler/tests/ErrorExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ErrorException;
use Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use PHPUnit\Framework\TestCase;
Expand All @@ -24,10 +25,17 @@
#[CoversNothing]
class ErrorExceptionHandlerTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
}

#[WithoutErrorHandler]
public function testHandleError()
{
$listener = new ErrorExceptionHandler();
$container = Mockery::mock(\Psr\Container\ContainerInterface::class);
$container->shouldReceive('has')->with(\Hyperf\Contract\StdoutLoggerInterface::class)->andReturn(false);
$listener = new ErrorExceptionHandler($container);
$listener->process((object) []);

$this->expectException(ErrorException::class);
Expand Down