Forum Moderators: coopster

Script Not Timing Out (Sometimes)

         

l008comm

9:01 am on Mar 31, 2026 (gmt 0)

10+ Year Member Top Contributors Of The Month



I have a CLI (launchd) script that runs periodically and makes some HTTP requests. I find that under certain situations, where the server is offline (like during a TCP reset), outgoing HTTP requests (via curl_exec( ) ) are hanging. But instead of timing out, they just keep going for 5, 10, 15 minutes or more.


<?
set_time_limit(120);

$ch = curl_init($url);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 etc");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
curl_setopt($ch,CURLOPT_INTERFACE,OPERATING_IP);
$return = curl_exec($ch);
?>


Apparently time spent in curl_exec( ) is excluded from set_time_limit( ) time limits.

But shouldn't either CURLOPT_TIMEOUT or CURLOPT_CONNECTTIMEOUT (especially CURLOPT_CONNECTTIMEOUT) cause the curl call to fail after 10 seconds?

If not, whats the best way to get curl_exec( ) to fail (quickly) when it can't connect, rather than waiting minutes?

l008comm

10:41 am on Mar 31, 2026 (gmt 0)

10+ Year Member Top Contributors Of The Month



I was checking out the curl docs and apparently CURLOPT_CONNECTTIMEOUT is included within the greater CURLOPT_TIMEOUT setting. Meaning just the CURLOPT_TIMEOUT,10 line should cause curl_exec() to timeout after 10 seconds. Even if I omit the CURLOPT_CONNECTTIMEOUT line entirely. But thats clearly is not what's happening.

lucy24

4:54 pm on Mar 31, 2026 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This puts me in mind of something I've occasionally done when I'm writing a function with a repeating loop and I’m not *absolutely* certain the loop will close when it's supposed to. Conceptually:

counter = 0
WHILE ({condition} AND counter < 1000)
  counter++
  do stuff that will eventually change the condition

It can be applied in other contexts.

Just sayin.

l008comm

9:25 pm on Mar 31, 2026 (gmt 0)

10+ Year Member Top Contributors Of The Month



But I'm not getting stuck in a loop. I'm literally just getting stuck on curl_exec( ). Its not succeeding, it's not failing, it's just "trying", for 6 minutes or more. Putting that in a loop wouldn't change this at all from what I can see.

No5needinput

9:38 pm on Mar 31, 2026 (gmt 0)

10+ Year Member Top Contributors Of The Month



I pasted your ENTIRE top post into chatgpt just to see what it would come up with - free version. It SEEMS to have the answer. Maybe try the same and see if it helps?