damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit d04a29405d3c7bdad72e8e529fef3d52bbead891
parent d3c75ce42b8ddedcb70ead7dc5d015fb39f1067d
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 26 Jan 2024 11:58:48 -0800

txn: don't attempt to close transactions from older db generations

Diffstat:
Mnostrdb/Ndb.swift | 6+++++-
Mnostrdb/NdbTxn.swift | 11+++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift @@ -31,6 +31,7 @@ class Ndb { var ndb: ndb_t let path: String? let owns_db: Bool + var generation: Int var closed: Bool static func safemode() -> Ndb? { @@ -124,7 +125,8 @@ class Ndb { guard let db = Self.open(path: path, owns_db_file: owns_db_file) else { return nil } - + + self.generation = 0 self.path = path self.owns_db = owns_db_file self.ndb = db @@ -170,6 +172,7 @@ class Ndb { init(ndb: ndb_t) { self.ndb = ndb + self.generation = 0 self.path = nil self.owns_db = true self.closed = false @@ -189,6 +192,7 @@ class Ndb { return false } + self.generation += 1 self.closed = false self.ndb = db return true diff --git a/nostrdb/NdbTxn.swift b/nostrdb/NdbTxn.swift @@ -18,10 +18,12 @@ class NdbTxn<T> { var moved: Bool var inherited: Bool var ndb: Ndb + var generation: Int init?(ndb: Ndb, with: (NdbTxn<T>) -> T = { _ in () }) { guard !ndb.closed else { return nil } self.ndb = ndb + self.generation = ndb.generation #if TXNDEBUG txn_count += 1 print("opening transaction \(txn_count)") @@ -30,14 +32,18 @@ class NdbTxn<T> { // some parent thread is active, use that instead self.txn = active_txn self.inherited = true + self.generation = Thread.current.threadDictionary["txn_generation"] as! Int } else { self.txn = ndb_txn() guard !ndb.closed else { return nil } + self.generation = ndb.generation let ok = ndb_begin_query(ndb.ndb.ndb, &self.txn) != 0 if !ok { return nil } + self.generation = ndb.generation Thread.current.threadDictionary["ndb_txn"] = self.txn + Thread.current.threadDictionary["txn_generation"] = ndb.generation self.inherited = false } self.moved = false @@ -50,6 +56,7 @@ class NdbTxn<T> { self.moved = false self.inherited = false self.ndb = ndb + self.generation = 0 } /// Only access temporarily! Do not store database references for longterm use. If it's a primitive type you @@ -60,6 +67,10 @@ class NdbTxn<T> { } deinit { + if self.generation != ndb.generation { + //print("txn: OLD GENERATION (\(self.generation) != \(ndb.generation)), IGNORING") + return + } if moved || inherited || ndb.closed { return }