From 4cf92756f1593b8ffe9f8f2284de37c92b2708ac Mon Sep 17 00:00:00 2001 From: William Casarin Date: Wed, 10 Jan 2024 16:25:07 -0800 Subject: [PATCH] close nostrdb and disconnect from relays on logout This was causing crashing and corruption issues. This should have been handled by the garbage collector, but for some reason old references still hang around. Add a "close" method to DamusState which disconnects from relays and closes nostrdb. Changelog-Fixed: Fix crash when logging out and switching accounts Changelog-Fixed: Fix persistent local notifications even after logout Signed-off-by: William Casarin --- damus/ContentView.swift | 9 ++++++++- damus/Models/DamusState.swift | 7 ++++++- damus/Nostr/RelayPool.swift | 10 ++++++++++ damus/Views/ConfigView.swift | 6 +++--- damus/Views/SideMenuView.swift | 4 ++-- nostrdb/Ndb.swift | 1 + 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/damus/ContentView.swift b/damus/ContentView.swift index e7e0a61c..69cb8330 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -624,7 +624,7 @@ struct ContentView: View { // out of space or something?? maybe we need a in-memory fallback if mndb == nil { - notify(.logout) + logout(nil) return } } @@ -1082,3 +1082,10 @@ func on_open_url(state: DamusState, url: URL, result: @escaping (OpenResult?) -> } } + +func logout(_ state: DamusState?) +{ + state?.close() + notify(.logout) +} + diff --git a/damus/Models/DamusState.swift b/damus/Models/DamusState.swift index 14d3fe91..3e5b4120 100644 --- a/damus/Models/DamusState.swift +++ b/damus/Models/DamusState.swift @@ -92,7 +92,12 @@ struct DamusState: HeadlessDamusState { var is_privkey_user: Bool { keypair.privkey != nil } - + + func close() { + pool.close() + ndb.close() + } + static var empty: DamusState { let empty_pub: Pubkey = .empty let empty_sec: Privkey = .empty diff --git a/damus/Nostr/RelayPool.swift b/damus/Nostr/RelayPool.swift index 23a1e8c1..385a6c54 100644 --- a/damus/Nostr/RelayPool.swift +++ b/damus/Nostr/RelayPool.swift @@ -39,6 +39,16 @@ class RelayPool { private let network_monitor_queue = DispatchQueue(label: "io.damus.network_monitor") private var last_network_status: NWPath.Status = .unsatisfied + func close() { + disconnect() + relays = [] + handlers = [] + request_queue = [] + seen.removeAll() + counts = [:] + keypair = nil + } + init(ndb: Ndb, keypair: Keypair? = nil) { self.ndb = ndb self.keypair = keypair diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift index 76129b56..6ffaf066 100644 --- a/damus/Views/ConfigView.swift +++ b/damus/Views/ConfigView.swift @@ -72,7 +72,7 @@ struct ConfigView: View { Section(NSLocalizedString("Sign Out", comment: "Section title for signing out")) { Button(action: { if state.keypair.privkey == nil { - notify(.logout) + logout(state) } else { confirm_logout = true } @@ -130,7 +130,7 @@ struct ConfigView: View { return } state.postbox.send(ev) - notify(.logout) + logout(state) } } .alert(NSLocalizedString("Logout", comment: "Alert for logging out the user."), isPresented: $confirm_logout) { @@ -138,7 +138,7 @@ struct ConfigView: View { confirm_logout = false } Button(NSLocalizedString("Logout", comment: "Button for logging out the user."), role: .destructive) { - notify(.logout) + logout(state) } } message: { Text("Make sure your nsec account key is saved before you logout or you will lose access to this account", comment: "Reminder message in alert to get customer to verify that their private security account key is saved saved before logging out.") diff --git a/damus/Views/SideMenuView.swift b/damus/Views/SideMenuView.swift index ca5968f4..8a0e3cdd 100644 --- a/damus/Views/SideMenuView.swift +++ b/damus/Views/SideMenuView.swift @@ -164,7 +164,7 @@ struct SideMenuView: View { Button(action: { //ConfigView(state: damus_state) if damus_state.keypair.privkey == nil { - notify(.logout) + logout(damus_state) } else { confirm_logout = true } @@ -202,7 +202,7 @@ struct SideMenuView: View { confirm_logout = false } Button(NSLocalizedString("Logout", comment: "Button for logging out the user."), role: .destructive) { - notify(.logout) + logout(damus_state) } } message: { Text("Make sure your nsec account key is saved before you logout or you will lose access to this account", comment: "Reminder message in alert to get customer to verify that their private security account key is saved saved before logging out.") diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift index d0d95c51..e1e968e4 100644 --- a/nostrdb/Ndb.swift +++ b/nostrdb/Ndb.swift @@ -176,6 +176,7 @@ class Ndb { } func close() { + guard !self.closed else { return } self.closed = true ndb_destroy(self.ndb.ndb) }