tmc / grpc-websocket-proxy

A proxy to transparently upgrade grpc-gateway streaming endpoints to use websockets
MIT License
553 stars 72 forks source link

fix write concurrency to websocket #28

Open jkralik opened 3 years ago

jkralik commented 3 years ago

according to https://pkg.go.dev/github.com/gorilla/websocket#hdr-Concurrency, concurrency is not supported by WriteMessage.

fixes issue with nginx proxy: SSL routines:ssl3_get_record:decryption failed or bad record mac

OS-M commented 1 year ago

Hi! Thank you for contributing and fixing that concurrency bug but unfortunately your solution introduces another one. As scanner.Bytes() returns Go slice (aka pointer to an array) it can be overwritten by another call to scanner.Scan() which ends up with corrupting data inside the chan before it is written to the conn by another goroutine. The solution here is to copy the array before adding it to the chan:

dataToWrite := make([]byte, len(scanner.Bytes()))
copy(dataToWrite, scanner.Bytes())
dataWriteChan <- dataToWrite