Skip to content

Commit

Permalink
Merge pull request #138 from neo4j-php/137-stuck-read
Browse files Browse the repository at this point in the history
throw extra exception when reading empty buffer after timeout
  • Loading branch information
stefanak-michal authored Apr 3, 2024
2 parents 241e936 + a3f83bb commit f6d1ad8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/connection/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public function read(int $length = 2048): string
throw new ConnectException('Not initialized socket');

$output = '';
$t = microtime(true);
do {
if (mb_strlen($output, '8bit') == 0 && $this->timeout > 0 && (microtime(true) - $t) >= $this->timeout)
throw new ConnectionTimeoutException('Read from connection reached timeout after ' . $this->timeout . ' seconds.');
$readed = @socket_read($this->socket, $length - mb_strlen($output, '8bit'));
if ($readed === false)
$this->throwConnectException();
Expand Down
6 changes: 5 additions & 1 deletion src/connection/StreamSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ public function write(string $buffer): void
public function read(int $length = 2048): string
{
$output = '';
$t = microtime(true);
do {
if (mb_strlen($output, '8bit') == 0 && $this->timeout > 0 && (microtime(true) - $t) >= $this->timeout)
throw new ConnectionTimeoutException('Read from connection reached timeout after ' . $this->timeout . ' seconds.');

$readed = stream_get_contents($this->stream, $length - mb_strlen($output, '8bit'));

if (stream_get_meta_data($this->stream)['timed_out'] ?? false)
throw new ConnectionTimeoutException('Connection timeout reached after ' . $this->timeout . ' seconds.');
throw new ConnectionTimeoutException('Stream connection timed out after ' . $this->timeout . ' seconds.');
if ($readed === false)
throw new ConnectException('Read error');

Expand Down

0 comments on commit f6d1ad8

Please sign in to comment.