Skip to content

Commit 785dadb

Browse files
authored
Merge pull request #43 from hollodotme/fast-cgi-client-41
Add proper handling of stream_select return value
2 parents f09beca + b3a37ac commit 785dadb

File tree

6 files changed

+340
-20
lines changed

6 files changed

+340
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a CH
55

66
## [2.7.2] - YYYY-MM-DD
77

8+
### Improved
9+
10+
* Handling of `stream_select` returning `false` in case of a system call interrupt. - [#41]
11+
812
### Fixed
913

1014
* Remove/close sockets after fetching their responses triggered async requests in order to prevent halt on further
@@ -230,3 +234,4 @@ Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](http
230234
[#33]: https://github.com/hollodotme/fast-cgi-client/pull/33
231235
[#37]: https://github.com/hollodotme/fast-cgi-client/issue/37
232236
[#40]: https://github.com/hollodotme/fast-cgi-client/issue/40
237+
[#41]: https://github.com/hollodotme/fast-cgi-client/issue/41

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"keywords": [
55
"fastcgi",
66
"php-fpm",
7-
"socket"
7+
"socket",
8+
"async"
89
],
910
"minimum-stability": "dev",
1011
"prefer-stable": true,

src/Client.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ public function waitForResponses( ?int $timeoutMs = null ) : void
172172

173173
while ( $this->hasUnhandledResponses() )
174174
{
175-
foreach ( $this->getSocketsHavingResponse() as $socket )
176-
{
177-
$this->fetchResponseAndNotifyCallback( $socket, $timeoutMs );
178-
}
175+
$this->handleReadyResponses( $timeoutMs );
179176
}
180177
}
181178

@@ -209,18 +206,6 @@ public function hasUnhandledResponses() : bool
209206
return $this->sockets->hasBusySockets();
210207
}
211208

212-
/**
213-
* @return Generator|Socket[]
214-
* @throws ReadFailedException
215-
*/
216-
private function getSocketsHavingResponse() : Generator
217-
{
218-
foreach ( $this->getRequestIdsHavingResponse() as $requestId )
219-
{
220-
yield $this->sockets->getById( $requestId );
221-
}
222-
}
223-
224209
/**
225210
* @param int $requestId
226211
*
@@ -246,7 +231,12 @@ public function getRequestIdsHavingResponse() : array
246231
$reads = $this->sockets->collectResources();
247232
$writes = $excepts = null;
248233

249-
stream_select( $reads, $writes, $excepts, 0, Socket::STREAM_SELECT_USEC );
234+
$result = @stream_select( $reads, $writes, $excepts, 0, Socket::STREAM_SELECT_USEC );
235+
236+
if ( false === $result || 0 === count( $reads ) )
237+
{
238+
return [];
239+
}
250240

251241
return $this->sockets->getSocketIdsByResources( $reads );
252242
}

src/Sockets/Socket.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@
5252
use function stream_select;
5353
use function stream_set_timeout;
5454
use function stream_socket_client;
55+
use function stream_socket_shutdown;
5556
use function strlen;
5657
use function substr;
58+
use const STREAM_SHUT_RDWR;
5759

5860
final class Socket
5961
{
@@ -541,6 +543,7 @@ private function disconnect() : void
541543
{
542544
if ( is_resource( $this->resource ) )
543545
{
546+
@stream_socket_shutdown( $this->resource, STREAM_SHUT_RDWR );
544547
fclose( $this->resource );
545548
}
546549
}

0 commit comments

Comments
 (0)