fasthttp / websocket

WebSocket implementation for fasthttp.
BSD 2-Clause "Simplified" License
540 stars 55 forks source link

sometimes have a panic when client unregistering #13

Closed maxjust closed 1 year ago

maxjust commented 5 years ago

a random panic with this stack trace, cant reproduce it just a random, and happen from time to time

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x1adec3e]
goroutine 79691 [running]:
github.com/valyala/fasthttp.(*hijackConn).SetWriteDeadline(0xc000bcdf80, 0xbf5cbb4f4adda610, 0x238e4ae57c47, 0x397c860, 0x2, 0x0)
         <autogenerated>:1 +0x2e
 github.com/fasthttp/websocket.(*Conn).write(0xc000c40dc0, 0x8, 0xbf5cbb4f4adda610, 0x238e4ae57c47, 0x397c860, 0xc000cfd30c, 0x2, 0x1002, 0x39a0e10, 0x0, ...)
         /home/max/go/pkg/mod/github.com/fasthttp/websocket@v1.4.1/conn.go:389 +0x11f
 github.com/fasthttp/websocket.(*messageWriter).flushFrame(0xc00100fe20, 0x39a0e01, 0x39a0e10, 0x0, 0x0, 0x0, 0xcbbea8)
         /home/max/go/pkg/mod/github.com/fasthttp/websocket@v1.4.1/conn.go:614 +0x1f3
 github.com/fasthttp/websocket.(*Conn).WriteMessage(0xc000c40dc0, 0x8, 0x39a0e10, 0x0, 0x0, 0x238e4ae57c47, 0x397c860)
         /home/max/go/pkg/mod/github.com/fasthttp/websocket@v1.4.1/conn.go:763 +0x23d
 main/websocks.(*Client).StartWriter(0xc000f52180)
         /main/websocks/client.go:127 +0x7eb

can anybody help or maybe have an idea how to fix?

savsgio commented 5 years ago

Could you share your code? please

maxjust commented 5 years ago

I just use this example https://github.com/fasthttp/websocket/tree/master/examples/chat/fasthttp

kirillDanshin commented 5 years ago

@savsgio @maxjust I've managed to reproduce the issue.

just run this script in a browser console several times:

for (let i = 0; i < 40000; i++) {
  (new WebSocket(`ws://${document.location.host}/ws`)).onopen = () => (
    conn.send('asdfasdf')
  )
}

note: browser console should be opened in the context of this example page (e.g. localhost:8080)

OPTIONPOOL commented 4 years ago

I'm having the same issue. Has anyone solved this?

savsgio commented 4 years ago

Hi @OPTIONPOOL,

Could you explain your case, please?

OPTIONPOOL commented 4 years ago

Hi @OPTIONPOOL,

Could you explain your case, please?

This is my code, and the crash's stack trace is exactly the same as the above example starting from conn.WriteMessage.

if conn != nil {
    connectionMutex.Lock()
    err := conn.WriteMessage(1, []byte(response))
    connectionMutex.Unlock()
}
savsgio commented 4 years ago

Which versions are you using of the websocket and fasthttp packages??

OPTIONPOOL commented 4 years ago

Which versions are you using of the websocket and fasthttp packages??

These are the packages that I am currently using.

github.com/fasthttp/websocket v1.4.1 github.com/valyala/fasthttp v1.8.0 github.com/qiangxue/fasthttp-routing v0.0.0-20160225050629-6ccdc2a18d87

OPTIONPOOL commented 4 years ago

Any progress so far?

savsgio commented 4 years ago

I have been researching about it but I don't get to reproduce the error in my laptop. Could you share your code and the panic stack trace, please?

azlan commented 4 years ago

@OPTIONPOOL, my quick solution is

    if c == nil {
        return errors.New("Connection already dropped")
    }

at https://github.com/fasthttp/websocket/blob/master/conn.go#L752

om26er commented 1 year ago

I see this issue in 1.5.1, I don't know the exact reason, though it is possible my automatic ping code tries to send a ping message even though the connection was closed. Wonder if there is a definitive way to know if the connection has closed ?

Feb 13 21:55:18 Hub: panic: runtime error: invalid memory address or nil pointer dereference
Feb 13 21:55:18 Hub: [signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x3863ec]
Feb 13 21:55:18 Hub: goroutine 301 [running]:
Feb 13 21:55:18 Hub: github.com/valyala/fasthttp.(*hijackConn).SetWriteDeadline(0x0?, {0x40025e720c?, 0x40025cf628?, 0x0?})
Feb 13 21:55:18 Hub:         <autogenerated>:1 +0x2c
Feb 13 21:55:18 Hub: github.com/fasthttp/websocket.(*Conn).write(0x40023bef20, 0x9, {0x40001869e0?, 0x400349f800?, 0x0?}, {0x40025e720c, 0x2, 0x402}, {0x0, 0x0, ...})
Feb 13 21:55:18 Hub:         /home/om26er/go/pkg/mod/github.com/fasthttp/websocket@v1.5.1/conn.go:416 +0x190
Feb 13 21:55:18 Hub: github.com/fasthttp/websocket.(*messageWriter).flushFrame(0x4002837ec8, 0x1, {0x0?, 0x19b2c?, 0x4003227748?})
Feb 13 21:55:18 Hub:         /home/om26er/go/pkg/mod/github.com/fasthttp/websocket@v1.5.1/conn.go:661 +0x360
Feb 13 21:55:18 Hub: github.com/fasthttp/websocket.(*Conn).WriteMessage(0x40023bef20, 0x448a48?, {0x0, 0x0, 0x0})
Feb 13 21:55:18 Hub:         /home/om26er/go/pkg/mod/github.com/fasthttp/websocket@v1.5.1/conn.go:815 +0xfc
Feb 13 21:55:18 Hub: github.com/code/connect/transport.(*FastHTTPPeer).Ping(0x53a82eb794f2f?, {0x0?, 0x554289?, 0x0?})
Feb 13 21:55:18 Hub:         /home/om26er/scm/scratch/connect/transport/fasthttpwebsocketpeer.go:89 +0x38
Feb 13 21:55:18 Hub: github.com/code/connect/server.(*ConnectionHandler).startAutoPinger.func1()
Feb 13 21:55:18 Hub:         /home/om26er/scm/scratch/connect/server/connectionhandler.go:333 +0xd4
Feb 13 21:55:18 Hub: created by github.com/code/connect/server.(*ConnectionHandler).startAutoPinger
Feb 13 21:55:18 Hub:         /home/om26er/scm/scratch/connect/server/connectionhandler.go:323 +0xac
savsgio commented 1 year ago

Hi @om26er,

Sorry for my delayed answer!

Could you share the ping handler code, please?

Remember that websocket connections are not safe concurrent, so if you are starting a custom ping handler in a second gorutine into the upgrader you will get race conditions, so you must use this to ping connections (https://pkg.go.dev/github.com/fasthttp/websocket#Conn.SetPingHandler)

Also see: https://pkg.go.dev/github.com/fasthttp/websocket#hdr-Concurrency