Skip to content

Improve default StreamSelectLoop to report any warnings for invalid streams passed to stream_select() #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 22 additions & 2 deletions src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,28 @@ private function streamSelect(array &$read, array &$write, $timeout)
}
}

// suppress warnings that occur, when stream_select is interrupted by a signal
$ret = @\stream_select($read, $write, $except, $timeout === null ? null : 0, $timeout);
/** @var ?callable $previous */
$previous = \set_error_handler(function ($errno, $errstr) use (&$previous) {
// suppress warnings that occur when `stream_select()` is interrupted by a signal
$eintr = \defined('SOCKET_EINTR') ? \SOCKET_EINTR : 4;
if ($errno === \E_WARNING && \strpos($errstr, '[' . $eintr .']: ') !== false) {
return;
}

// forward any other error to registered error handler or print warning
return ($previous !== null) ? \call_user_func_array($previous, \func_get_args()) : false;
});

try {
$ret = \stream_select($read, $write, $except, $timeout === null ? null : 0, $timeout);
\restore_error_handler();
} catch (\Throwable $e) { // @codeCoverageIgnoreStart
\restore_error_handler();
throw $e;
} catch (\Exception $e) {
\restore_error_handler();
throw $e;
} // @codeCoverageIgnoreEnd

if ($except) {
$write = \array_merge($write, $except);
Expand Down
79 changes: 79 additions & 0 deletions tests/StreamSelectLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,85 @@ public function testStreamSelectTimeoutEmulation()
$this->assertGreaterThan(0.04, $interval);
}

public function testStreamSelectReportsWarningForStreamWithFilter()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Not supported on legacy HHVM');
}

$stream = tmpfile();
stream_filter_append($stream, 'string.rot13');

$this->loop->addReadStream($stream, $this->expectCallableNever());

$loop = $this->loop;
$this->loop->futureTick(function () use ($loop, $stream) {
$loop->futureTick(function () use ($loop, $stream) {
$loop->removeReadStream($stream);
});
});

$error = null;
$previous = set_error_handler(function ($_, $errstr) use (&$error) {
$error = $errstr;
});

try {
$this->loop->run();
} catch (\ValueError $e) {
// ignore ValueError for PHP 8+ due to empty stream array
}

restore_error_handler();

$this->assertNotNull($error);

$now = set_error_handler(function () { });
restore_error_handler();
$this->assertEquals($previous, $now);
}

public function testStreamSelectThrowsWhenCustomErrorHandlerThrowsForStreamWithFilter()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Not supported on legacy HHVM');
}

$stream = tmpfile();
stream_filter_append($stream, 'string.rot13');

$this->loop->addReadStream($stream, $this->expectCallableNever());

$loop = $this->loop;
$this->loop->futureTick(function () use ($loop, $stream) {
$loop->futureTick(function () use ($loop, $stream) {
$loop->removeReadStream($stream);
});
});

$previous = set_error_handler(function ($_, $errstr) {
throw new \RuntimeException($errstr);
});

$e = null;
try {
$this->loop->run();
restore_error_handler();
$this->fail();
} catch (\RuntimeException $e) {
restore_error_handler();
} catch (\ValueError $e) {
restore_error_handler(); // PHP 8+
$e = $e->getPrevious();
}

$this->assertInstanceOf('RuntimeException', $e);

$now = set_error_handler(function () { });
restore_error_handler();
$this->assertEquals($previous, $now);
}

public function signalProvider()
{
return array(
Expand Down