gobwas / ws

Tiny WebSocket library for Go.
MIT License
6.1k stars 373 forks source link

Is WriteServerMessage thread-safe? #176

Open nncsang opened 1 year ago

nncsang commented 1 year ago

I have experienced the "Invalid frame header" and "One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 0" when calling WriteServerMessage concurrently from multiple goroutines.

cristaloleg commented 1 year ago

No, it's not thread safe.

nncsang commented 1 year ago

@cristaloleg is there any function supporting writing message to a connection concurrently?

cristaloleg commented 1 year ago

Just access it with sync.Mutex and voila. You can share a small code snippet, so it will be easier to suggest what/where to change.

mahditakrim commented 1 year ago

@cristaloleg How about ws package?

cristaloleg commented 1 year ago

What exactly?

mahditakrim commented 1 year ago

@cristaloleg is ws write methods thread safe?

cristaloleg commented 1 year ago

Sorry, missed your response. Depends on the param, functions themself are thread-safe, params might not be.

leedstyh commented 1 year ago

hi @cristaloleg , is it thread-safe between wsutil.WriteServerMessage() and wsutil.ReadClientText() ?

I guess wsutil.ReadClientText() writes pong message sometimes.

cristaloleg commented 1 year ago

These are just funcs, they depend only on the given params. If a passed io.ReadWriter is not thread-safe you might get a data race.

I guess wsutil.ReadClientText() writes pong message sometimes.

I don't see any write-specific call in it. Can you clarify why you think it writes pong messages?