fix race condition on ticker ping.

fixes nevent1qqszqakrmmjyjelgp06f3hx0fu70xyvmqt4ed8v65xpmpy9g8nj9qgqpz4mhxue69uhkummnw3ezummcw3ezuer9wchsz9nhwden5te0dehhxarjv4kxjar9wvhx7un89uq32amnwvaz7tmjv4kxz7fwv3sk6atn9e5k7tcnynjra
This commit is contained in:
fiatjaf 2024-11-24 14:49:21 -03:00
parent 72c7892a8b
commit 98d3766474

View File

@ -182,11 +182,13 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
for {
select {
case <-ticker.C:
err := wsutil.WriteClientMessage(r.Connection.conn, ws.OpPing, nil)
if err != nil {
InfoLogger.Printf("{%s} error writing ping: %v; closing websocket", r.URL, err)
r.Close() // this should trigger a context cancelation
return
if r.Connection != nil {
err := wsutil.WriteClientMessage(r.Connection.conn, ws.OpPing, nil)
if err != nil {
InfoLogger.Printf("{%s} error writing ping: %v; closing websocket", r.URL, err)
r.Close() // this should trigger a context cancelation
return
}
}
case writeRequest := <-r.writeQueue:
// all write requests will go through this to prevent races