From 258d08723fd3cf5afa2010a0051390b5d0b452ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=E2=80=99Aquino?= Date: Fri, 26 Sep 2025 12:04:51 -0700 Subject: [PATCH] Check if Ndb is closed before running subscribe and query operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should prevent background crashes caused by race conditions between usages of Ndb and the Ndb/app lifecycle operations. Signed-off-by: Daniel D’Aquino --- nostrdb/Ndb.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift index 134d4f0d..0dabf95d 100644 --- a/nostrdb/Ndb.swift +++ b/nostrdb/Ndb.swift @@ -623,6 +623,7 @@ class Ndb { /// - Returns: Array of note keys matching the filters /// - Throws: NdbStreamError if the query fails func query(with txn: NdbTxn, filters: [NdbFilter], maxResults: Int) throws(NdbStreamError) -> [NoteKey] { + guard !self.is_closed else { throw .ndbClosed } let filtersPointer = UnsafeMutablePointer.allocate(capacity: filters.count) defer { filtersPointer.deallocate() } @@ -636,6 +637,7 @@ class Ndb { let results = UnsafeMutablePointer.allocate(capacity: maxResults) defer { results.deallocate() } + guard !self.is_closed else { throw .ndbClosed } guard ndb_query(&txn.txn, filtersPointer, Int32(filters.count), results, Int32(maxResults), count) == 1 else { throw NdbStreamError.initialQueryFailed } @@ -707,6 +709,7 @@ class Ndb { } func subscribe(filters: [NdbFilter], maxSimultaneousResults: Int = 1000) throws(NdbStreamError) -> AsyncStream { + guard !self.is_closed else { throw .ndbClosed } // Fetch initial results guard let txn = NdbTxn(ndb: self) else { throw .cannotOpenTransaction } @@ -914,6 +917,7 @@ extension Ndb { case initialQueryFailed case timeout case cancelled + case ndbClosed } /// An error that may happen when looking something up