-
Notifications
You must be signed in to change notification settings - Fork 5.2k
HttpClient throws TimeoutException wrapped by TaskCancellationException when request times out #2281
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
HttpClient throws TimeoutException wrapped by TaskCancellationException when request times out #2281
Changes from all commits
93bc5b2
70a0389
935d893
355cd80
ad0cad8
8347c67
e1a84bf
29aaa4a
aa543d0
192ea1d
04fdd78
46f2191
730b6f0
7a01b85
6adf5c3
7ea3e19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Net.Http.Headers; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
|
|
@@ -491,12 +492,14 @@ public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompl | |
| CancellationTokenSource cts; | ||
| bool disposeCts; | ||
| bool hasTimeout = _timeout != s_infiniteTimeout; | ||
| long timeoutTime = long.MaxValue; | ||
| if (hasTimeout || cancellationToken.CanBeCanceled) | ||
| { | ||
| disposeCts = true; | ||
| cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _pendingRequestsCts.Token); | ||
| if (hasTimeout) | ||
| { | ||
| timeoutTime = Environment.TickCount64 + (_timeout.Ticks / TimeSpan.TicksPerMillisecond); | ||
| cts.CancelAfter(_timeout); | ||
| } | ||
| } | ||
|
|
@@ -512,19 +515,25 @@ public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompl | |
| { | ||
| sendTask = base.SendAsync(request, cts.Token); | ||
| } | ||
| catch | ||
| catch (Exception e) | ||
| { | ||
| HandleFinishSendAsyncCleanup(cts, disposeCts); | ||
|
|
||
| if (e is OperationCanceledException operationException && TimeoutFired(cancellationToken, timeoutTime)) | ||
| { | ||
| throw CreateTimeoutException(operationException); | ||
| } | ||
|
|
||
| throw; | ||
| } | ||
|
|
||
| return completionOption == HttpCompletionOption.ResponseContentRead && !string.Equals(request.Method.Method, "HEAD", StringComparison.OrdinalIgnoreCase) ? | ||
| FinishSendAsyncBuffered(sendTask, request, cts, disposeCts) : | ||
| FinishSendAsyncUnbuffered(sendTask, request, cts, disposeCts); | ||
| FinishSendAsyncBuffered(sendTask, request, cts, disposeCts, cancellationToken, timeoutTime) : | ||
| FinishSendAsyncUnbuffered(sendTask, request, cts, disposeCts, cancellationToken, timeoutTime); | ||
| } | ||
|
|
||
| private async Task<HttpResponseMessage> FinishSendAsyncBuffered( | ||
| Task<HttpResponseMessage> sendTask, HttpRequestMessage request, CancellationTokenSource cts, bool disposeCts) | ||
| Task<HttpResponseMessage> sendTask, HttpRequestMessage request, CancellationTokenSource cts, bool disposeCts, CancellationToken callerToken, long timeoutTime) | ||
| { | ||
| HttpResponseMessage response = null; | ||
| try | ||
|
|
@@ -548,6 +557,13 @@ private async Task<HttpResponseMessage> FinishSendAsyncBuffered( | |
| catch (Exception e) | ||
| { | ||
| response?.Dispose(); | ||
|
|
||
| if (e is OperationCanceledException operationException && TimeoutFired(callerToken, timeoutTime)) | ||
| { | ||
| HandleSendAsyncTimeout(operationException); | ||
| throw CreateTimeoutException(operationException); | ||
alnikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| HandleFinishSendAsyncError(e, cts); | ||
| throw; | ||
| } | ||
|
|
@@ -558,7 +574,7 @@ private async Task<HttpResponseMessage> FinishSendAsyncBuffered( | |
| } | ||
|
|
||
| private async Task<HttpResponseMessage> FinishSendAsyncUnbuffered( | ||
| Task<HttpResponseMessage> sendTask, HttpRequestMessage request, CancellationTokenSource cts, bool disposeCts) | ||
| Task<HttpResponseMessage> sendTask, HttpRequestMessage request, CancellationTokenSource cts, bool disposeCts, CancellationToken callerToken, long timeoutTime) | ||
| { | ||
| try | ||
| { | ||
|
|
@@ -573,6 +589,12 @@ private async Task<HttpResponseMessage> FinishSendAsyncUnbuffered( | |
| } | ||
| catch (Exception e) | ||
| { | ||
| if (e is OperationCanceledException operationException && TimeoutFired(callerToken, timeoutTime)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this could be an exception filter.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's an exception filter, then you need two catch blocks; doesn't really help anything, e.g. catch (OperationCanceledException oce) when TimeoutFired(callerToken, timeoutTime)
{
HandleSendAsyncTimeout(oce);
throw CreateTimeoutException(oce);
}
catch (Exception e)
{
HandleFinishSendAsyncError(e, cts);
throw;
}But either way is fine.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find the two catch blocks cleaner; is there a reason to avoid multiple catch blocks when they have entirely exclusive bodies? perf concerns?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously they weren't entirely discrete:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @scalablecory Do you want me to change it back to the exception filters? I'd rather not do this since it will require rerunning all perf tests for Linux and Windows which takes a lot of time.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My opinion is that this is fine as-is. If we think this is a problem later, you can change it. |
||
| { | ||
| HandleSendAsyncTimeout(operationException); | ||
| throw CreateTimeoutException(operationException); | ||
alnikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| HandleFinishSendAsyncError(e, cts); | ||
| throw; | ||
| } | ||
|
|
@@ -582,6 +604,14 @@ private async Task<HttpResponseMessage> FinishSendAsyncUnbuffered( | |
| } | ||
| } | ||
|
|
||
| private bool TimeoutFired(CancellationToken callerToken, long timeoutTime) => !callerToken.IsCancellationRequested && Environment.TickCount64 >= timeoutTime; | ||
|
|
||
| private TaskCanceledException CreateTimeoutException(OperationCanceledException originalException) | ||
| { | ||
| return new TaskCanceledException(string.Format(SR.net_http_request_timedout, _timeout.TotalSeconds), | ||
| new TimeoutException(originalException.Message, originalException), originalException.CancellationToken); | ||
| } | ||
|
|
||
| private void HandleFinishSendAsyncError(Exception e, CancellationTokenSource cts) | ||
| { | ||
| if (NetEventSource.IsEnabled) NetEventSource.Error(this, e); | ||
|
|
@@ -595,6 +625,15 @@ private void HandleFinishSendAsyncError(Exception e, CancellationTokenSource cts | |
| } | ||
| } | ||
|
|
||
| private void HandleSendAsyncTimeout(OperationCanceledException e) | ||
| { | ||
| if (NetEventSource.IsEnabled) | ||
| { | ||
| NetEventSource.Error(this, e); | ||
| NetEventSource.Error(this, "Canceled due to timeout"); | ||
| } | ||
| } | ||
|
|
||
| private void HandleFinishSendAsyncCleanup(CancellationTokenSource cts, bool disposeCts) | ||
| { | ||
| // Dispose of the CancellationTokenSource if it was created specially for this request | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.