Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throws TimeoutException when TransferTimeout is exceeded #382

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from

Conversation

luzrain
Copy link
Contributor

@luzrain luzrain commented Mar 4, 2025

I found out that when setting a TransferTimeout limit on a request, the thrown exception is a SocketException instead of a TimeoutException. This issue only affects HTTP connections, HTTP/2 connections correctly throw TimeoutException in such cases as expected.
This change makes the code throw a TimeoutException instead of a SocketException when a connection session exceeds the TransferTimeout period.

Closes #379

@luzrain luzrain force-pushed the transfer_timeout_exception branch from 6cc11c8 to 57cce05 Compare March 4, 2025 04:51
@luzrain
Copy link
Contributor Author

luzrain commented Mar 7, 2025

Here is the code to reproduce the issue.

<?php

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;

require dirname(__DIR__, 1) . '/vendor/autoload.php';

$client = (new HttpClientBuilder())->retry(0)->build();

$request = new Request('https://httpbin.org/delay/5');
$request->setTransferTimeout(1);

try {
    $request1 = clone $request;
    $request1->setProtocolVersions(['1.1']);
    $response = $client->request($request1);
} catch (\Throwable $e) {
    var_dump('HTTP/1.1 ' . $e::class . ': ' . $e->getMessage());
}

try {
    $request2 = clone $request;
    $request2->setProtocolVersions(['2']);
    $response = $client->request($request2);
} catch (\Throwable $e) {
    var_dump('HTTP/2 ' . $e::class . ': ' . $e->getMessage());
}

The output without the fix:
Different exceptions are thrown in the same scenario for HTTP/1.1 and HTTP/2 requests

string(214) "HTTP/1.1 Amp\Http\Client\SocketException: Receiving the response headers for 'https://httpbin.org/delay/5' failed, because the socket to 'httpbin.org' @ '???' closed early with 0 bytes received within 1.000 seconds"
string(96) "HTTP/2 Amp\Http\Client\TimeoutException: Allowed transfer timeout exceeded, took longer than 1 s"

With this fix:
Exceptions are consistent for HTTP/1.1 and HTTP/2 requests

string(98) "HTTP/1.1 Amp\Http\Client\TimeoutException: Allowed transfer timeout exceeded, took longer than 1 s"
string(96) "HTTP/2 Amp\Http\Client\TimeoutException: Allowed transfer timeout exceeded, took longer than 1 s"

@kelunik
Copy link
Member

kelunik commented Mar 12, 2025

LGTM on first sight, but only looked on mobile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

SocketException distinction
2 participants