-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[browser] WS cancelation fix #99522
Merged
Merged
[browser] WS cancelation fix #99522
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -135,9 +135,10 @@ public override string? SubProtocol | |||
|
||||
internal Task ConnectAsync(Uri uri, List<string>? requestedSubProtocols, CancellationToken cancellationToken) | ||||
{ | ||||
AbortIfCancelationRequested(cancellationToken); | ||||
|
||||
lock (_lockObject) | ||||
{ | ||||
cancellationToken.ThrowIfCancellationRequested(); | ||||
ThrowIfDisposed(); | ||||
|
||||
if (FastState != WebSocketState.None) | ||||
|
@@ -184,7 +185,6 @@ public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? | |||
// this validation should be synchronous | ||||
WebSocketValidate.ValidateCloseStatus(closeStatus, statusDescription); | ||||
|
||||
cancellationToken.ThrowIfCancellationRequested(); | ||||
ThrowIfDisposed(); | ||||
|
||||
return CloseAsyncCore(closeStatus, statusDescription, false, cancellationToken); | ||||
|
@@ -195,7 +195,6 @@ public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? status | |||
// this validation should be synchronous | ||||
WebSocketValidate.ValidateCloseStatus(closeStatus, statusDescription); | ||||
|
||||
cancellationToken.ThrowIfCancellationRequested(); | ||||
ThrowIfDisposed(); | ||||
|
||||
return CloseAsyncCore(closeStatus, statusDescription, true, cancellationToken); | ||||
|
@@ -284,6 +283,17 @@ private void ThrowIfDisposed() | |||
} // lock | ||||
} | ||||
|
||||
private void AbortIfCancelationRequested(CancellationToken cancellationToken) | ||||
{ | ||||
if (cancellationToken.IsCancellationRequested) | ||||
{ | ||||
lock (_lockObject) | ||||
{ | ||||
Abort(); | ||||
} // lock | ||||
cancellationToken.ThrowIfCancellationRequested(); | ||||
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. fyi: this internally checks for 'cancellationToken.IsCancellationRequested`. runtime/src/libraries/System.Private.CoreLib/src/System/Threading/CancellationToken.cs Line 359 in efa2b78
|
||||
} | ||||
} | ||||
|
||||
private void CreateCore(Uri uri, List<string>? requestedSubProtocols) | ||||
{ | ||||
|
@@ -366,14 +376,14 @@ private async Task SendAsyncCore(ArraySegment<byte> buffer, WebSocketMessageType | |||
{ | ||||
lock (_lockObject) | ||||
{ | ||||
cancellationToken.ThrowIfCancellationRequested(); | ||||
ThrowIfDisposed(); | ||||
|
||||
previousState = FastState; | ||||
if (previousState != WebSocketState.Open && previousState != WebSocketState.CloseReceived) | ||||
{ | ||||
throw new InvalidOperationException(SR.net_WebSockets_NotConnected); | ||||
} | ||||
AbortIfCancelationRequested(cancellationToken); | ||||
|
||||
if (buffer.Count == 0) | ||||
{ | ||||
|
@@ -416,14 +426,14 @@ private async Task<WebSocketReceiveResult> ReceiveAsyncCore(ArraySegment<byte> b | |||
{ | ||||
lock (_lockObject) | ||||
{ | ||||
cancellationToken.ThrowIfCancellationRequested(); | ||||
ThrowIfDisposed(); | ||||
|
||||
previousState = FastState; | ||||
if (previousState != WebSocketState.Open && previousState != WebSocketState.CloseSent) | ||||
{ | ||||
throw new WebSocketException(WebSocketError.InvalidState, SR.Format(SR.net_WebSockets_InvalidState, previousState, "Open, CloseSent")); | ||||
} | ||||
AbortIfCancelationRequested(cancellationToken); | ||||
|
||||
Memory<byte> bufferMemory = buffer.AsMemory(); | ||||
pinBuffer = bufferMemory.Pin(); | ||||
|
@@ -502,22 +512,22 @@ private async Task CloseAsyncCore(WebSocketCloseStatus closeStatus, string? stat | |||
WebSocketState previousState; | ||||
lock (_lockObject) | ||||
{ | ||||
cancellationToken.ThrowIfCancellationRequested(); | ||||
|
||||
previousState = FastState; | ||||
if (_aborted) | ||||
{ | ||||
return; | ||||
} | ||||
if (previousState == WebSocketState.None || previousState == WebSocketState.Closed) | ||||
{ | ||||
throw new WebSocketException(WebSocketError.InvalidState, SR.Format(SR.net_WebSockets_InvalidState, previousState, "Connecting, Open, CloseSent, CloseReceived, Aborted")); | ||||
} | ||||
AbortIfCancelationRequested(cancellationToken); | ||||
|
||||
if (!_closeReceived) | ||||
{ | ||||
_closeStatus = closeStatus; | ||||
_closeStatusDescription = statusDescription; | ||||
} | ||||
if (previousState == WebSocketState.None || previousState == WebSocketState.Closed) | ||||
{ | ||||
throw new WebSocketException(WebSocketError.InvalidState, SR.Format(SR.net_WebSockets_InvalidState, previousState, "Connecting, Open, CloseSent, Aborted")); | ||||
} | ||||
|
||||
_closeSent = true; | ||||
|
||||
|
@@ -544,7 +554,6 @@ private async Task CancellationHelper(Task promise, CancellationToken cancellati | |||
{ | ||||
try | ||||
{ | ||||
cancellationToken.ThrowIfCancellationRequested(); | ||||
if (promise.IsCompletedSuccessfully) | ||||
{ | ||||
disposable?.Dispose(); | ||||
|
@@ -556,6 +565,7 @@ private async Task CancellationHelper(Task promise, CancellationToken cancellati | |||
await promise.ConfigureAwait(false); | ||||
return; | ||||
} | ||||
AbortIfCancelationRequested(cancellationToken); | ||||
|
||||
using (var receiveRegistration = cancellationToken.Register(static s => | ||||
{ | ||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why moving it out of the lock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably doesn't matter