Skip to content

Conversation

@grendello
Copy link
Contributor

The HttpURLConnection.ConnectAsync API doesn't accept a CancellationToken
instance and, thus, cannot be easily cancelled in a graceful manner. This
limitation resulted in using the Task.WhenAny to make sure the connection
attempt is aborted whenever the calling task is cancelled. This, however, led to
a problem of unobserved exceptions should the cancellation occur before the
connection attempt was successful.

This commit wraps the synchronous HttpURLConnection.Connect call in a task
that is passed the cancellation token, so that it can be gracefully cancelled
when needed.

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=51804

@alanmcgovern
Copy link

lgtm!

@swert-hon
Copy link

Task.Run only checks the CancellationToken before starting the task; after that point, the task itself is responsible for checking the token. Hence the complicated Task.WhenAny stuff that was added to properly support CancellationToken in the first place.

@grendello
Copy link
Contributor Author

Which seems to be the problem here. I'm working on a better solution, will update the PR once it's ready.

The `HttpURLConnection.ConnectAsync` API doesn't accept a `CancellationToken`
instance and, thus, cannot be easily cancelled in a graceful manner. This
limitation resulted in using the `Task.WhenAny` to make sure the connection
attempt is aborted whenever the calling task is cancelled. This, however, led to
a problem of unobserved exceptions should the cancellation occur before the
connection attempt was successful.

This commit wraps the synchronous `HttpURLConnection.Connect` call in a task
that is passed the cancellation token, so that it can be gracefully cancelled
when needed. The cancellation is done by registering a handler for when token is
about to expire as Task.Run will check if cancellation was requested only before
it runs the passed code, the rest is the code's responsibility.
Since `URLConnection.Connect()` is asynchronous we abort it by calling
`Disconnect()` on the instance which is a brutal but effective way to interrupt
the connection attempt.

Additionally, the diff makes sure to properly configure a few tasks for `await`

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=51804
@jonpryor jonpryor merged commit 36d7e73 into dotnet:master Mar 9, 2017
@grendello grendello deleted the bug51804 branch March 9, 2017 20:14
grendello added a commit to grendello/xamarin-android that referenced this pull request Mar 23, 2017
The `HttpURLConnection.ConnectAsync` API doesn't accept a `CancellationToken`
instance and, thus, cannot be easily cancelled in a graceful manner. This
limitation resulted in using the `Task.WhenAny` to make sure the connection
attempt is aborted whenever the calling task is cancelled. This, however, led to
a problem of unobserved exceptions should the cancellation occur before the
connection attempt was successful.

This commit wraps the synchronous `HttpURLConnection.Connect` call in a task
that is passed the cancellation token, so that it can be gracefully cancelled
when needed. The cancellation is done by registering a handler for when token is
about to expire as Task.Run will check if cancellation was requested only before
it runs the passed code, the rest is the code's responsibility.
Since `URLConnection.Connect()` is asynchronous we abort it by calling
`Disconnect()` on the instance which is a brutal but effective way to interrupt
the connection attempt.

Additionally, the diff makes sure to properly configure a few tasks for `await`

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=51804
grendello added a commit to grendello/xamarin-android that referenced this pull request Mar 23, 2017
The `HttpURLConnection.ConnectAsync` API doesn't accept a `CancellationToken`
instance and, thus, cannot be easily cancelled in a graceful manner. This
limitation resulted in using the `Task.WhenAny` to make sure the connection
attempt is aborted whenever the calling task is cancelled. This, however, led to
a problem of unobserved exceptions should the cancellation occur before the
connection attempt was successful.

This commit wraps the synchronous `HttpURLConnection.Connect` call in a task
that is passed the cancellation token, so that it can be gracefully cancelled
when needed. The cancellation is done by registering a handler for when token is
about to expire as Task.Run will check if cancellation was requested only before
it runs the passed code, the rest is the code's responsibility.
Since `URLConnection.Connect()` is asynchronous we abort it by calling
`Disconnect()` on the instance which is a brutal but effective way to interrupt
the connection attempt.

Additionally, the diff makes sure to properly configure a few tasks for `await`

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=51804
jonpryor pushed a commit that referenced this pull request Mar 23, 2017
The `HttpURLConnection.ConnectAsync` API doesn't accept a `CancellationToken`
instance and, thus, cannot be easily cancelled in a graceful manner. This
limitation resulted in using the `Task.WhenAny` to make sure the connection
attempt is aborted whenever the calling task is cancelled. This, however, led to
a problem of unobserved exceptions should the cancellation occur before the
connection attempt was successful.

This commit wraps the synchronous `HttpURLConnection.Connect` call in a task
that is passed the cancellation token, so that it can be gracefully cancelled
when needed. The cancellation is done by registering a handler for when token is
about to expire as Task.Run will check if cancellation was requested only before
it runs the passed code, the rest is the code's responsibility.
Since `URLConnection.Connect()` is asynchronous we abort it by calling
`Disconnect()` on the instance which is a brutal but effective way to interrupt
the connection attempt.

Additionally, the diff makes sure to properly configure a few tasks for `await`

Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=51804
grendello added a commit that referenced this pull request Mar 23, 2017
The `HttpURLConnection.ConnectAsync` API doesn't accept a `CancellationToken`
instance and, thus, cannot be easily cancelled in a graceful manner. This
limitation resulted in using the `Task.WhenAny` to make sure the connection
attempt is aborted whenever the calling task is cancelled. This, however, led to
a problem of unobserved exceptions should the cancellation occur before the
connection attempt was successful.

This commit wraps the synchronous `HttpURLConnection.Connect` call in a task
that is passed the cancellation token, so that it can be gracefully cancelled
when needed. The cancellation is done by registering a handler for when token is
about to expire as Task.Run will check if cancellation was requested only before
it runs the passed code, the rest is the code's responsibility.
Since `URLConnection.Connect()` is asynchronous we abort it by calling
`Disconnect()` on the instance which is a brutal but effective way to interrupt
the connection attempt.

Additionally, the diff makes sure to properly configure a few tasks for `await`

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=51804
@github-actions github-actions bot locked and limited conversation to collaborators Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants