php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36216 timeout doesnt works in fsockopen
Submitted: 2006-01-30 20:29 UTC Modified: 2006-01-31 16:10 UTC
From: ipmax at freesurf dot fr Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 5.1.2 OS: debian
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: ipmax at freesurf dot fr
New email:
PHP Version: OS:

 

 [2006-01-30 20:29 UTC] ipmax at freesurf dot fr
Description:
------------
fsockopen hang if remote server doesnt reply, like no dns, or server down, or firewall drop paquets. 
Timeout only works if remote server reply  something
if a firewall drop paquets, then timeout wont work, and fsockopen hang forever.
Same problem on socket_server

This is a big problem, that is why many people see theyr script hanging when using fsockopen.
I v seen this bug on redhat php 4, and debian php 5


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-30 20:47 UTC] tony2001@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.


Works fine here.
 [2006-01-30 21:04 UTC] tony2001@php.net
>Hello,
>i got the bug of fsockopen no timeout
>on different debian and latest php
>you say it works perfectly, did you tried with a blocked >adress or non existant domain ?
Yes, I did.

>did you got it to work on debian ?
No, since I don't use debian and have no chance to test it there. So if you're able to provide some more information - please provide it.
And please use this system instead of mailing privately so we can track the things.
 [2006-01-31 04:33 UTC] ipmax at freesurf dot fr
This echo "connected," and hangs for ever, timout doesnt works :
---------------------------------
$scfp=fsockopen("www.microsoft.com", 111,&$errno,&$errstr,5);
if(!$scfp) {echo "error"; exit;
} else {
echo "connected,";
stream_set_timeout($scfp,2);
fputs($scfp,"GET /admin.cgi\r\n\r\n");

while(!feof($scfp)) {$page .= fgets($scfp);   }
fclose($scfp);

//////////////////////

This works, considering timout is checked in the fgets loop. stream_set_timeout() replace the bug of fsockopen that doesnt timeout.

$scfp=fsockopen("www.microsoft.com", 111,&$errno,&$errstr,2);
if(!$scfp) {echo "error"; exit;
} else {
echo "connected,";
stream_set_timeout($scfp,2);
fputs($scfp,"GET /admin.cgi\r\n\r\n");

while(!feof($scfp)) {$page .= fgets($scfp); 
 $info = stream_get_meta_data($scfp);
 if ($info['timed_out']) {  echo "timeout"; exit; }
  }
fclose($scfp);

---------------------
PHP 5.1.1-1.dotdeb.2 (cli) (built: Dec  1 2005 12:46:40)
 [2006-01-31 04:40 UTC] ipmax at freesurf dot fr
we are talking here of a fsockopen connection attempt to a non responding remote server, like a server with DROP iptable firewall. Noteven responding error or 404, just not responding at all.
fsockopen never timeout, fgets never find the EOF.
=> Hang.
 [2006-01-31 04:45 UTC] ipmax at freesurf dot fr
did a mistake in the first post, here is the way to understand. Timeout works only if add stream_set_timeout() to the inloop stream. Why the fsokopen timeout doesnt timeout ? no idea.

--------------------------------

This echo "connected," and hangs for ever, timout doesnt works :
---------------------------------
$scfp=fsockopen("www.microsoft.com", 111,&$errno,&$errstr,5);
if(!$scfp) {echo "error"; exit;
} else {
echo "connected,";
fputs($scfp,"GET /admin.cgi\r\n\r\n");

while(!feof($scfp)) {$page .= fgets($scfp);   }
fclose($scfp);

//////////////////////

This works, considering timout is checked in the fgets loop.
stream_set_timeout() replace the bug of fsockopen that doesnt timeout.

$scfp=fsockopen("www.microsoft.com", 111,&$errno,&$errstr,2);
if(!$scfp) {echo "error"; exit;
} else {
echo "connected,";
stream_set_timeout($scfp,2);
fputs($scfp,"GET /admin.cgi\r\n\r\n");

while(!feof($scfp)) {$page .= fgets($scfp); 
 $info = stream_get_meta_data($scfp);
 if ($info['timed_out']) {  echo "timeout"; exit; }
  }
fclose($scfp);

---------------------
PHP 5.1.1-1.dotdeb.2 (cli) (built: Dec  1 2005 12:46:40)
 [2006-01-31 04:50 UTC] ipmax at freesurf dot fr
Please use this adress to test :

$scfp=fsockopen("generation.elliptic.fr", 8060,&$errno,&$errstr,1);

I dont know what firewall there is, but sometimes, my demo is correct, on microsoft it is not.
 [2006-01-31 04:52 UTC] ipmax at freesurf dot fr
You can delete previous post, here is a good example :

This echo "connected," and hangs for ever, timout doesnt works :
---------------------------------
$scfp=fsockopen("generation.elliptic.fr", 8060,&$errno,&$errstr,5);
if(!$scfp) {echo "error"; exit;
} else {
echo "connected,";
fputs($scfp,"GET /admin.cgi\r\n\r\n");

while(!feof($scfp)) {$page .= fgets($scfp);   }
fclose($scfp);

//////////////////////

This works, considering timout is checked in the fgets loop.
stream_set_timeout() replace the bug of fsockopen that doesnt timeout.

$scfp=fsockopen("generation.elliptic.fr", 8060,&$errno,&$errstr,2);
if(!$scfp) {echo "error"; exit;
} else {
echo "connected,";
stream_set_timeout($scfp,2);
fputs($scfp,"GET /admin.cgi\r\n\r\n");

while(!feof($scfp)) {$page .= fgets($scfp); 
 $info = stream_get_meta_data($scfp);
 if ($info['timed_out']) {  echo "timeout"; exit; }
  }
fclose($scfp);

---------------------
PHP 5.1.1-1.dotdeb.2 (cli) (built: Dec  1 2005 12:46:40)
 [2006-01-31 07:50 UTC] tony2001@php.net
Please try using this CVS snapshot:

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


 [2006-01-31 12:07 UTC] ipmax at freesurf dot fr
thanks, i already got 5.1
i can't reinstall php now, but this bug as been reported many times, since years, so i dont think it is debug today.
 [2006-01-31 12:32 UTC] tony2001@php.net
Works fine on different Fedora Core and SuSE versions, AIX, Solaris and FreeBSD, hence the problem is not in PHP itself, but somewhere in the underlying system libraries.
Please report this issue to Debian developers.
 [2006-01-31 14:23 UTC] ipmax at freesurf dot fr
Try this one, it reply connected, but hangs for ever.
Tested on Debian and redhat. Same Result.

$scfp=fsockopen("generation.elliptic.fr", 8060,&$errno,&$errstr,5);
if(!$scfp) {echo "error"; exit;
} else {
echo "connected,";
fputs($scfp,"GET /admin.cgi\r\n\r\n");

while(!feof($scfp)) {$page .= fgets($scfp);   }
}
fclose($scfp);
 [2006-01-31 14:40 UTC] tony2001@php.net
Host generation.elliptic.fr is alive and fsockopen() succeeds for me.
But with that's what I get with sf.net, for example:
# time ./sapi/cli/php -r 'var_dump(fsockopen("sf.net", 8060,&$errno,&$errstr,5));'

Warning: fsockopen(): unable to connect to sf.net:8060 (Connection timed out) in Command line code on line 1
bool(false)

real    0m5.257s
user    0m0.007s
sys     0m0.002s

 [2006-01-31 16:10 UTC] ipmax at freesurf dot fr
Right, same for me :
-------------------------
ns55:~# time ./sapi/cli/php -r 'var_dump(fsockopen("sf.net",
> 8060,&$errno,&$errstr,5));'
-bash: ./sapi/cli/php: No such file or directory

real    0m0.004s
user    0m0.010s
sys     0m0.000s
ns55:~# time php -r 'var_dump(fsockopen("sf.net",8060,&$errno,&$errstr,5));'

Warning: fsockopen(): unable to connect to sf.net:8060 (Connection timed out) in                                                                              Command line code on line 1
bool(false)

real    0m5.107s
user    0m0.080s
sys     0m0.020s
ns55:~# time php -r 'var_dump(fsockopen("generation.elliptic.fr",8060,&$errno,&$                                                                             errstr,5));'
resource(4) of type (stream)

real    0m0.084s
user    0m0.060s
sys     0m0.020s
-----------------------------------------

Does it mean, the problem is after the fsockopen ?
Fsockopen reach host, so it says ok, but dont care if server send something back, then the feof stay stuck as it doesnt find EOF ? Maybe this is it ?
in the :
while(!feof($scfp)) {$page .= fgets($....
As you see my script, this one hangs in the while loop :

<?
$scfp=fsockopen("generation.elliptic.fr", 8060,&$errno,&$errstr,5);
if(!$scfp) {echo "error"; exit;
} else {
echo "connected,";
fputs($scfp,"GET /admin.cgi\r\n\r\n");

while(!feof($scfp)) {$page .= fgets($scfp);   }
}
fclose($scfp);
?>
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Jan 18 08:03:18 2022 UTC