damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 4fecf729639082351646e4c1382875abac8087c7
parent 593d0e2abe61a5b2208214727e1747ba7ecf7888
Author: Bryan Montz <bryanmontz@me.com>
Date:   Fri, 28 Jul 2023 07:34:38 -0500

fix: endless connection attempt loop after user removes relay

This patch fixes an issue where, after the user removes a misbehaving
relay, the RelayConnection will keep trying to reconnect endlessly. You
can reproduce the issue prior to this change by adding the relay
wss://brb.io. It will fail to connect over and over. Then remove the
relay in the UI. In the console, you will see that it keeps trying to
connect, and the corresponding RelayConnection never gets deallocated.
After the change, it stops connecting and deallocates the
RelayConnection.

Changelog-Fixed: endless connection attempt loop after user removes relay
Signed-off-by: Bryan Montz <bryanmontz@me.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Nostr/RelayConnection.swift | 15++++++++++++---
Mdamus/Nostr/RelayPool.swift | 1+
2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/damus/Nostr/RelayConnection.swift b/damus/Nostr/RelayConnection.swift @@ -40,6 +40,7 @@ public struct RelayURL: Hashable { final class RelayConnection: ObservableObject { @Published private(set) var isConnected = false @Published private(set) var isConnecting = false + private var isDisabled = false private(set) var last_connection_attempt: TimeInterval = 0 private(set) var last_pong: Date? = nil @@ -57,7 +58,11 @@ final class RelayConnection: ObservableObject { } func ping() { - socket.ping { err in + socket.ping { [weak self] err in + guard let self else { + return + } + if err == nil { self.last_pong = .now self.log?.add("Successful ping") @@ -103,6 +108,10 @@ final class RelayConnection: ObservableObject { isConnecting = false } + func disablePermanently() { + isDisabled = true + } + func send_raw(_ req: String) { socket.send(.string(req)) } @@ -168,8 +177,8 @@ final class RelayConnection: ObservableObject { } func reconnect() { - guard !isConnecting else { - return // we're already trying to connect + guard !isConnecting && !isDisabled else { + return // we're already trying to connect or we're disabled } disconnect() connect() diff --git a/damus/Nostr/RelayPool.swift b/damus/Nostr/RelayPool.swift @@ -95,6 +95,7 @@ class RelayPool { for relay in relays { if relay.id == relay_id { + relay.connection.disablePermanently() relays.remove(at: i) break }