Skip to content

Commit

Permalink
Don't run loop automatically when an uncaught exceptions occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jun 30, 2021
1 parent d82858b commit f2556b4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public static function get()
});

register_shutdown_function(function () use ($loop, &$hasRun) {
// Don't run if we're coming from a fatal error (uncaught exception).
$error = error_get_last();
if ((isset($error['type']) ? $error['type'] : 0) & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR)) {
return;
}

if (!$hasRun) {
$loop->run();
}
Expand Down
18 changes: 18 additions & 0 deletions tests/BinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,22 @@ public function testExecuteExampleWithExplicitLoopRunAndStopRunsLoopAndExecutesT

$this->assertEquals('abc', $output);
}

public function testExecuteExampleWithUncaughtExceptionShouldNotRunLoop()
{
$time = microtime(true);
exec(escapeshellarg(PHP_BINARY) . ' 11-uncaught.php 2>/dev/null');
$time = microtime(true) - $time;

$this->assertLessThan(1.0, $time);
}

public function testExecuteExampleWithUndefinedVariableShouldNotRunLoop()
{
$time = microtime(true);
exec(escapeshellarg(PHP_BINARY) . ' 12-undefined.php 2>/dev/null');
$time = microtime(true) - $time;

$this->assertLessThan(1.0, $time);
}
}
11 changes: 11 additions & 0 deletions tests/bin/11-uncaught.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use React\EventLoop\Loop;

require __DIR__ . '/../../vendor/autoload.php';

Loop::addTimer(10.0, function () {
echo 'never';
});

throw new RuntimeException();
11 changes: 11 additions & 0 deletions tests/bin/12-undefined.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use React\EventLoop\Loop;

require __DIR__ . '/../../vendor/autoload.php';

Loop::get()->addTimer(10.0, function () {
echo 'never';
});

$undefined->foo('bar');

0 comments on commit f2556b4

Please sign in to comment.