Skip to content
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
6 changes: 6 additions & 0 deletions src/FreeDSx/Socket/Timeout/SwooleTimerEnforcer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use function error_clear_last;
use function fwrite;
use function sprintf;
use function stream_set_blocking;
use function substr;

/**
Expand Down Expand Up @@ -68,6 +69,11 @@ private function writeWithinCoroutine(
string $data,
int $timeout,
): void {
stream_set_blocking(
$stream,
true,
);

$remaining = $data;
$coroutineId = Coroutine::getCid();
$timeoutMs = $timeout * 1000;
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/Timeout/SwooleTimerEnforcerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,36 @@ public function test_it_throws_a_write_timeout_inside_a_coroutine_when_the_peer_
);
}

public function test_it_throws_a_write_timeout_inside_a_coroutine_when_a_non_blocking_stream_stalls(): void
{
$caught = null;
$this->runInCoroutine(function () use (&$caught): void {
[$local, $remote] = $this->createSocketPair();
stream_set_blocking(
$local,
false,
);

try {
$this->subject->write(
$local,
str_repeat('x', 32 * 1024 * 1024),
1,
);
} catch (Throwable $e) {
$caught = $e;
} finally {
fclose($local);
fclose($remote);
}
});

self::assertInstanceOf(
WriteTimeoutException::class,
$caught,
);
}

private function runInCoroutine(callable $callback): void
{
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
Expand Down
Loading