Check if Ndb is closed before running subscribe and query operations

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 <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2025-09-26 12:04:51 -07:00
parent 9153a912b0
commit 258d08723f

View File

@@ -623,6 +623,7 @@ class Ndb {
/// - Returns: Array of note keys matching the filters /// - Returns: Array of note keys matching the filters
/// - Throws: NdbStreamError if the query fails /// - Throws: NdbStreamError if the query fails
func query<Y>(with txn: NdbTxn<Y>, filters: [NdbFilter], maxResults: Int) throws(NdbStreamError) -> [NoteKey] { func query<Y>(with txn: NdbTxn<Y>, filters: [NdbFilter], maxResults: Int) throws(NdbStreamError) -> [NoteKey] {
guard !self.is_closed else { throw .ndbClosed }
let filtersPointer = UnsafeMutablePointer<ndb_filter>.allocate(capacity: filters.count) let filtersPointer = UnsafeMutablePointer<ndb_filter>.allocate(capacity: filters.count)
defer { filtersPointer.deallocate() } defer { filtersPointer.deallocate() }
@@ -636,6 +637,7 @@ class Ndb {
let results = UnsafeMutablePointer<ndb_query_result>.allocate(capacity: maxResults) let results = UnsafeMutablePointer<ndb_query_result>.allocate(capacity: maxResults)
defer { results.deallocate() } 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 { guard ndb_query(&txn.txn, filtersPointer, Int32(filters.count), results, Int32(maxResults), count) == 1 else {
throw NdbStreamError.initialQueryFailed throw NdbStreamError.initialQueryFailed
} }
@@ -707,6 +709,7 @@ class Ndb {
} }
func subscribe(filters: [NdbFilter], maxSimultaneousResults: Int = 1000) throws(NdbStreamError) -> AsyncStream<StreamItem> { func subscribe(filters: [NdbFilter], maxSimultaneousResults: Int = 1000) throws(NdbStreamError) -> AsyncStream<StreamItem> {
guard !self.is_closed else { throw .ndbClosed }
// Fetch initial results // Fetch initial results
guard let txn = NdbTxn(ndb: self) else { throw .cannotOpenTransaction } guard let txn = NdbTxn(ndb: self) else { throw .cannotOpenTransaction }
@@ -914,6 +917,7 @@ extension Ndb {
case initialQueryFailed case initialQueryFailed
case timeout case timeout
case cancelled case cancelled
case ndbClosed
} }
/// An error that may happen when looking something up /// An error that may happen when looking something up