Skip to content

Commit 42f269b

Browse files
committed
Workaround connection rejection race condition
See clue/reactphp-buzz#19
1 parent f75e2e6 commit 42f269b

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src/UnixConnector.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*
1717
* Unix domain sockets use atomic operations, so we can as well emulate
1818
* async behavior.
19+
*
20+
* Use a small delay of 0.001s to avoid a race condition in the http library,
21+
* see https://github.com/clue/php-buzz-react/issues/19
1922
*/
2023
class UnixConnector implements ConnectorInterface
2124
{
@@ -32,13 +35,17 @@ public function create($host, $port)
3235
{
3336
$deferred = new Deferred();
3437

35-
$resource = @stream_socket_client($this->path, $errno, $errstr, 1.0);
36-
37-
if (!$resource) {
38-
$deferred->reject(new RuntimeException('Unable to connect to unix domain socket path: ' . $errstr, $errno));
39-
} else {
40-
$deferred->resolve(new Stream($resource, $this->loop));
41-
}
38+
$path = $this->path;
39+
$loop = $this->loop;
40+
$this->loop->addTimer(0.001, function() use ($deferred, $path, $loop) {
41+
$resource = @stream_socket_client($this->path, $errno, $errstr, 1.0);
42+
43+
if (!$resource) {
44+
$deferred->reject(new RuntimeException('Unable to connect to unix domain socket path: ' . $errstr, $errno));
45+
} else {
46+
$deferred->resolve(new Stream($resource, $this->loop));
47+
}
48+
});
4249

4350
return $deferred->promise();
4451
}

0 commit comments

Comments
 (0)