pajlada / go-twitch-pubsub

Twitch PubSub library for Go
MIT License
21 stars 9 forks source link

Gorilla - concurrent write to websocket connection #32

Closed andoleal closed 11 months ago

andoleal commented 11 months ago

Got this error while using go-twitch-pubsub to read moderation events from Twitch

[go-twitch-pubsub] Lost connection, will try to reconnect
panic: concurrent write to websocket connection
goroutine 86520 [running]:
github.com/gorilla/websocket.(*messageWriter).flushFrame(0xc00015da40, 0x1, {0x0?, 0x0?, 0x0?})
         /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:617 +0x52b
 github.com/gorilla/websocket.(*messageWriter).Close(0xc000382fc0?)
         /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:731 +0x45
 github.com/gorilla/websocket.(*Conn).beginMessage(0xc000f44000, 0xc000382fc0, 0x1)
        /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:480 +0x42
 github.com/gorilla/websocket.(*Conn).NextWriter(0xc000f44000, 0x1)
        /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:520 +0x45
 github.com/gorilla/websocket.(*Conn).WriteMessage(0xc000035f98?, 0xc000035f78?, {0xc000d4ebb0, 0xf, 0x10})
         /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:773 +0x152
 github.com/pajlada/go-twitch-pubsub.(*connection).startWriter(0xc00007f540)
        /root/go/pkg/mod/github.com/pajlada/go-twitch-pubsub@v0.1.0/connection.go:223 +0x47
 created by github.com/pajlada/go-twitch-pubsub.(*connection).connect
        /root/go/pkg/mod/github.com/pajlada/go-twitch-pubsub@v0.1.0/connection.go:193 +0xf6

A quick Google search produced this, might be helpful: https://github.com/gorilla/websocket/issues/119

As for my code, I basically use the example:

func (a *twitchModeration) runPubSubClient() {
    pubsubClient := pubsub.NewClient(pubsub.DefaultHost)
    pubsubClient.Listen(pubsub.ModerationActionTopic(a.moderatorId, a.broadcasterId), a.config.Token)
    pubsubClient.OnModerationAction(func(channelID string, event *pubsub.ModerationAction) {
        a.processPubSubEvent(event)
    })

    go pubsubClient.Start()
}
pajlada commented 11 months ago

v0.1.1 is being released and contains this fix - let me know if this reappears. Thanks for the detailed report!