Doc #71951 [Com]: stream_socket_shutdown(..., STREAM_SHUT_RD) will not shutdown read on Linux
| From: | grzegorz129 at gmail dot com | Date: | Fri, 08 Apr 2016 03:08:28 +0000 |
| Subject: | Doc #71951 [Com]: stream_socket_shutdown(..., STREAM_SHUT_RD) will not shutdown read on Linux | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-13392@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=71951&edit=1
ID: 71951
Comment by: grzegorz129 at gmail dot com
Reported by: grzegorz129 at gmail dot com
Summary: stream_socket_shutdown(..., STREAM_SHUT_RD) will not
shutdown read on Linux
Status: Closed
Type: Documentation Problem
Package: Streams related
Operating System: Linux
PHP Version: 7.0.5
Assigned To: krakjoe
Block user comment: N
Private report: N
New Comment:
After digging in old Linux list archives I found the suspect - in fact it's how Linux handles
(or actually not handles at all) shutdown(sock, SHUT_RD): http://oss.sgi.com/archives/netdev/2003-07/msg00692.html
- it's a pretty interesting discussion about this particular issue.
Maybe, since PHP is trying to be platform-independent, interpreter can unify this behavior and
discard data on recv() when socket/stream was closed for reading?
Previous Comments:
------------------------------------------------------------------------
[2016-04-08 02:22:33] grzegorz129 at gmail dot com
This documentation not is quite nice, maybe not 100% clear for beginners but anyway - it's
somewhat weird how it's actually working.
Let's take a look at this code:
<?php
$server = stream_socket_server('tcp://127.0.0.1:9999');
$clientOnClient = stream_socket_client('tcp://127.0.0.1:9999');
$clientOnServer = stream_socket_accept($server, 0.5);
var_dump(stream_set_chunk_size($clientOnServer, 1));
var_dump(stream_set_read_buffer($clientOnServer, 1));
fwrite($clientOnClient, 'ping');
//This now works as defined in documentation
var_dump(fread($clientOnServer, 2));
stream_socket_shutdown($clientOnServer, STREAM_SHUT_RD);
var_dump(fread($clientOnServer, 3));
//This should not work since buffer is empty at call
stream_socket_shutdown($clientOnServer, STREAM_SHUT_RD);
fwrite($clientOnClient, 'still-broken');
var_dump(fread($clientOnServer, 12));
//...and iterestingly it works as expected if you use RDWR or WR
stream_socket_shutdown($clientOnServer, STREAM_SHUT_WR);
fwrite($clientOnClient, 'or-is-it');
var_dump(fread($clientOnServer, 8));
Since buffer is closed and in fact event emptied why it's still receiving data on Linux? ;)
To be entirely sure I've also tested it using PHP server on the one side and telnet at the
other - stream still receives data. This behavior is also present while using sockets.
<?php
$server = stream_socket_server('tcp://127.0.0.1:9999');
$clientOnServer = stream_socket_accept($server, 10);
stream_socket_shutdown($clientOnServer, STREAM_SHUT_RD);
while(1) {
sleep(1);
var_dump(fread($clientOnServer, 1));
}
------------------------------------------------------------------------
[2016-04-04 08:59:15] krakjoe@php.net
The fix for this bug has been committed.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
For Windows:
http://windows.php.net/snapshots/
Thank you for the report, and for helping us make PHP better.
------------------------------------------------------------------------
[2016-04-04 08:58:45] krakjoe@php.net
Automatic comment from SVN on behalf of krakjoe
Revision: http://svn.php.net/viewvc/?view=revision&revision=338908
Log: Fix #71951: shutdown behaviour note
------------------------------------------------------------------------
[2016-04-04 07:53:58] krakjoe@php.net
On OSX (BSD) the kernel space read buffer is emptied when the socket is shutdown.
On Linux, it only rejects further packets (so the other side cannot write).
A note in the documentation would be nice.
------------------------------------------------------------------------
[2016-04-04 03:34:18] grzegorz129 at gmail dot com
Edit: "Expected result" was accidentally switched with "Actual result".
------------------------------------------------------------------------
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=71951
--
Edit this bug report at https://bugs.php.net/bug.php?id=71951&edit=1