Skip to content

Commit

Permalink
Add warning log if websocket closing is stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 5, 2023
1 parent e99578a commit d075cff
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions appservice/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,15 @@ func (as *AppService) StartWebsocket(baseURL string, onConnect func()) error {
closeChanOnce := sync.Once{}
stopFunc := func(err error) {
closeChanOnce.Do(func() {
closeChan <- err
select {
case closeChan <- err:
default:
as.Log.Warn().
AnErr("close_error", err).
Msg("Nothing is reading on close channel")
closeChan <- err
as.Log.Warn().Msg("Websocket close completed after being stuck")
}
})
}
as.ws = ws
Expand All @@ -384,11 +392,20 @@ func (as *AppService) StartWebsocket(baseURL string, onConnect func()) error {

go as.consumeWebsocket(stopFunc, ws)

var onConnectDone atomic.Bool
if onConnect != nil {
onConnect()
go func() {
onConnect()
onConnectDone.Store(true)
}()
} else {
onConnectDone.Store(true)
}

closeErr := <-closeChan
if onConnectDone.Load() {
as.Log.Warn().Msg("Websocket closed before onConnect returned, things may explode")
}

if as.ws == ws {
as.clearWebsocketResponseWaiters()
Expand Down

0 comments on commit d075cff

Please sign in to comment.