diff --git a/src/Loop.php b/src/Loop.php index 8cc9dd8f..a247579e 100644 --- a/src/Loop.php +++ b/src/Loop.php @@ -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(); } diff --git a/tests/BinTest.php b/tests/BinTest.php index 99c05d91..55b3aaca 100644 --- a/tests/BinTest.php +++ b/tests/BinTest.php @@ -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); + } } diff --git a/tests/bin/11-uncaught.php b/tests/bin/11-uncaught.php new file mode 100644 index 00000000..0655698b --- /dev/null +++ b/tests/bin/11-uncaught.php @@ -0,0 +1,11 @@ +addTimer(10.0, function () { + echo 'never'; +}); + +$undefined->foo('bar');