Skip to content

Fix timeout catch in HTTP LoggingHandler and add tests to verify#2072

Draft
lbussell wants to merge 5 commits intodotnet:mainfrom
lbussell:improve-logging-2
Draft

Fix timeout catch in HTTP LoggingHandler and add tests to verify#2072
lbussell wants to merge 5 commits intodotnet:mainfrom
lbussell:improve-logging-2

Conversation

@lbussell
Copy link
Copy Markdown
Member

This PR is a follow up from #2071.

The LoggingHandler timeout catch clause never fired because HttpClient passes a linked CancellationToken (user token + timeout token) to DelegatingHandler.SendAsync. When a timeout occurs, the linked token is already cancelled, so the when (!cancellationToken.IsCancellationRequested) guard evaluated to false and the catch was skipped entirely.

lbussell and others added 4 commits April 10, 2026 09:25
The LoggingHandler timeout catch clause never fired because HttpClient
passes a linked CancellationToken (user token + timeout token) to
DelegatingHandler.SendAsync. When a timeout occurs, the linked token
is already cancelled, so the 'when (!cancellationToken.IsCancellationRequested)'
guard evaluated to false and the catch was skipped entirely.

Fix by catching OperationCanceledException and checking for
TimeoutException as InnerException to distinguish timeouts from
caller-driven cancellations. Also add a pre-request Debug log so
hanging requests are visible, and restore the pre-fetch log in
GetReferrersAsync for non-dry-run calls.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Extract LoggingHandler as a public class (this is an exe, not a library,
so public visibility has no external impact) with a testable constructor
that accepts ILogger directly.

Tests verify:
- Timeout (TaskCanceledException with TimeoutException inner) logs
  'HTTP timeout' at Warning level
- Caller cancellation (OperationCanceledException without TimeoutException)
  logs 'HTTP canceled' at Warning level, not 'HTTP timeout'

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lbussell lbussell changed the title Fix timeout catch in HTTP LoggingHandler and add test to verify Fix timeout catch in HTTP LoggingHandler and add tests to verify Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant