gobwas / ws

Tiny WebSocket library for Go.
MIT License
6.14k stars 376 forks source link

[BUG] 0 is invalid utf8 #133

Closed ioworker0 closed 3 years ago

ioworker0 commented 3 years ago

There was a bug that stuck us for a few days. The error message shows as follows:

0 is invalid utf8

This bug is hard to find through testing alone, because it is triggered by low-probability events.

But, let's take a closer look at the code below:

https://github.com/gobwas/ws/blob/2effe5ec7e4fd737085eb56beded3ad6491269dc/write.go#L90-L97

WriteFrame() line:90
      \ -> WriteHeader() line:91
      \ --------------------------- At the same time, if someone writes again, then something terrible happens.
      \ -> WritePayload() line:95

So, it's necessary that we write every frame in a concurrency-safe way.

P.S. we figured it out with a lock.

cristaloleg commented 3 years ago

From what I understand, WriteFrame isn't designed to be concurrent safe and it's documentation says nothing about thread safety. Isn't this an expected behaviour?

ioworker0 commented 3 years ago

From what I understand, WriteFrame isn't designed to be concurrent safe and it's documentation says nothing about thread safety. Isn't this an expected behaviour?

Thank you for your immediate response!

I just hope this helps someone else and I didn't know it is thread-unsafe.