php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72611 CURLOPT_TCP_KEEPALIVE missing
Submitted: 2016-07-17 22:42 UTC Modified: 2020-03-12 17:09 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: binki at gentoo dot org Assigned: cmb (profile)
Status: Closed Package: cURL related
PHP Version: 7.0.8 OS: unix
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: binki at gentoo dot org
New email:
PHP Version: OS:

 

 [2016-07-17 22:42 UTC] binki at gentoo dot org
Description:
------------
I have a script that uses php’s curl interface to connect to a web server and download data. I don’t put a timeout on the request because it could take a long time to respond. However, sometimes the TCP connection gets stale. The underlying sockets never disconnect because the connection never gets reset because the network connection is stable (this is on a VPS). TCP offers KeepAlive which allows for detecting a dead connection by sending data outside of the application visible stream to validate that the other host still sees the connection as valid. Thus, sometimes the connection dies without resetting the socket (the remote system closes it uncleanly) and the script on my VPS hangs for months until I notice it is stuck and manually kill it.

libcurl offers a way to enable TCP KeepAlive on its connections: https://curl.haxx.se/libcurl/c/CURLOPT_TCP_KEEPALIVE.html . I don’t see this option documented at either http://php.net/manual/en/curl.constants.php or http://php.net/manual/en/function.curl-setopt.php . I would like to be able to turn on that option from my PHP scripts or have PHP just enable it by default.

For the test script, start the script and then execute something like “iptables -I INPUT 1 -s httpbin.org -j DROP” before the 10 second delay expires. Note that the script hangs forever. If you have a recent enough version of libcurl, the CLI will instead have this behavior after a couple minutes (because the CLI curl command turns on CURLOPT_TCP_KEEPALIVE by default):

ohnobinki@ohnopublishing ~ $ http_proxy= curl http://httpbin.org/delay/10
curl: (56) Recv failure: Connection timed out


When done testing, use “iptables -D INPUT 1” to clean up the rule.

Test script:
---------------
#!/usr/bin/env php
<?php
$curl = curl_init('http://httpbin.org/delay/10');
curl_setopt($curl, CURLOPT_TCP_KEEPALIVE, TRUE); // I get a warning I shouldn’t here about undefined constant
/*
 * Within 10 seconds of this line being reached, configure your
 * gateway to start dropping all packets from the connection,
 * including connection reset packets without letting the client
 * computer reset its interface (turning off WiFi or unplugging
 * Ethernet will reset the connection and *not* trigger this
 * condition). If TCP KeepAlive is enabled, the call should terminate
 * when the OS’s keepalive timers expire. Otherwise, the command will
 * hang forever.
 */
curl_exec($curl);


Expected result:
----------------
The script exit. The curl_exec() call would return an error and nothing would be printed on screen.

Actual result:
--------------
Script hangs forever with following output:

Notice: Use of undefined constant CURLOPT_TCP_KEEPALIVE - assumed 'CURLOPT_TCP_KEEPALIVE' in /home/ohnobinki/php_curl_tcp_keepalive.php on line 4
PHP Warning:  curl_setopt() expects parameter 2 to be long, string given in /home/ohnobinki/php_curl_tcp_keepalive.php on line 4

Warning: curl_setopt() expects parameter 2 to be long, string given in /home/ohnobinki/php_curl_tcp_keepalive.php on line 4


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-17 22:53 UTC] requinix@php.net
-Status: Open +Status: Feedback -Type: Bug +Type: Documentation Problem
 [2016-07-17 22:53 UTC] requinix@php.net
It doesn't appear to be documented, but CURLOPT_TCP_KEEPALIVE was added in PHP 5.5.0 and requires libcurl >=7.25.0. What version are you using?
 [2016-07-18 03:25 UTC] binki at gentoo dot org
I was testing with php-5.4.10--p10-gentoo. I just happened to update the system I care about to 5.6.17, and I was able to verify that my test case is fixed with it.

Sorry for the false version in the bug report. Thanks for bumping this to a documentation bug!
 [2016-07-31 04:22 UTC] php-bugs at lists dot php dot net
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 "Re-Opened". Thank you.
 [2016-07-31 10:00 UTC] requinix@php.net
-Status: No Feedback +Status: Open
 [2020-03-12 17:05 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2020-03-12 17:08 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=349396
Log: Fix #72611: CURLOPT_TCP_KEEPALIVE missing
 [2020-03-12 17:09 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2020-03-12 17:09 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-03-12 17:10 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=12bae7345751805662cb4e7a6412a2f3db6bbcfc
Log: Fix #72611: CURLOPT_TCP_KEEPALIVE missing
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC