r/PHPhelp • u/l008com • Apr 01 '26
Solved Script Not Timing Out on a hung curl_exec( ) call
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?
Reading the curl docs, CURLOPT_TIMEOUT should include everything including CURL_CONNECTTIMEOUT but in this case it is not. My script gets to the curl_exec( ) call (under these specific circumstances, not always), and just hangs.
How can I really FORCE a timeout, where the call actually fails after 10 seconds or so?
1
u/flyingron Apr 01 '26 edited Apr 01 '26
Works for me. Something else is wrong. What is the URL?
CURLOPT_CONNECTIMEOUT is the number of seconds to try to create the http connection. It catches hosts that are down or unreachable without a hard failure. CURLOPT_TIMEOUT puts a total time on the execution of curl_exec which will cover both the machine not opening the connection at all to the http request taking an inordinate amount of time after the connection is opened.
What is OPERATING_IP that you are feeding to the INTERFACE option?
Note, I think that if DNS hangs up on your local machine, that may interfere with CURL timing.
1
u/AzozzALFiras Apr 04 '26
The reason your script is hanging despite
CURLOPT_TIMEOUTis thatlibcurlsometimes relies on operating system signals that don't trigger correctly in certain CLI environments (like those managed bylaunchd) or during specific TCP states (like a reset or "black hole"). Furthermore, as you noted,set_time_limit()only counts PHP execution time, not the time spent waiting for network I/O.To truly FORCE a timeout, you need a multi-layered approach:
1. The "Low Speed" Kill Switch (Most Effective)
This is the most reliable way to kill a hanging connection. Instead of a hard clock, you tell cURL: "If data transfer drops below X bytes per second for Y seconds, kill the connection."
PHP
2. Disable Signals (CURLOPT_NOSIGNAL)
This is critical for CLI and cron-like environments (launchd). In many environments, cURL tries to use standard POSIX signals to handle timeouts. If these signals are masked or ignored by the parent process, the timeout never triggers. Setting this to
1forces cURL to use its internal timer.PHP
3. Address DNS and IPv6 "Hanging"
Sometimes the hang occurs during the DNS lookup or while trying to connect via IPv6 before falling back to IPv4.
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);The Optimized Code Block
PHP
Why this works:
By adding
CURLOPT_NOSIGNAL, you ensure the timer is handled internally by the library rather than the OS kernel. By adding theLOW_SPEEDlimits, you create a "heartbeat" check; even if the TCP stack is stuck in a weird state (like a half-closed reset), the lack of data throughput will trigger an immediate abort.