Skip to content

Commit 867ba80

Browse files
kjpou1lewing
andauthored
[browser][websocket] Fix for Close Description validation (#45536)
* [browser][websocket] Fix for Close Description validation - Issue #45531 - The description validation exception was not being percolated through to the unit test * Remove test code * Address review comments. Wrap in a try/catch that will catch that exception and store it into a task to be returned. * Fix merge conflict * Remove active issue Co-authored-by: Larry Ewing <lewing@microsoft.com>
1 parent 65bcfd0 commit 867ba80

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -463,24 +463,39 @@ public override void Abort()
463463
Dispose();
464464
}
465465

466-
public override async Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
466+
public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
467467
{
468468
_writeBuffer = null;
469469
ThrowIfNotConnected();
470-
await CloseAsyncCore(closeStatus, statusDescription, cancellationToken).ConfigureAwait(continueOnCapturedContext: true);
471-
}
472-
473-
private async Task CloseAsyncCore(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
474-
{
475-
ThrowOnInvalidState(State, WebSocketState.Connecting, WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent);
476470

477471
WebSocketValidate.ValidateCloseStatus(closeStatus, statusDescription);
478472

479-
_tcsClose = new TaskCompletionSource();
480-
_innerWebSocketCloseStatus = closeStatus;
481-
_innerWebSocketCloseStatusDescription = statusDescription;
482-
_innerWebSocket!.Invoke("close", (int)closeStatus, statusDescription);
483-
await _tcsClose.Task.ConfigureAwait(continueOnCapturedContext: true);
473+
try
474+
{
475+
ThrowOnInvalidState(State, WebSocketState.Connecting, WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent);
476+
}
477+
catch (Exception exc)
478+
{
479+
return Task.FromException(exc);
480+
}
481+
482+
return CloseAsyncCore(closeStatus, statusDescription, cancellationToken);
483+
}
484+
485+
private Task CloseAsyncCore(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
486+
{
487+
try
488+
{
489+
_tcsClose = new TaskCompletionSource();
490+
_innerWebSocketCloseStatus = closeStatus;
491+
_innerWebSocketCloseStatusDescription = statusDescription;
492+
_innerWebSocket!.Invoke("close", (int)closeStatus, statusDescription);
493+
return _tcsClose.Task;
494+
}
495+
catch (Exception exc)
496+
{
497+
return Task.FromException(exc);
498+
}
484499
}
485500

486501
public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) => throw new PlatformNotSupportedException();

src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public async Task CloseAsync_CloseDescriptionIsMaxLength_Success(Uri server)
103103

104104
[OuterLoop("Uses external server")]
105105
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
106-
[ActiveIssue("https://github.com/dotnet/runtime/issues/45531", TestPlatforms.Browser)]
107106
public async Task CloseAsync_CloseDescriptionIsMaxLengthPlusOne_ThrowsArgumentException(Uri server)
108107
{
109108
string closeDescription = new string('C', CloseDescriptionMaxLength + 1);

0 commit comments

Comments
 (0)