Skip to content

Commit f0853b1

Browse files
committed
Don't run loop automatically when an uncaught exceptions occurs
1 parent f129a5b commit f0853b1

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/Loop.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public static function get()
4141
});
4242

4343
register_shutdown_function(function () use ($loop, &$hasRun) {
44+
// Don't run if we're coming from a fatal error (uncaught exception).
45+
$error = error_get_last();
46+
if ((isset($error['type']) ? $error['type'] : 0) & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR)) {
47+
return;
48+
}
49+
4450
if (!$hasRun) {
4551
$loop->run();
4652
}

tests/BinTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,22 @@ public function testExecuteExampleWithExplicitLoopRunAndStopRunsLoopAndExecutesT
3636

3737
$this->assertEquals('abc', $output);
3838
}
39+
40+
public function testExecuteExampleWithUncaughtExceptionShouldNotRunLoop()
41+
{
42+
$time = microtime(true);
43+
exec(escapeshellarg(PHP_BINARY) . ' 11-uncaught.php 2>/dev/null');
44+
$time = microtime(true) - $time;
45+
46+
$this->assertLessThan(1.0, $time);
47+
}
48+
49+
public function testExecuteExampleWithUndefinedVariableShouldNotRunLoop()
50+
{
51+
$time = microtime(true);
52+
exec(escapeshellarg(PHP_BINARY) . ' 12-undefined.php 2>/dev/null');
53+
$time = microtime(true) - $time;
54+
55+
$this->assertLessThan(1.0, $time);
56+
}
3957
}

tests/bin/11-uncaught.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use React\EventLoop\Loop;
4+
5+
require __DIR__ . '/../../vendor/autoload.php';
6+
7+
Loop::addTimer(10.0, function () {
8+
echo 'never';
9+
});
10+
11+
throw new RuntimeException();

tests/bin/12-undefined.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use React\EventLoop\Loop;
4+
5+
require __DIR__ . '/../../vendor/autoload.php';
6+
7+
Loop::get()->addTimer(10.0, function () {
8+
echo 'never';
9+
});
10+
11+
$undefined->foo('bar');

0 commit comments

Comments
 (0)