Bug #72611 [NEW]: CURLOPT_TCP_KEEPALIVE missing

From: Date: Sun, 17 Jul 2016 22:42:46 +0000
Subject: Bug #72611 [NEW]: CURLOPT_TCP_KEEPALIVE missing
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202383@lists.php.net to get a copy of this message
From:             binki at gentoo dot org
Operating system: unix
PHP version:      7.0.8
Package:          cURL related
Bug Type:         Bug
Bug description:CURLOPT_TCP_KEEPALIVE missing

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


-- 
Edit bug report at https://bugs.php.net/bug.php?id=72611&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=72611&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=72611&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=72611&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=72611&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=72611&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=72611&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=72611&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=72611&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=72611&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=72611&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=72611&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=72611&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=72611&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72611&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=72611&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=72611&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=72611&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72611&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=72611&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=72611&r=mysqlcfg



Thread (1 message)

  • binki at gentoo dot org
« previous php.bugs (#202383) next »