gobwas / ws

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

panic at too more ws client #149

Closed poolqa closed 2 years ago

poolqa commented 2 years ago

go: 1.17.6 gobwas : v1.1.0 os: centos 7

when I open ws connection more then 500, I would probably get panic.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x66142d]

goroutine 464660 [running]:
github.com/gobwas/ws.WriteHeader({0x0, 0x0}, {0x1, 0x0, 0x1, 0x1, {0xaf, 0x22, 0x5c, 0x4}, ...})
    /root/go/pkg/mod/github.com/gobwas/ws@v1.1.0/write.go:84 +0x14d
github.com/gobwas/ws.WriteFrame({0x0, 0x0}, {{0x1, 0x0, 0x1, 0x1, {0xaf, 0x22, 0x5c, 0x4}, ...}, ...})
    /root/go/pkg/mod/github.com/gobwas/ws@v1.1.0/write.go:91 +0x2c
github.com/gobwas/ws/wsutil.writeFrame({0x0, 0x0}, 0x0, 0x1, 0x1, {0xc000b0d040, 0x44, 0x28})
    /root/go/pkg/mod/github.com/gobwas/ws@v1.1.0/wsutil/writer.go:570 +0x316
github.com/gobwas/ws/wsutil.WriteMessage(...)
    /root/go/pkg/mod/github.com/gobwas/ws@v1.1.0/wsutil/helper.go:161
github.com/gobwas/ws/wsutil.WriteClientMessage(...)
    /root/go/pkg/mod/github.com/gobwas/ws@v1.1.0/wsutil/helper.go:185

example:

func main() {
        wg := &sync.WaitGroup{}
    for i := 0; i <= 1000; i++ {
                wg.Add(1)
        go wsClient()
    }
       wg.Wait()
}

func wsClient() error {
  ctx := context.Background()
  wsConn, _, _, err = ws.Dial(ctx, wsUri)
  if err != nil {
    return err
  }
 // do some thing
}
poolqa commented 2 years ago

I found the bug, that is my code issue. when my ws client got recv error, it will release ws connection. even though it checked ws connection is not nil, it still got nil ws connection.