grpc-websocket-proxy
grpc-websocket-proxy copied to clipboard
fix write concurrency to websocket
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
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