From 37ef70e4cbab434e09c5c4c3c5258e788620c7d6 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 19 Jun 2024 09:59:25 -0300 Subject: [PATCH] relay: prevent panic on double-closing. probably fixes https://github.com/nbd-wtf/go-nostr/issues/130 --- relay.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/relay.go b/relay.go index 0715417..c624b27 100644 --- a/relay.go +++ b/relay.go @@ -487,10 +487,20 @@ func (r *Relay) Close() error { defer r.closeMutex.Unlock() if r.connectionContextCancel == nil { + return fmt.Errorf("relay already closed") + } + r.connectionContextCancel() + r.connectionContextCancel = nil + + if r.Connection == nil { return fmt.Errorf("relay not connected") } - r.connectionContextCancel() - r.connectionContextCancel = nil - return r.Connection.Close() + err := r.Connection.Close() + r.Connection = nil + if err != nil { + return err + } + + return nil }