Closed
Description
Describe the bug
When multiple goroutines attempt to write data to a WebSocket simultaneously using WriteMessageToSocket(wsMessage)
, it throws an error.
To Reproduce
- Use multiple goroutines to write data to a WebSocket.
- Call
gofrCtx.WriteMessageToSocket(wsMessage)
inside each goroutine. - Observe the error.
- The code is
func test(gofrCtx *gofr.Context){
err = gofrCtx.WriteMessageToSocket(wsMessage)
}
- The error is
panic: concurrent write to websocket connection
goroutine 434 [running]: github.com/gorilla/websocket.(*messageWriter).flushFrame(0x14000c93d28, 0x1, {0x140006c8953?, 0x1033e6634?, 0x1039f79e0?})
Expected behavior
Concurrent writes to a Web socket should not cause errors. The function should handle multiple goroutines writing data safely without requiring external synchronization.
Environments (please complete the following information):
- OS : MacOS
- gofr version : v1.35.0
- go version : go1.24.1 darwin/arm64
More description
i solved the problem temporarily by using
func test(gofrCtx *gofr.Context){
mu.Lock()
err = gofrCtx.WriteMessageToSocket(wsMessage)
mu.Unlock()
}