commit 4cf92756f1593b8ffe9f8f2284de37c92b2708ac
parent 683436738606a6a3689200005d039f90d502a7c9
Author: William Casarin <jb55@jb55.com>
Date: Wed, 10 Jan 2024 16:25:07 -0800
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 <jb55@jb55.com>
Diffstat:
6 files changed, 30 insertions(+), 7 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -176,6 +176,7 @@ class Ndb {
}
func close() {
+ guard !self.closed else { return }
self.closed = true
ndb_destroy(self.ndb.ndb)
}