close
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Don't run loop automatically when an uncaught exceptions occurs
  • Loading branch information
clue committed Jul 1, 2021
commit f0853b115e9f1b922168e6a824ee2ebea7267468
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');