1

Curl connection to remote server timeout once in a while, however when I take a tcpdump, it shows that the server actually responded even though the curl request had timed out. Is this a tcp behaviour? Below is a sample server side php code to simulate the behaviour, file named test.php and deployed on Nginx with PHP-FPM.

<?php
sleep(10);
echo "Hello World";

Below is a curl request with a 5seconds timeout.

curl -m 5 'http://server_ip:port/test.php'

Below is a sample client side script that can be used in place of the above

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://server_ip:port/test.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);

if (curl_errno($curl) > 0)
{
    echo curl_error($curl), curl_errno($curl), PHP_EOL;
}
else 
{
    echo $response, PHP_EOL;
}

curl_close($curl);

tcpdump -w timeout_1.pcap -s0 -v -tttt -i eth0 host

The tcpdump was taken on the client machine.

st__gen_server
  • 597
  • 5
  • 9
  • 1
    _"or there's something I'm not doing right"_ - We don't know since we have no idea what you're actually doing. Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) and edit your question accordingly. – M. Eriksson Jul 01 '20 at 12:46
  • I think the problem is about how TCP/IP works, server responds to your ip, that's why tcpdump can see those packets, but it's not being processed by curl, as it doesn't listen for it anymore. That's used for DDOS attacks. Normally some router should fiter that, but... maybe in your case it doesn't. Not reproducible on my config. – Flash Thunder Jul 02 '20 at 09:32
  • Check this out about PHP side, and why it might keep executing the script till the end after disconnect: https://stackoverflow.com/questions/1280291/does-php-execution-stop-after-a-user-leaves-the-page – Flash Thunder Jul 02 '20 at 09:37
  • 1
    @FlashThunder, the link and the previous explanation were very helpful. – st__gen_server Jul 04 '20 at 08:14

0 Answers0