From 4fecf729639082351646e4c1382875abac8087c7 Mon Sep 17 00:00:00 2001 From: Bryan Montz Date: Fri, 28 Jul 2023 07:34:38 -0500 Subject: [PATCH] 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 Signed-off-by: William Casarin --- damus/Nostr/RelayConnection.swift | 15 ++++++++++++--- damus/Nostr/RelayPool.swift | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/damus/Nostr/RelayConnection.swift b/damus/Nostr/RelayConnection.swift index 926e6415..b02ce592 100644 --- 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 index f697f791..59d98439 100644 --- 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 }