Skip to content

Commit

Permalink
[AndroidClientHandler] Observe cancellation requests more
Browse files Browse the repository at this point in the history
Context: dotnet#4550

It is possible that, in certain situations, a cancellation request
arriving from code external to `AndroidClientHandler` will not be
satisfied while starting the task to acquire connection status, because
the cancellation request appears after handler already established
connection to the remote host but before the request status is obtained.

Failure to check that the cancellation was requested may result in the
status retrieval task starting only to run into the issue that the
`AndroidClientHandler` object itself is already disposed (since the
cancellation request has been applied) and throw the
`ObjectDisposedException` instead of cancelling the status request
quietly.  Note that this is likely happen only in situations when the
application code aggressively cancels and collects (via calls to the
`GC` class) `AndroidClientHandler` instances or in other situations
which induce potential for timing issues in the TPL or `async/await`
state machine, thus leading to possible race conditions there.

This commit passes cancellation token to the task which runs the status
request, thus preventing it from starting in the situation described
above.  The possibility of a race condition still remains, but is
seriously reduced.
  • Loading branch information
grendello committed Apr 21, 2020
1 parent 3fee243 commit 697faa6
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ protected virtual async Task WriteRequestContentToOutput (HttpRequestMessage req
if (httpConnection.DoOutput)
await WriteRequestContentToOutput (request, httpConnection, cancellationToken);

statusCode = await Task.Run (() => (HttpStatusCode)httpConnection.ResponseCode).ConfigureAwait (false);
statusCode = await Task.Run (() => (HttpStatusCode)httpConnection.ResponseCode, cancellationToken).ConfigureAwait (false);
connectionUri = new Uri (httpConnection.URL.ToString ());
} finally {
cancelRegistration.Dispose ();
Expand Down

0 comments on commit 697faa6

Please sign in to comment.