From 56b1efc6f1f44991d4f5a2d169b726f310616dc7 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 25 Jan 2024 12:06:35 -0800 Subject: [PATCH] txn: do not close txn if database is already closed This is a potential fix for some of the crash reports that have been streaming in. They all seem to be crashing within ndb_close_txn. I suspect this means that the transactions are trying to get closed when ndb is already closed. Closing Ndb will close the transactions automatically, so it looks like we might be trying to close twice. --- damus/Models/Mute/MuteManager.swift | 8 ++++++++ nostrdb/NdbTxn.swift | 13 ++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 damus/Models/Mute/MuteManager.swift diff --git a/damus/Models/Mute/MuteManager.swift b/damus/Models/Mute/MuteManager.swift new file mode 100644 index 00000000..11f03879 --- /dev/null +++ b/damus/Models/Mute/MuteManager.swift @@ -0,0 +1,8 @@ +// +// MuteManager.swift +// damus +// +// Created by William Casarin on 2024-01-25. +// + +import Foundation diff --git a/nostrdb/NdbTxn.swift b/nostrdb/NdbTxn.swift index 76e9ccaf..3660f30f 100644 --- a/nostrdb/NdbTxn.swift +++ b/nostrdb/NdbTxn.swift @@ -17,9 +17,11 @@ class NdbTxn { private var val: T! var moved: Bool var inherited: Bool + var ndb: Ndb init?(ndb: Ndb, with: (NdbTxn) -> T = { _ in () }) { guard !ndb.closed else { return nil } + self.ndb = ndb #if TXNDEBUG txn_count += 1 print("opening transaction \(txn_count)") @@ -41,11 +43,12 @@ class NdbTxn { self.val = with(self) } - private init(txn: ndb_txn, val: T) { + private init(ndb: Ndb, txn: ndb_txn, val: T) { self.txn = txn self.val = val self.moved = false self.inherited = false + self.ndb = ndb } /// Only access temporarily! Do not store database references for longterm use. If it's a primitive type you @@ -56,7 +59,7 @@ class NdbTxn { } deinit { - if moved || inherited { + if moved || inherited || ndb.closed { return } @@ -71,14 +74,14 @@ class NdbTxn { // functor func map(_ transform: (T) -> Y) -> NdbTxn { self.moved = true - return .init(txn: self.txn, val: transform(val)) + return .init(ndb: self.ndb, txn: self.txn, val: transform(val)) } // comonad!? // useful for moving ownership of a transaction to another value func extend(_ with: (NdbTxn) -> Y) -> NdbTxn { self.moved = true - return .init(txn: self.txn, val: with(self)) + return .init(ndb: self.ndb, txn: self.txn, val: with(self)) } } @@ -101,7 +104,7 @@ extension NdbTxn where T: OptionalType { return nil } self.moved = true - return NdbTxn(txn: self.txn, val: unwrappedVal) + return NdbTxn(ndb: self.ndb, txn: self.txn, val: unwrappedVal) } }