@@ -85,7 +85,18 @@ private function handles($sockets) {
8585 }
8686
8787 /** Maps handles -> socket */
88- private function sockets ($ handles , $ sockets ) {
88+ private function search ($ handles , $ sockets ) {
89+ $ r = [];
90+ foreach ($ sockets as $ key => $ socket ) {
91+ if (false !== array_search ($ socket ->getHandle (), $ handles , true )) {
92+ $ r [$ key ]= $ socket ;
93+ }
94+ }
95+ return $ r ;
96+ }
97+
98+ /** Maps handles -> socket */
99+ private function map ($ handles , $ sockets ) {
89100 $ r = [];
90101 foreach ($ handles as $ key => $ _ ) {
91102 $ r [$ key ]= $ sockets [$ key ];
@@ -125,10 +136,19 @@ public function select(&$read, &$write, &$except, $timeout= null) {
125136 // Call "raw" select on handles
126137 $ n = $ this ->select0 ($ r , $ w , $ e , $ timeout );
127138
128- // Map handles to sockets
129- $ read = null === $ r ? null : $ this ->sockets ($ r , $ read );
130- $ write = null === $ w ? null : $ this ->sockets ($ w , $ write );
131- $ except = null === $ e ? null : $ this ->sockets ($ e , $ except );
139+ // Map handles to sockets. For PHP < 7.2, use less-performant search-based lookup
140+ // as stream_select() sometimes (?!) doesn't preserve the keys even though PHP bug
141+ // #53427 was fixed in PHP 5.4.1, see https://bugs.php.net/bug.php?id=53427 and
142+ // https://github.com/php/php-src/commit/22d461df621a1c059800a50c9d5c8bba41a14f16
143+ if (PHP_VERSION_ID < 70200 ) {
144+ $ read = null === $ r ? null : $ this ->search ($ r , $ read );
145+ $ write = null === $ w ? null : $ this ->search ($ w , $ write );
146+ $ except = null === $ e ? null : $ this ->search ($ e , $ except );
147+ } else {
148+ $ read = null === $ r ? null : $ this ->map ($ r , $ read );
149+ $ write = null === $ w ? null : $ this ->map ($ w , $ write );
150+ $ except = null === $ e ? null : $ this ->map ($ e , $ except );
151+ }
132152
133153 return $ n ;
134154 }
0 commit comments