Skip to content

Error while concurrently writing to WebSocket using WriteMessageToSocket #1573

Closed
@deadland2002

Description

@deadland2002

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.
  1. The code is
func test(gofrCtx *gofr.Context){
     err = gofrCtx.WriteMessageToSocket(wsMessage)
}   
  1. 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()
}   

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions