php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40750 fsockopen timeout parameter overwrites timeout for reading
Submitted: 2007-03-07 18:29 UTC Modified: 2007-03-14 19:22 UTC
From: andreas dot rieber at 2e-systems dot com Assigned: tony2001 (profile)
Status: Closed Package: Network related
PHP Version: 5.2.1 OS: OpenSuse, Ubuntu, Redhat
Private report: No CVE-ID: None
 [2007-03-07 18:29 UTC] andreas dot rieber at 2e-systems dot com
Description:
------------
The timeout parameter of fsockopen overwrites the general read/write timeout. Without the timeout parameter in fsockopen or if you use stream_set_timeout after fsockopen it works.

I created scipt a.php which opens b.php. the connection has a timeout of 5 seconds. b.php sleeps for 7 seconds. The first fgets fails and stream_get_meta_data shows that it timed out.

It works with php 5.2.0.


Reproduce code:
---------------
code a.php

<?php

$fp = @fsockopen( 'localhost', 80, &$errno, &$errstr, 5);
if( !$fp) die( "$errno: $errstr");

fwrite( $fp, "GET /b.php HTTP/1.0\r\nHost: localhost\r\n\r\n");

// read result
$data = '';
while( !feof( $fp)) {
  $line = fgets( $fp, 8192);
  if( trim( $line) == '')
    break;

  $data .= $line;
}

fclose( $fp);

// check header
if( eregi( '^HTTP\/[0-9\.]+ ([0-9]{3}) .*', $data, $reg)) {
  echo "Header OK";
} else {
  echo "Header failed";
}

?>

code b.php

<?php

sleep( 7);

?>


Expected result:
----------------
Header OK

Actual result:
--------------
Header failed

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-07 18:41 UTC] tony2001@php.net
I get "Header OK".
Also I don't understand what the expected result has to do with the timeout.

 [2007-03-07 20:00 UTC] andreas dot rieber at 2e-systems dot com
I want to use the default connection timeout for read/write but a shorter timeout to connect (here 5 seconds). What happens is that the first fgets gets also the 5 seconds timeout and so my header is empty.

The manual says for fsockopen:

 Note: If you need to set a timeout for reading/writing data over the
 socket, use stream_set_timeout(), as the timeout parameter to
 fsockopen() only applies while connecting the socket.

and thats absolut ok and what i want. It worked with php 5.2.0 and before but not with 5.2.1.
 [2007-03-07 21:59 UTC] tony2001@php.net
>The manual says for fsockopen:
> Note: If you need to set a timeout for reading/writing data over the
> socket, use stream_set_timeout(), as the timeout parameter to
> fsockopen() only applies while connecting the socket.

That's still correct, there were no changes to this behavior.
 [2007-03-08 13:32 UTC] andreas dot rieber at 2e-systems dot com
I tried now also the latest snapshoot (php5.2-200703081130) and double checked my default_socket_timeout which is 60. The fgets gets the timeout from fsockopen.
 [2007-03-09 10:30 UTC] tony2001@php.net
Not reproducible.
 [2007-03-11 14:51 UTC] andreas dot rieber at 2e-systems dot com
we reproduced it on second OS and wrote better test:

--TEST--
Bug #40750 (default_socket_timeout Test)
--FILE--
<?php

ini_set( 'default_socket_timeout', 5);

$fp = fsockopen( 'www.php.net', 80, &$errno, &$errstr, 3);
$start = time();
$data = fread( $fp, 8192);
fclose( $fp);

echo time() - $start;

?>
--EXPECT--
5
 [2007-03-12 05:24 UTC] judas dot iscariote at gmail dot com
Im experiencing a very similar problem like this, probably not the same but related, that makes our application stop working after upgrading from php 5.2.0 to 5.2.1 all previuos versions do the job fine,

I 'll try to isolate the problem better.
 [2007-03-12 09:34 UTC] tony2001@php.net
What am I doing wrong?
# ./sapi/cli/php /tmp/5.php
5

# cat /tmp/5.php
<?php

ini_set( 'default_socket_timeout', 5);

$fp = fsockopen( 'www.php.net', 80, &$errno, &$errstr, 3);
$start = time();
$data = fread( $fp, 8192);
fclose( $fp);

echo time() - $start;

?>

 [2007-03-12 12:59 UTC] andreas dot rieber at 2e-systems dot com
Now tested also redhat with same problem.

There is nothing special done:
tar xvzf php-5.2.1.tar.gz
cd php-5.2.1
./configure --enable-track-vars --with-mysql=/usr/local/mysql --enable-trans-sid --with-apxs2=/usr/local/apache2/bin/apxs --with-mcrypt --with-zlib-dir=/usr/lib/ --with-gd --enable-exif --enable-soap --with-openssl --with-xmlrpc --enable-sockets
make

thats it...
 [2007-03-12 13:15 UTC] tony2001@php.net
Remove all the configure options not related to the problem and figure out what is the minimal list of options required to reproduce it.
Try with PHP CLI, with --enable-debug etc.
I still have no idea how to replicate it so far.
 [2007-03-12 14:32 UTC] andreas dot rieber at 2e-systems dot com
OK, openssl is causing the problem. So

./configure --with-openssl

will do it.
 [2007-03-12 15:16 UTC] tony2001@php.net
Ok, that helped.
I have a patch, but I need to discuss it with the extension maintainer first.
 [2007-03-12 18:12 UTC] judas dot iscariote at gmail dot com
YUp, I confirm our code start to work again when openssl extension is not loaded.
 [2007-03-14 19:22 UTC] tony2001@php.net
This bug has been fixed in CVS.

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/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC