Skip to content
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

websockets - calling dataAvailableForRead can cause errors #39

Open
schveiguy opened this issue Jul 7, 2024 · 0 comments
Open

websockets - calling dataAvailableForRead can cause errors #39

schveiguy opened this issue Jul 7, 2024 · 0 comments

Comments

@schveiguy
Copy link

If you have a simple web page which just sends hello every 1 msec, and then you have the server read in a loop like this:

void handleConn(scope WebSocket sock)
{
    while (sock.connected) {
        if(sock.dataAvailableForRead) {
            auto msg = sock.receiveText();
            writeln(msg, " msg.length=", msg.length);
        }
        sleep(1.msecs);
    }
}

You will run into a crash.

The issue is that when a WebSocket is created, it starts a read task, which reads messages as they come in.

But the call to dataAvailableForRead boils down to also trying a read if there is no data available.

Having both these tasks reading at the same time causes this assert to fail:

https://github.com/vibe-d/vibe-core/blob/master/source/vibe/core/net.d#L663

The sequence of events is:

  1. Task A gets the next frame off the web socket via
    skipFrame(); // reads the first frame
    . This starts a read call on the socket, which asynchronously waits for data
  2. Task B calls dataAvailableForRead on the socket, which also sets up a read call.
  3. The web messages are coming in with the correct timing such that both reads succeed, but not in sync. Therefore, the assert is hit because there is still data on the buffer.

At least, I believe this is the case, it is hard to tell.

I think the correct fix is to have dataAvailableForRead to avoid attempting a read on websockets, and just only check for buffer data available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant