ndb/txn: make transactions failable
Since there may be situations where we close and re-open the database,
we need to make sure transactions fail when the database is not open.
Make NdbTxn an init?() constructor and check for ndb.closed. If it's
closed, then fail transaction construction.
This fixes crashes during high database activity when switching from
background to foreground and vice-versa.
Fixes: da2bdad18d ("nostrdb: close database when backgrounded")
This commit is contained in:
@@ -18,8 +18,9 @@ class NdbTxn<T> {
|
||||
var moved: Bool
|
||||
var inherited: Bool
|
||||
|
||||
init(ndb: Ndb, with: (NdbTxn<T>) -> T = { _ in () }) {
|
||||
#if TXNDEBUG
|
||||
init?(ndb: Ndb, with: (NdbTxn<T>) -> T = { _ in () }) {
|
||||
guard !ndb.closed else { return nil }
|
||||
#if TXNDEBUG
|
||||
txn_count += 1
|
||||
print("opening transaction \(txn_count)")
|
||||
#endif
|
||||
@@ -31,11 +32,7 @@ class NdbTxn<T> {
|
||||
self.txn = ndb_txn()
|
||||
let ok = ndb_begin_query(ndb.ndb.ndb, &self.txn) != 0
|
||||
if !ok {
|
||||
self.moved = false
|
||||
self.txn = ndb_txn()
|
||||
self.inherited = true
|
||||
self.val = with(self)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
Thread.current.threadDictionary["ndb_txn"] = self.txn
|
||||
self.inherited = false
|
||||
|
||||
Reference in New Issue
Block a user