Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

readBytes fix-read-endless-loop #196

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/Engine/AbstractSocketIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ElephantIO\Payload\Decoder;
use ElephantIO\Exception\UnsupportedActionException;
use ElephantIO\Exception\MalformedUrlException;
use ElephantIO\Exception\ServerConnectionFailureException;

abstract class AbstractSocketIO implements EngineInterface
{
Expand Down Expand Up @@ -112,9 +113,15 @@ protected function readBytes($bytes)
{
$data = '';
$chunk = null;
$i = 0;
while ($bytes > 0 && false !== ($chunk = \fread($this->stream, $bytes))) {
if ($i > 2) {
throw new ServerConnectionFailureException('fread times error');
}
Comment on lines +118 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty arbitrary that after 2 iterations, we would make it die.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did not find correct way to solve endless loop problem.
when fread always return empty string, cpu will went high and loop will not break off.
i tried some ways, timed_out, is_resource... but did not work...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What could be done would be to temper with pcntl signals to stop the loop then. WDYT ?


$bytes -= \strlen($chunk);
$data .= $chunk;
$i++;
}

if (false === $chunk) {
Expand All @@ -136,6 +143,7 @@ public function read()
}

$this->keepAlive();

/*
* The first byte contains the FIN bit, the reserved bits, and the
* opcode... We're not interested in them. Yet.
Expand Down
1 change: 1 addition & 0 deletions src/Engine/SocketIO/Version1X.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function write($code, $message = null)
}

$payload = new Encoder($code . $message, Encoder::OPCODE_TEXT, true);

$bytes = @\fwrite($this->stream, (string) $payload);

if ($bytes === false){
Expand Down