Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Allow OperationCanceledExceptions to propagate out of ClientWebSocket.ConnectAsync #38204

Merged
merged 1 commit into from
Jun 7, 2019

Conversation

stephentoub
Copy link
Member

They're currently being wrapped in WebSocketExceptions, but they should be allowed to escape unwrapped.

Fixes https://github.com/dotnet/corefx/issues/38199
cc: @davidsh, @matthid

@davidsh davidsh added this to the 3.0 milestone Jun 4, 2019
@stephentoub
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@stephentoub
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@@ -223,10 +223,12 @@ public async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken,
Abort();
response?.Dispose();

if (exc is WebSocketException)
if (exc is WebSocketException ||
(exc is OperationCanceledException && cancellationToken.IsCancellationRequested))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any risk of getting an OperationCanceledException for a different CancellationToken?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any risk of getting an OperationCanceledException for a different CancellationToken?

It's certainly possible, e.g. for an Abort() call, which is why we check whether this cancellation token had cancellation requested. If something else caused cancellation, then from the perspective of this call it wasn't the one to cancel it, and so it continues to wrap the exception. If something else caused cancellation but this was also canceled, then it doesn't matter which actually triggered things to cancel, and so we allow it through.

@matthid
Copy link

matthid commented Jun 5, 2019

The PR looks good to me as well. In the end, I feel like it is even more important to document this kind of non-happy-cases (see linked issue).
Sadly, it is very much possible that people already depend on previous behavior. I'm not sure what your stand on this is.

@stephentoub
Copy link
Member Author

I'm not sure what your stand on this is.

Every bug fix has the potential to be a breaking change for someone. We definitely take such things into account, and in general we're wary of changing the type of exception thrown in certain circumstances. In this case, however, I think it's ok. Across .NET the broad pattern is that cancellation results in OperationCanceledException (or derived exceptions), and it's a bug that this case was wrapping them in another layer.

@stephentoub
Copy link
Member Author

/azp run corefx-outerloop-windows

@stephentoub
Copy link
Member Author

/azp run corefx-outerloop-linux

@stephentoub
Copy link
Member Author

/azp run corefx-outerloop-osx

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

2 similar comments
@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

….ConnectAsync

They're currently being wrapped in WebSocketExceptions, but they should be allowed to escape unwrapped.
@stephentoub
Copy link
Member Author

/azp run corefx-outerloop-windows

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephentoub stephentoub merged commit 513f9f9 into dotnet:master Jun 7, 2019
@stephentoub stephentoub deleted the wsconnectcancel branch June 7, 2019 10:58
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
….ConnectAsync (dotnet/corefx#38204)

They're currently being wrapped in WebSocketExceptions, but they should be allowed to escape unwrapped.

Commit migrated from dotnet/corefx@513f9f9
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent behavior of ClientWebSockets on cancellation
4 participants