From 11ecddfa9ed5621fb927c1b7d4379be3d9b8a6a7 Mon Sep 17 00:00:00 2001 From: obelloc <9536475+obelloc@users.noreply.github.com> Date: Fri, 27 Mar 2026 15:57:01 +0100 Subject: [PATCH] Fix truncation of large payloads --- src/Connection.php | 13 +++---------- tests/ForkTest.php | 14 ++++++++------ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index c188f0b..7b78606 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -7,19 +7,12 @@ class Connection { - protected int $timeoutSeconds; - protected int $timeoutMicroseconds; - protected function __construct( protected Socket $socket, protected int $bufferSize = 1024, - protected float $timeout = 0.0001, + protected int $writeTimeoutSeconds = 5, ) { socket_set_nonblock($this->socket); - - $this->timeoutSeconds = floor($this->timeout); - - $this->timeoutMicroseconds = ($this->timeout * 1_000_000) - ($this->timeoutSeconds * 1_000_000); } /** @@ -55,7 +48,7 @@ public function write(string $payload): self $except = null; - $selectResult = @socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds); + $selectResult = @socket_select($read, $write, $except, $this->writeTimeoutSeconds); if ($selectResult === false && socket_last_error() === SOCKET_EINTR) { continue; @@ -94,7 +87,7 @@ public function read(): Generator $except = null; - $selectResult = @socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds); + $selectResult = @socket_select($read, $write, $except, 0); if ($selectResult === false && socket_last_error() === SOCKET_EINTR) { continue; diff --git a/tests/ForkTest.php b/tests/ForkTest.php index 760a7f4..d7706ca 100644 --- a/tests/ForkTest.php +++ b/tests/ForkTest.php @@ -135,13 +135,16 @@ function () { }); test('will not hang by truncating the result when large output is returned', function () { + $value = str_repeat('#', 1048576); // 1MB expect( - Fork::new()->run( - fn () => file_get_contents('https://stitcher.io/rss'), - fn () => file_get_contents('https://sebastiandedeyne.com/index.xml'), - fn () => file_get_contents('https://rubenvanassche.com/rss/'), + // compare md5 instead of content to avoid large text dumps in case the test fails + array_map( + 'md5', + Fork::new()->run( + ...array_fill(0, 16, fn () => $value) + ) ) - )->toHaveCount(3); + )->toEqual(array_fill(0, 16, md5($value))); }); test('can return objects', function () { @@ -192,5 +195,4 @@ static function () { }, ) )->toThrow(CouldNotManageTask::class); - });