Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Connect timeout never set in cURL, hanging longer than timeout/max_duration on unavailable hosts #47246

Closed
@beberlei

Description

@beberlei

Symfony version(s) affected

4.4.x and higher

Description

In #33022 the CURL_CONNECTTIMEOUT_MS was removed but it was not added to the allow list of curl.extra again. As such its not possible to set this setting, causing the default of 5 seconds to be used when connecting to a host that is not available.

How to reproduce

<?php

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

require_once __DIR__ . '/../vendor/autoload.php';

$client = HttpClient::create();
var_dump(get_class($client));
$ts = hrtime(true);
try {
    $response = $client->request(
        'GET',
        'http://unknown.local:1080/messages',
        options: ['timeout' => 1, 'max_duration' => 1]
    );

    $messages = $response->toArray();
} catch (TransportExceptionInterface $e) {
    var_dump($e->getMessage());
}
echo "Duration: " . ((hrtime(true) - $ts) / 1e+6) . "\n";

/**
 * string(43) "Symfony\Component\HttpClient\CurlHttpClient"
string(85) "Resolving timed out after 1000 milliseconds for "http://unknown.local:1080/messages"."
Duration: 5024.504761
 */

Possible Solution

Allow at least to set CURLOPT_CONNECTTIMEOUT_MS in curl.extra, but in general at least for max_duration, it should also set CURLOPT_CONNECTTIMEOUT_MS in addition to CURLOPT_TIMEOUT_MS.

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions