python-hyper / wsproto

Sans-IO WebSocket protocol implementation
https://wsproto.readthedocs.io/
MIT License
261 stars 38 forks source link

Not thread safe #186

Closed maartenbreddels closed 6 months ago

maartenbreddels commented 11 months ago

Hi,

I'm using flask_sock (cc @miguelgrinberg) which uses simple-websocket, which uses this project, and we found some flakeyness in our CI for Solara.

About 1 in 5 runs on CI and 1 in 50 runs on our local machines gave corrupt websocket messages, which triggered errors in json parsing.

I believe the reason is because FrameProtocol is not thread safe due to _outbound_opcode. See

I'm not sure if this should be documented or if this can be fixed. The workaround for us now is to put a mutex around the send and close calls:

Do you think this is sufficient? We only call receive from the same thread, but send will only also be called from other threads.

I would love to get your opinions on this.

Regards,

Maarten

Kriechi commented 6 months ago

Yes, a mutex is one possible option. Happy to review and merge a PR to mention it in the docs.