PHP_STREAM_OPTION_READ_TIMEOUT is also used for socket write timeouts
Description
stream_set_timeout() passes PHP_STREAM_OPTION_READ_TIMEOUT to the stream wrapper:
The constant and its comment explicitly describe a read timeout:
However, the native socket stream stores this option in the single sock->timeout field:
That timeout is used by both php_sockop_read() and php_sockop_write(). In particular, a blocking write which encounters EAGAIN/EWOULDBLOCK waits for POLLOUT using the same timeout:
This creates an ambiguity for extension and alternative stream-wrapper implementers: following the constant name and header comment produces a read-only implementation, while matching native PHP socket stream behavior requires applying the value to both blocking reads and blocking writes.
This ambiguity caused a real compatibility issue in Swoole's coroutine stream hook, where stream_set_timeout() changed the read timeout but a blocked fwrite() continued using the previous write timeout:
https://github.com/swoole/swoole-src/pull/6260
The PHP manual currently says only that the function “sets the timeout value on the stream”; it does not document that native network/socket streams use the value for writes as well, and its example demonstrates only a read timeout:
https://github.com/php/doc-en/blob/master/reference/stream/functions/stream-set-timeout.xml
Suggested changes:
- Clarify the
stream_set_timeout()documentation: for native network/socket streams, the configured timeout applies to blocking reads and blocking writes. - Update the misleading comment in
main/php_streams.h. - Introduce a direction-neutral internal name such as
PHP_STREAM_OPTION_IO_TIMEOUTorPHP_STREAM_OPTION_TIMEOUT, while retainingPHP_STREAM_OPTION_READ_TIMEOUTas an alias for source compatibility, and migrate in-tree users to the clearer name. - Consider adding a php-src regression test covering a blocked socket-stream write after
stream_set_timeout().
No runtime behavior change is being requested here; the goal is to make the existing behavior unambiguous and prevent downstream implementations from interpreting the option differently.
Source: php/php-src