Skip to content

socket_select(): document key preservation, and correct both select pages - #5829

Merged
lacatoire merged 3 commits into
php:masterfrom
lacatoire:doc/socket-select-keys
Sep 3, 2026
Merged

socket_select(): document key preservation, and correct both select pages#5829
lacatoire merged 3 commits into
php:masterfrom
lacatoire:doc/socket-select-keys

Conversation

@lacatoire

@lacatoire lacatoire commented Sep 3, 2026

Copy link
Copy Markdown
Member

socket_select() already warns that the arrays are modified on exit, but not that an entry that stays keeps the key it came in with. That is what callers need in order to map a ready socket back to whatever it belongs to. php_sock_array_from_fd_set() reinserts each surviving entry under its original key, integer or string. Checked on 8.5.4 with non-contiguous integer keys, string keys and mixed arrays, for read as well as write. No changelog: keys have been preserved since 5.3.0.

The second commit fixes what the surrounding twenty lines got wrong. The page said the arrays could be left out; they cannot, the function takes at least four arguments and ValueErrors when all three are empty. The microseconds entry was an empty paragraph, and the notes carried a sentence that never parsed.

The third one is about stream_select(), which is where this started: its page says its operation is equivalent to socket_select() apart from what it acts on, and that is not true. A negative timeout is a ValueError on one side and a warning plus false on the other, an invalid element is skipped on one side and a TypeError on the other, and microseconds is nullable only on the stream side. The one worth documenting had no mention anywhere: when a stream already holds buffered data on the PHP side, select() is never called and the write and except arrays come back empty without having been looked at, so a caller watching for writability silently gets nothing.

The two pages now point at each other in seealso. Paragraphs that are touched become simpara where the style check asks for it.

Fixes: #5787

The page already warns that the arrays are modified on exit, but not that
an entry that stays keeps the key it came in with, which is what callers
need in order to map a ready socket back to whatever it belongs to.
stream_select() documents it; php_sock_array_from_fd_set() reinserts each
surviving entry under its original key, integer or string, so the two
functions agree.

Fixes: php#5787
The page said the arrays could be left out. They cannot: the function
takes at least four arguments, an uninteresting one has to be an empty
array or null, and all three empty raises a ValueError. The microseconds
entry was an empty paragraph, and the notes carried a sentence that never
parsed ("If you read/write to a socket returns in the arrays").

Also stream_select() in seealso, since the two functions are each other's
counterpart, and the usual typos: happens.On, zero , causing, Due a
limitation, socket_select return.
The page claimed the two functions operate identically apart from what
they act on. They do not. A negative timeout is a ValueError here and a
warning plus false there, an invalid element is skipped here and a
TypeError there, and microseconds is nullable only here.

The larger difference had no mention anywhere: when a stream already has
buffered data on the PHP side, select() is never called, and the write
and except arrays come back empty without having been looked at. A
caller watching for writability silently gets nothing.

Also socket_select() in seealso, and the same unparseable sentence about
partial reads that the socket_select() page carries.
@lacatoire lacatoire changed the title socket_select(): say that the array keys are preserved socket_select(): document key preservation, and correct both select pages Sep 3, 2026
@lacatoire
lacatoire merged commit 3e6082f into php:master Sep 3, 2026
2 checks passed
@lacatoire
lacatoire deleted the doc/socket-select-keys branch September 3, 2026 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

socket_select() missing note that array keys are preserved

1 participant