Skip to content

[WIP] [release/8.0][browser] BrowserWebSocket.ReceiveAsync after server initiated close #99413

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ await socket.CloseAsync(
{
await Task.Delay(5000);
}
else if (receivedMessage == ".receiveMessageAfterClose")
{
byte[] buffer = new byte[1024];
string message = $"{receivedMessage} {DateTime.Now.ToString("HH:mm:ss")}";
buffer = System.Text.Encoding.UTF8.GetBytes(message);
await socket.SendAsync(
new ArraySegment<byte>(buffer, 0, message.Length),
WebSocketMessageType.Text,
true,
CancellationToken.None);
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, receivedMessage, CancellationToken.None);
}
else if (socket.State == WebSocketState.Open)
{
sendMessage = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,6 @@ private void CreateCore(Uri uri, List<string>? requestedSubProtocols)
#endif
_closeStatus = (WebSocketCloseStatus)code;
_closeStatusDescription = reason;
_closeReceived = true;
WebSocketState state = State;
if (state == WebSocketState.Connecting || state == WebSocketState.Open || state == WebSocketState.CloseSent)
{
FastState = WebSocketState.Closed;
}
#if FEATURE_WASM_THREADS
} //lock
#endif
Expand Down
55 changes: 55 additions & 0 deletions src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,61 @@ await cws.SendAsync(
}
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/28957", typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_ServerInitiated_CanReceiveAfterClose(Uri server)
{
await ReceiveMessageAfterClose(server, false);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/28957", typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_ServerInitiated_CannotReceiveAfterCloseAndStateSync(Uri server)
{
try
{
await ReceiveMessageAfterClose(server, true);
}
catch (WebSocketException e)
{
Assert.Equal(WebSocketError.InvalidState, e.WebSocketErrorCode);
return;
}
catch (Exception e)
{
Assert.True(false, $"Unexpected exception: {e}");
}
Assert.True(false, "Expected WebSocketException not thrown.");
Copy link
Member Author

Choose a reason for hiding this comment

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

Change to Assert.Fail when CI passes.

}

private async Task ReceiveMessageAfterClose(Uri server, bool syncState)
{
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
await cws.SendAsync(
WebSocketData.GetBufferFromText(".receiveMessageAfterClose"),
WebSocketMessageType.Text,
true,
cts.Token);

await Task.Delay(2000);

if (syncState)
{
var state = cws.State;
Assert.Equal(WebSocketState.Closed, state);
// should not be able to receive after this sync
}

var recvBuffer = new ArraySegment<byte>(new byte[1024]);
WebSocketReceiveResult recvResult = await cws.ReceiveAsync(recvBuffer, cts.Token);
var message = Encoding.UTF8.GetString(recvBuffer.ToArray(), 0, recvResult.Count);

Assert.Contains(".receiveMessageAfterClose", message);
}
}

[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_CloseDescriptionIsNull_Success(Uri server)
Expand Down
9 changes: 0 additions & 9 deletions src/mono/wasm/runtime/web-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,6 @@ export function ws_wasm_receive(ws: WebSocketExtension, buffer_ptr: VoidPtr, buf
return null;
}

const readyState = ws.readyState;
if (readyState == WebSocket.CLOSED) {
const receive_status_ptr = ws[wasm_ws_receive_status_ptr];
setI32(receive_status_ptr, 0); // count
setI32(<any>receive_status_ptr + 4, 2); // type:close
setI32(<any>receive_status_ptr + 8, 1);// end_of_message: true
return null;
}

const { promise, promise_control } = createPromiseController<void>();
const receive_promise_control = promise_control as ReceivePromiseControl;
receive_promise_control.buffer_ptr = buffer_ptr;
Expand Down