From 98d37664746a83028964ed2d6743ac4e64ffcb7a Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 24 Nov 2024 14:49:21 -0300 Subject: [PATCH] fix race condition on ticker ping. fixes nevent1qqszqakrmmjyjelgp06f3hx0fu70xyvmqt4ed8v65xpmpy9g8nj9qgqpz4mhxue69uhkummnw3ezummcw3ezuer9wchsz9nhwden5te0dehhxarjv4kxjar9wvhx7un89uq32amnwvaz7tmjv4kxz7fwv3sk6atn9e5k7tcnynjra --- relay.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/relay.go b/relay.go index 5825689..0319f24 100644 --- a/relay.go +++ b/relay.go @@ -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