php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33471 socket_read PHP_NORMAL_READ blocks socket
Submitted: 2005-06-25 07:36 UTC Modified: 2005-07-04 01:00 UTC
Votes:16
Avg. Score:4.2 ± 1.1
Reproduced:13 of 13 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (7.7%)
From: info at goldenelite dot com Assigned:
Status: No Feedback Package: Sockets related
PHP Version: 5.0.4 OS: FreeBSD 4.10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: info at goldenelite dot com
New email:
PHP Version: OS:

 

 [2005-06-25 07:36 UTC] info at goldenelite dot com
Description:
------------
The socker_read function has two modes: PHP_BINARY_READ and PHP_NORMAL_READ. The latter is useful to get lines instead of raw data. But using it will result in a blocked socket, regardless of socket_set_nonblock() has been used.

I'm not sure this is a bug. If it is not, the manual page should mention that the socket_read function would block regardless of blocking mode if the PHP_NORMAL_READ parameter is used.

Reproduce code:
---------------
// short example
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($socket);
socket_connect($socket, $host, $port);
$data = socket_read($socket, 512, PHP_NORMAL_READ);

Expected result:
----------------
Let's assume no data is transmitted by the remote host, thus no data is to be received. Since we set non-blocking mode, the socket_read function should produce a socket error very quickly and the script execution continues.

Actual result:
--------------
Instead of continuing script execution, the socket_read function blocks the further execution, waiting for data to be read until its buffer is full. Thus it shows the effect of blocking mode, even though we explicitly set to non blocking mode.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-26 13:14 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-07-04 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2007-08-20 00:04 UTC] piro at pirocast dot net
bug still exists as of PHP 5.2.4
 [2009-09-11 05:19 UTC] alonsoalonsocr at yahoo dot com
this bug exists in 5.2.9 on Linux (either documentation has still not been fixed, or function does not respect blocking or timout setting. I should add that setting a read timeout with socket_set_option has no effect, it still blocks forever when using PHP_NORMAL_READ
 [2011-01-06 22:55 UTC] bneff84 at gmail dot com
I've just encountered this with PHP 5.3.4 running as FastCGI on Apache in CentOS.

After consulting the documentation in light of finding this bug report I do believe the documentation covers why this is happening, but I do believe it would still be considered a bug.

Doc says: PHP_NORMAL_READ - reading stops at \n or \r.

I think technically this means regardless of what you enter as the second parameter, the function will not stop reading until it encounters a newline character. This is probably why it appears to be "blocking" even when set to nonblocking because it is attempting to read until a newline is encountered.

Technically this would still be a bug IMO because the expected behavior of nonblocking socket reading is that it should immediately return whatever it found regardless of if it filled it's "buffer" by finding the line end.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Jan 25 03:03:35 2022 UTC