relay: prevent panic on double-closing.

probably fixes https://github.com/nbd-wtf/go-nostr/issues/130
This commit is contained in:
fiatjaf
2024-06-19 09:59:25 -03:00
parent d6d02686a9
commit 37ef70e4cb

View File

@@ -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
}