content icon indicating copy to clipboard operation
content copied to clipboard

Issue with "Writing a WebSocket server in C#": example code has race condition

Open marmyr opened this issue 4 years ago • 1 comments

MDN URL: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_server

What information was incorrect, unhelpful, or incomplete?

Bug in code

Specific section or headline?

C# code example

What did you expect to see?

Code without a race condition

Did you test this? If so, how?

Yes, try spamming the websocket with data:

        byte[] bytes = new byte[client.Available];
        stream.Read(bytes, 0, client.Available);

The size in "client.Available" may have changed between allocating the byte array, and reading the data. The safe way is to read bytes.Length of bytes.

MDN Content page report details
  • Folder: en-us/web/api/websockets_api/writing_websocket_server
  • MDN URL: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_server
  • GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/api/websockets_api/writing_websocket_server/index.html
  • Last commit: https://github.com/mdn/content/commit/ba4a07195d8f8f4941e7ae06118b8405f9cac9cf
  • Document last modified: 2021-04-10T23:58:36.000Z

marmyr avatar May 01 '21 15:05 marmyr

Hi @marmyr thanks a lot for creating this issue. Would you like to open a pull request that addresses the problem?

bsmth avatar Nov 02 '22 15:11 bsmth