|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12360 fsockopen timeout doesn't work
Submitted: 2001-07-25 07:08 UTC Modified: 2004-01-06 20:53 UTC
Avg. Score:4.7 ± 0.7
Reproduced:22 of 22 (100.0%)
Same Version:9 (40.9%)
Same OS:8 (36.4%)
From: sheep at fearthisclan dot com Assigned:
Status: No Feedback Package: Sockets related
PHP Version: 4.3.2 OS: FreeBSD
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2001-07-25 07:08 UTC] sheep at fearthisclan dot com
When I try to connect to a port on a server that isn't reachable or the packets are dropped the timeout doesn't work...

This worked when I was using PHP 4.0.4pl1


echo "opening socket... \n";
fsockopen("", 12, $errno, $errstr, 5.0);
echo "$errno\n$errstr\n";
echo "done!\n";


I have also tried this on a Red Hat 7.1 machine and it's just the samee there...

My configure line: './configure' '--with-zlib-dir=/usr/lib' '--with-png-dir=/usr/local/lib' '--with-gd=/usr/local' '--with-ttf=/software/freetype' '--with-apxs=/software/web/bin/apxs' '--with-mysql=/software/mysql/'


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-25 09:34 UTC]
I have reproduced this error.

When requesting an valid address, but a port that the server
does not listen on, the script hangs.


 [2002-12-11 19:00 UTC]
Verified on BSD boxes only:
BSDI 4.3
FreeBSD 4.6.2-RELEASE-p3
also tested but not reproduced on:
SuSE 7.0 (Linux 2.2.16)

It's interesting to note that the *bsd boxes have IPv6 support while the linux and xp boxes do not.
I'm waiting for some more feedback on some other boxes.

I wonder if this is an IPv6 problem?
 [2002-12-22 08:02 UTC]
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at
In case this was a documentation problem, the fix will show up soon at

In case this was a website problem, the change will show
up on the site and on the mirror sites in short time.
Thank you for the report, and for helping us make PHP better.

Fixed (will be in 4.3.0).

The problem was related to IPv6 support; specifically, if getaddrinfo returned multiple addresses, PHP would try each in turn, waiting up to the full timeout for each.
Now, we abort the loop if a connection times out.
 [2002-12-22 09:59 UTC]
An additional note:

Under BSDi and threaded builds, the timeout isn't respected. Additionally the openssl test, takes much longer, which leads me to believe, this is a BSDi threading issue, to do with time values. This issue doesn't surface, when --with-experimental-zts is removed from the equation and can't be reproduced on FreeBSD.
 [2003-09-16 04:53 UTC] uros at sir-mag dot com
I'm using PHP 4.3.3 on FreeBSD and also have problems with this fsockopen

It seams that no timeout is using, because simple script takes about 1 minute to finish with error.

$fp = @fsockopen ('', 80, $errno, $errstr, 1.0);

Error is:
22 Invalid argument

And it takes about 1 minute. What about 1.0 second I set.

If i use regular system call like dig or nslookup It only takes about 10s for this host.

I have same problems with function checkdnsrr and gethostbyname
 [2003-09-16 05:25 UTC]
Can you guys that still experience this problem try recompiling PHP using --disable-ipv6 and see if that "solves" the problem?
 [2003-09-20 07:46 UTC] johannes at zoomposters dot com
I recompiled using --disable-ipv6. Still doesn't work.

For a certain host, I get Error 11, "Resource temporarily unavailable" and the timeout arg has no effect.
 [2003-12-02 01:57 UTC] sheep at fearthisclan dot com
I am having this problem as well now.  My host is running FreeBSd.  Here is the info page that shows what they have compiled and everything.
The script im using this for queries a bunch of game servers.  I was having a problem with the socket hanging on servers that were up.  The socket would wait(hang) even when the information was all sent.  I worked around this using socket_get_status (when it's zero I can quit waiting), but if a server is down from the get go my script hangs forever it seems.  If anyone has found a fix for this problem yet I'd appreciate the help.

Thank you,
 [2003-12-22 13:45 UTC] sheep at fearthisclan dot com
Maybe I can be a little more specific about the behavior.  Whenever my program tries to query a server that is down, the socket seems to prevent php from doing anything at all.  The max_execution_time of 30 seconds does not even step in and give a fatal error after 30 seconds of running.  If there has been a fix or if you know around this I'd really like to know.  I'm not sure how willing these guys will be to use a CVS or not, but if you tell me its fixed in one of them I could try and ask them to.
 [2003-12-22 14:21 UTC]
Try adjusting the default_socket_timeout ini setting,
which defaults to 60 seconds.

ini_set('default_socket_timeout', 10);

(I know you shouldn't need to do this, but it will
help me to figure out what is going on)
 [2003-12-22 16:03 UTC] sheep at fearthisclan dot com
It doesn't seem to have any effect on it.  I tried 10, 5 and 0.
 [2004-01-06 20:53 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.

 [2004-01-30 02:45 UTC] johannes at zoomposters dot com
The timeout still doesn't work. The function doesn't hang but the timeout has no effect at all.

Redhat 9 (2004-01-30)
 [2004-02-03 07:02 UTC] johannes at zoomposters dot com
I got it to work now by setting up my own dns server.

It's apparently at the DNS lookup that the code hangs and where the timeout has no effect.
 [2005-01-14 20:40 UTC] shelby at coolpage dot com
Patching the DNS just eliminates the hanging on DNS.  That doesn't fix the fact that the timeout isn't working.

This is still broken, see:
 [2006-05-10 18:21 UTC] adult at adult-inc dot biz
I'm experiencing same problems with PHP 4.3.4 for Win32.

| CODE |
$socket = fsockopen($ip, $port, &$errno, &$errstr, 5);
stream_set_blocking($socket, FALSE );
stream_set_timeout($socket, 5);
fputs($socket, $request);
$html = "";
while (!feof($socket))
	$html = $html.fgets($socket, 128);

| Result |
I got a fatal error after 30 seconds of running. I tried even 600 seconds in max_execution_time and have no luck - still have the same error. Looks like script hangs up while connecting to non-existent port on remote host.

PS: Notice that i'm using only IP-addresses to connect, no hostnames! So the problem not in DNS-resolving.
 [2017-02-23 10:00 UTC] mars dot msn at gmail dot com
With PHP 5.6 i am still experiencing this same problem.

When an IP+PORT is blocked by a firewall, it doesn't timeout at the set time, but keeps waiting for 30seconds.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jul 18 05:01:28 2024 UTC