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>
This commit is contained in:
William Casarin
2024-01-10 16:25:07 -08:00
parent 6834367386
commit 4cf92756f1
6 changed files with 30 additions and 7 deletions

View File

@@ -624,7 +624,7 @@ struct ContentView: View {
// out of space or something?? maybe we need a in-memory fallback // out of space or something?? maybe we need a in-memory fallback
if mndb == nil { if mndb == nil {
notify(.logout) logout(nil)
return return
} }
} }
@@ -1082,3 +1082,10 @@ func on_open_url(state: DamusState, url: URL, result: @escaping (OpenResult?) ->
} }
} }
func logout(_ state: DamusState?)
{
state?.close()
notify(.logout)
}

View File

@@ -92,7 +92,12 @@ struct DamusState: HeadlessDamusState {
var is_privkey_user: Bool { var is_privkey_user: Bool {
keypair.privkey != nil keypair.privkey != nil
} }
func close() {
pool.close()
ndb.close()
}
static var empty: DamusState { static var empty: DamusState {
let empty_pub: Pubkey = .empty let empty_pub: Pubkey = .empty
let empty_sec: Privkey = .empty let empty_sec: Privkey = .empty

View File

@@ -39,6 +39,16 @@ class RelayPool {
private let network_monitor_queue = DispatchQueue(label: "io.damus.network_monitor") private let network_monitor_queue = DispatchQueue(label: "io.damus.network_monitor")
private var last_network_status: NWPath.Status = .unsatisfied 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) { init(ndb: Ndb, keypair: Keypair? = nil) {
self.ndb = ndb self.ndb = ndb
self.keypair = keypair self.keypair = keypair

View File

@@ -72,7 +72,7 @@ struct ConfigView: View {
Section(NSLocalizedString("Sign Out", comment: "Section title for signing out")) { Section(NSLocalizedString("Sign Out", comment: "Section title for signing out")) {
Button(action: { Button(action: {
if state.keypair.privkey == nil { if state.keypair.privkey == nil {
notify(.logout) logout(state)
} else { } else {
confirm_logout = true confirm_logout = true
} }
@@ -130,7 +130,7 @@ struct ConfigView: View {
return return
} }
state.postbox.send(ev) state.postbox.send(ev)
notify(.logout) logout(state)
} }
} }
.alert(NSLocalizedString("Logout", comment: "Alert for logging out the user."), isPresented: $confirm_logout) { .alert(NSLocalizedString("Logout", comment: "Alert for logging out the user."), isPresented: $confirm_logout) {
@@ -138,7 +138,7 @@ struct ConfigView: View {
confirm_logout = false confirm_logout = false
} }
Button(NSLocalizedString("Logout", comment: "Button for logging out the user."), role: .destructive) { Button(NSLocalizedString("Logout", comment: "Button for logging out the user."), role: .destructive) {
notify(.logout) logout(state)
} }
} message: { } 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.") 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.")

View File

@@ -164,7 +164,7 @@ struct SideMenuView: View {
Button(action: { Button(action: {
//ConfigView(state: damus_state) //ConfigView(state: damus_state)
if damus_state.keypair.privkey == nil { if damus_state.keypair.privkey == nil {
notify(.logout) logout(damus_state)
} else { } else {
confirm_logout = true confirm_logout = true
} }
@@ -202,7 +202,7 @@ struct SideMenuView: View {
confirm_logout = false confirm_logout = false
} }
Button(NSLocalizedString("Logout", comment: "Button for logging out the user."), role: .destructive) { Button(NSLocalizedString("Logout", comment: "Button for logging out the user."), role: .destructive) {
notify(.logout) logout(damus_state)
} }
} message: { } 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.") 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.")

View File

@@ -176,6 +176,7 @@ class Ndb {
} }
func close() { func close() {
guard !self.closed else { return }
self.closed = true self.closed = true
ndb_destroy(self.ndb.ndb) ndb_destroy(self.ndb.ndb)
} }