Skip to content

Commit f6d1ad8

Browse files
Merge pull request #138 from neo4j-php/137-stuck-read
throw extra exception when reading empty buffer after timeout
2 parents 241e936 + a3f83bb commit f6d1ad8

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/connection/Socket.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ public function read(int $length = 2048): string
7575
throw new ConnectException('Not initialized socket');
7676

7777
$output = '';
78+
$t = microtime(true);
7879
do {
80+
if (mb_strlen($output, '8bit') == 0 && $this->timeout > 0 && (microtime(true) - $t) >= $this->timeout)
81+
throw new ConnectionTimeoutException('Read from connection reached timeout after ' . $this->timeout . ' seconds.');
7982
$readed = @socket_read($this->socket, $length - mb_strlen($output, '8bit'));
8083
if ($readed === false)
8184
$this->throwConnectException();

src/connection/StreamSocket.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,15 @@ public function write(string $buffer): void
9090
public function read(int $length = 2048): string
9191
{
9292
$output = '';
93+
$t = microtime(true);
9394
do {
95+
if (mb_strlen($output, '8bit') == 0 && $this->timeout > 0 && (microtime(true) - $t) >= $this->timeout)
96+
throw new ConnectionTimeoutException('Read from connection reached timeout after ' . $this->timeout . ' seconds.');
97+
9498
$readed = stream_get_contents($this->stream, $length - mb_strlen($output, '8bit'));
9599

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

0 commit comments

Comments
 (0)