Skip to content

Commit

Permalink
Merge pull request #32 from henzeb/socket_interrupted_issue
Browse files Browse the repository at this point in the history
fixes socket_select interrupted exception
  • Loading branch information
freekmurze authored Oct 3, 2022
2 parents 87eb830 + a4fc285 commit d0232e9
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Generator;
use Socket;
use ErrorException;

class Connection
{
Expand Down Expand Up @@ -55,7 +56,15 @@ public function write(string $payload): self

$except = null;

$selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
try {
$selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
} catch (ErrorException $e) {
if ($this->isInterruptionErrorException()) {
continue;
}

throw $e;
}

if ($selectResult === false) {
break;
Expand Down Expand Up @@ -90,7 +99,15 @@ public function read(): Generator

$except = null;

$selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
try {
$selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
} catch (ErrorException $e) {
if ($this->isInterruptionErrorException()) {
continue;
}

throw $e;
}

if ($selectResult === false) {
break;
Expand All @@ -109,4 +126,9 @@ public function read(): Generator
yield $outputFromSocket;
}
}

private function isInterruptionErrorException(): bool
{
return 4 === socket_last_error();
}
}

0 comments on commit d0232e9

Please sign in to comment.