1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-15 01:24:17 +02:00

Fix cycle detection within fibers (#1753)

* Fix cycle detection within fibers

We keep a separate depth count per fiber.

Fixes #1752.

* Avoid additional call to Fiber::getCurrent()

Suppresses phpstan errors, as they're false positives.
This commit is contained in:
Niklas Keller
2023-02-04 15:49:17 +01:00
committed by GitHub
parent 58f503004d
commit 0f014206a4
2 changed files with 106 additions and 4 deletions

View File

@@ -792,6 +792,58 @@ class LoggerTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($datetime->format('Y-m-d H:i:s'), $record['datetime']->format('Y-m-d H:i:s'));
}
}
/**
* @requires PHP 8.1
*/
public function testLogCycleDetectionWithFibersWithoutCycle()
{
$logger = new Logger(__METHOD__);
$fiberSuspendHandler = new FiberSuspendHandler();
$testHandler = new TestHandler();
$logger->pushHandler($fiberSuspendHandler);
$logger->pushHandler($testHandler);
$fibers = [];
for ($i = 0; $i < 10; $i++) {
$fiber = new \Fiber(static function () use ($logger) {
$logger->info('test');
});
$fiber->start();
// We need to keep a reference here, because otherwise the fiber gets automatically cleaned up
$fibers[] = $fiber;
}
self::assertCount(10, $testHandler->getRecords());
}
/**
* @requires PHP 8.1
*/
public function testLogCycleDetectionWithFibersWithCycle()
{
$logger = new Logger(__METHOD__);
$fiberSuspendHandler = new FiberSuspendHandler();
$loggingHandler = new LoggingHandler($logger);
$testHandler = new TestHandler();
$logger->pushHandler($fiberSuspendHandler);
$logger->pushHandler($loggingHandler);
$logger->pushHandler($testHandler);
$fiber = new \Fiber(static function () use ($logger) {
$logger->info('test');
});
$fiber->start();
self::assertCount(3, $testHandler->getRecords());
}
}
class LoggingHandler implements HandlerInterface
@@ -826,3 +878,27 @@ class LoggingHandler implements HandlerInterface
{
}
}
class FiberSuspendHandler implements HandlerInterface
{
public function isHandling(array $record): bool
{
return true;
}
public function handle(array $record): bool
{
\Fiber::suspend();
return true;
}
public function handleBatch(array $records): void
{
}
public function close(): void
{
}
}