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 does not support concurrent sends #1196

Open
attdona opened this issue Nov 21, 2024 · 1 comment · May be fixed by #1197
Open

WebSockets does not support concurrent sends #1196

attdona opened this issue Nov 21, 2024 · 1 comment · May be fixed by #1197

Comments

@attdona
Copy link

attdona commented Nov 21, 2024

First of all, thanks for the great HTTP.jl package, I really appreciate this project!

I've noticed an issue with the WebSocket implementation: when multiple async tasks send WebSocket messages, a race condition may occur, causing the frames to become corrupted.

Below a minimal working example that exposes the issue (HTTP.jl v1.10.10).

using HTTP.WebSockets

function write_message(ws, msg)
    send(ws, msg)
end

function client_twin(ws)
    for count in 1:10
        @async write_message(ws, count)
    end
end

function serve()
    server = WebSockets.listen!("127.0.0.1", 8081) do ws
        client_twin(ws)
        # just for waiting
        receive(ws)
    end

    wait(server)
end

srvtask = @async serve()

WebSockets.open("ws://127.0.0.1:8081") do ws
    try
        while true
            s = receive(ws)
            println(s)
        end
    catch e
        @error "Ws client: $e"
    end
end;

wait(srvtask)
[ Info: Listening on: 127.0.0.1:8081, thread id: 3
┌ Error: Ws client: HTTP.WebSockets.WebSocketError(HTTP.WebSockets.CloseFrameBody(1007, "Invalid UTF-8"))

I've a forked HTTP.jl with a fix: cardo-org@d60bb5e

Would you be open to reviewing the fix and eventually accept a pull request?

greetings,
Attilio

@fredrikekre
Copy link
Member

That fix looks fine to me.

@attdona attdona linked a pull request Nov 22, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants