|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24066 socket_read() in PHP_NORMAL_READ exits abnormally
Submitted: 2003-06-06 14:36 UTC Modified: 2003-07-27 14:32 UTC
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:3 (75.0%)
From: jason at superlink dot net Assigned:
Status: No Feedback Package: Sockets related
PHP Version: 4.3.2 OS: FreeBSD 4.8
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2003-06-06 14:36 UTC] jason at superlink dot net
I'm running a socket connection that needs NORMAL_READ mode enabled.

Here is a sample of the socket connection:

        $r = array($socket); 
        if(socket_select($r, $w, $except = NULL, 0)) { 
                if($buffer = socket_read($socket, 2048, PHP_NORMAL_READ)) { 
                        dostuff($data);// do something with the data. 
                } else { 
                        // for some reason my socket connection FAILS a lot. 
                        die("ERROR: failed to read socket to $remotehost"); 
        } else { 

In the event of an error, I've been logging the error: 

The error for the last 5 attempts has returned: 
  uptime: 211 Unknown error: 0 
  uptime: 439 Unknown error: 0 
  uptime: 275 Unknown error: 0 
  uptime: 279 Unknown error: 0 
  uptime: 395 Unknown error: 0  

The socket connection doesn't seem to want to stay alive for long. The error message seems.... very unfriendly. I've tried the program in binary mode (while my program doesn't function with binary mode on, it can still download and read from the socket). In binary mode, the problem does not occur.

I've tried NORMAL_READ with and without socket blocking, with the same results (unexplained socket error).

this problem seems to have been happening in php 4.3.1 too.

Configure Command  './configure' '--prefix=/usr/local' '--with-apache=/home/jason/apache_1.3.27' '--enable-exif' '--enable-track-vars' '--with-calendar=shared' '--enable-magic-quotes' '--enable-trans-sid' '--enable-wddx' '--enable-sockets' '--disable-debug' '--enable-gd-native-tt' '--with-zlib' '--enable-inline-optimization' '--enable-memory-limit' '--with-mysql=/usr/local'  


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-06 21:50 UTC] jason at superlink dot net
Okay, a few things to add to my post...

I wanted to add that my initial assessment that binary_read did not suffer from this issue is false.

In my inital test with binary read, I thought that simply watching the socket download data was enough to validate if the issue was with normal_read. There was one glaring diff between normal_read and binary_read -- my software was only able to properly process input data from normal_read. After a bit of tweaking, I made binary_read also function and properly parse input. Once it was processing input, it too had socket disconnects. Without processing input, it's simply reading in data, with processing, it has to check data with a RDBMs, which causes a slight delay between reads.

Perhaps this bit of info can help narrow down the problem.

Also, I wanted to add that I read the bug report about socket_read() returning an infinate number of "\n" ... (, that bug is still present in 4.3.2. I had to write a shell script to watch for cpu use over 40% and kill the process. It seems to happen around ~5% of the time.
 [2003-07-21 10:11 UTC]
Can you try your example with  var_dump($buffer) in your code? 
socket_read returns boolean(FALSE) on an actual error, and empty string "" when the connecting system has closed the connection. The connection closed is not considered and error, and thus socket_last_error() is 0 (no error)

If you don't like this behaivior there is socket_recv, which returns the length of the message returned(0 = connection close), and takes a buf argument of the string, but it does not have the PHP_NORMAL_READ capibility.

If this is the problem you should recode tests of socket_read like so

if (($buffer = socket_read($socket, 2048, xxx)) === FALSE) {
    // Error has occured
} else if (empty($buffer)) {
    // client disconnected
} else {
    // do something with data


 [2003-07-27 14:32 UTC]
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 03:01:28 2024 UTC