Bug #52602 [Asn->Csd]: fread is blocking even with the use of stream_select

From: Date: Sun, 16 Oct 2022 11:33:10 +0000
Subject: Bug #52602 [Asn->Csd]: fread is blocking even with the use of stream_select
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-242608@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=52602&edit=1 ID: 52602 Updated by: bukka@php.net Reported by: g at gsxm dot net Summary: fread is blocking even with the use of stream_select -Status: Assigned +Status: Closed Type: Bug Package: Sockets related Operating System: Gentoo x86_64 PHP Version: 5.2.14 -Assigned To: cataphract +Assigned To: bukka Block user comment: N Private report: N New Comment: This is fixed in master by https://github.com/php/php-src/commit/18fe337bae37913e3ce1001fd3896ed0d89116c5 and will be released in PHP 8.3. Previous Comments: ------------------------------------------------------------------------ [2014-07-18 12:18:42] rob at associatedtechs dot com I just got bit by this exact bug after spending a while trying to figure out what the heck was going on. PHP version 5.5.10-1, Debian x86_64. gauthierm's solution prevents fread() from blocking, but I can only get the first 8k chunk. stream_select() subsequently returns 0 even though there's data left in the stream. I understand why it does that, but this makes fread() and stream_select() impossible to reliably use together, which means non-blocking functions have to be used for TLS connections. ------------------------------------------------------------------------ [2013-01-06 09:23:34] gauthierm@php.net As another workaround, you can use the same chunk size in your read calls as the PHP stream chunk size. Then there is never buffered data that has not been consumed. In most cases the chunk size is 8192. In PHP 5.4 I think you can set the chunk size using stream_set_chunk_size(). This workaround allows you to continue using fread(). ------------------------------------------------------------------------ [2013-01-06 09:01:59] gauthierm@php.net This problem affected me while writing a WebSocket server in PHP. stream_select() returns streams ready for reading and fread() blocks when I try to read them. What is the point of stream_select() if it behaves like this? How are you supposed to write a blocking I/O loop? My solution was to use stream_socket_recvfrom() and give up on implementing SSL/TLS for my server (making me a bit sad inside). Since stream_select() returns the stream ready for reading, I would expect fread() to return the available bytes from the stream buffer in this case rather than blocking. See Bug 51056 for a long discussion on fread() behaviour with no conclusion. Can some conclusion be reached for PHP? Is there any actual evidence of people writing their servers in a way where fixing fread() will break? ------------------------------------------------------------------------ [2010-09-27 04:16:43] cataphract@php.net To sum up, the emulation behavior of stream_select is worse than useless: * If using stream_socket_recvfrom(), the buffer will be bypassed anyway, so the emulation behavior won't kick in. * If using fread() and there's something in the buffer, it will return the stream in the readfs set, which is documented "to see if characters become available for reading (more precisely, to see if a read will not block [...])", but a subsequent fread call will nevertheless eventually block when trying to fill the buffer. ------------------------------------------------------------------------ [2010-09-27 04:09:08] cataphract@php.net fread is not really adequate for this because of its buffering (see stream_socket_recvfrom()). Since you are specifying a length of 128 to fread, if you receive say, a 138 bytes long payload, the first call to fread will to read into the stream buffer the whole data, but will only return 128 bytes. The stream buffer remains with 10 bytes, but the socket buffer will be empty. On the second call to fread, it will try to read into the stream buffer again, but this time the socket buffer is empty, so it will block until it can read another packet or until the timeout. This appears, however, to be the desired behavior (perhaps fread could just return what it has in the buffer if it cannot read anything immediately, i.e., if it would otherwise block). So stream_select implements an emulation behavior that says the stream is readable without calling select if the there's data in the stream buffer. This is indeed odd, because the usual semantics of select are that it a read on a socket returned in the readfs set will not block. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=52602 -- Edit this bug report at https://bugs.php.net/bug.php?id=52602&edit=1

« previous php.bugs (#242608) next »