-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[ws] Fix debugging web socket JS code + add test #99240
Conversation
Tagging subscribers to this area: @dotnet/ncl Issue DetailsFollow up for #96618.
|
WebSocketMessageType.Text, | ||
true, | ||
CancellationToken.None); | ||
await Task.Delay(1990); // try to sync with receive request from the client: 1.9k is too little, 2k too much |
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.
This is the tricky part. What would happen if the wait was not there at all ?
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.
We would have sent one after another, so it would be event order:
- connect, ask for receive 1, send1, send2, ask for receive 2.
Adding this delay we are on the boarder of the above scenario and the below:
- connect, ask for receive 1, send1, ask for receive 2, send 2.
By "on the boarder" I mean it's random, sometimes we fall into 1st scenario, sometimes into the 2nd.
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.
ask for receive 2
should have no impact on the JS side order of WS events and JS side buffering, right ?
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.
Are we trying to get into situation that "ws.state == CLOSE" but the on_message
for the second message was not called yet ?
If that's possible, the current implementation is wrong. But I need to see it before I can believe it 🔍
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.
The time when we ask for receive seems important, when using the debugger, we enter the receiving method before we got "on_message_sent" event but after the WS is closed. That's why I was trying to time it as close in time to closing and sending actions as possible.
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.
Anyway, we could try to stop using ws.state for this and rely on the on_close
event
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.
Removing the if (readyState == WebSocket.CLOSED)
block of code from ws_wasm_receive
eliminates the exception during debugging.
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.
Does it mean that
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 resolvedPromise();
}
is redundant? Managed code does no need setting these bits?
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.
That's good question. on_close
has similar code and would deliver the resolution if we deleted this.
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.
There is a difference between the demo and the tests: demo does not close the socket, only dispose of it and in case the socket was opened without using CloseAsync()
, the WebSocket connection will be abruptly terminated. This termination will occur without following the WebSocket protocol's normal closure handshake, potentially leading to unexpected behavior or errors on the client side. Upon correcting the demo code to close the socket (like our tests do), I cannot reproduce the issue with "lost messages". The current, working version of code has the if (readyState == WebSocket.CLOSED)
block removed.
Follow up for #96618. The test tries to resemble the actions from demo as much as possible. The goal is to reproduce
Observed when debugging: receive and send+close WS are launched nearly at the same time and we enter "ws_wasm_receive" with empty event buffer (no on_message event were recorded previously) but also with WS state as "closed" (so obviously the "send" action was already triggered but did not get recorded by the browser).