Skip to content

Commit d0232e9

Browse files
authored
Merge pull request #32 from henzeb/socket_interrupted_issue
fixes socket_select interrupted exception
2 parents 87eb830 + a4fc285 commit d0232e9

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Connection.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Generator;
66
use Socket;
7+
use ErrorException;
78

89
class Connection
910
{
@@ -55,7 +56,15 @@ public function write(string $payload): self
5556

5657
$except = null;
5758

58-
$selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
59+
try {
60+
$selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
61+
} catch (ErrorException $e) {
62+
if ($this->isInterruptionErrorException()) {
63+
continue;
64+
}
65+
66+
throw $e;
67+
}
5968

6069
if ($selectResult === false) {
6170
break;
@@ -90,7 +99,15 @@ public function read(): Generator
9099

91100
$except = null;
92101

93-
$selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
102+
try {
103+
$selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
104+
} catch (ErrorException $e) {
105+
if ($this->isInterruptionErrorException()) {
106+
continue;
107+
}
108+
109+
throw $e;
110+
}
94111

95112
if ($selectResult === false) {
96113
break;
@@ -109,4 +126,9 @@ public function read(): Generator
109126
yield $outputFromSocket;
110127
}
111128
}
129+
130+
private function isInterruptionErrorException(): bool
131+
{
132+
return 4 === socket_last_error();
133+
}
112134
}

0 commit comments

Comments
 (0)