Repository navigation
fix(database): keep active queries marked active after getData - #17009
Conversation
Route getData query-activity updates through FSyncTree so completing a one-shot get does not mark a query inactive in FPersistenceManager while an active view (observer or keepSynced) still exists at that path. Previously, FRepo called FPersistenceManager directly, causing active queries to become prunable and triggering an NSAssert crash in FPersistenceManager when tracked keys were subsequently updated on an active filtered view.
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. |
paulb777
left a comment
There was a problem hiding this comment.
Review: #17009 — fix(database): keep active queries marked active after getData
Verdict: LGTM. No blockers. The PR description has one inaccuracy to fix, plus a few suggestions and nits. It's a draft by ncooke3, one commit on top of #16722.
What it does: When getData finished, it unconditionally marked its query inactive in persistence, even if an observer or keepSynced still used that query. Both calls now go through new methods, -[FSyncTree setQueryActive:/setQueryInactive:]. The inactive call is skipped while a matching view still exists: the exact view for filtered queries, or any complete view for loadsAllData queries.
The logic is correct. It uses the same rule as the existing removal path in -[FSyncPoint removeEventRegistration:…]. There, a filtered query goes inactive when its view empties, and the default query only when the last complete view goes. That fits normalizeQuery:, which folds every loadsAllData query into the default tracked query. Nothing can get stuck active, because the view that keeps the query alive marks it inactive when it's removed.
When the bug happens: only when getData misses the in-memory cache. That means the observer's view isn't complete yet: its listen is pending, or the app is offline with no complete disk cache. The success-path call dates back to #7264 (7.4.0). #16833 (13.1.1) added the failure-path call, which makes the offline keepSynced case easy to hit.
Verification
I ran DatabaseUnit/FRepoGetDataPersistenceTests through SPM on an iPhone 17 Pro simulator (iOS 26.5):
- PR head: all 4 tests pass.
- With the fix disabled (
setQueryInactive:made unconditional again, like before the PR): all 3 new tests fail, and the existing test still passes.- Both filtered-query tests crash with
'We only expect tracked keys for currently-active queries.'at FPersistenceManager.m:224. - So the tests really do catch the regression. I restored the file afterwards.
- Both filtered-query tests crash with
Comments
- PR description (should fix). The JS SDK has no
syncTreeSetQueryActive/syncTreeSetQueryInactivemethods; it has no persistence layer at all.- These names match Android's
SyncTree, but theresetQueryInactiveis unconditional, and Android'sRepo.getValuenever calls it. - So Android doesn't have this bug. It does leave the query marked active after a failed get, which is what #16833 fixed here.
- Suggest citing Android and saying the conditional check is intentional.
- These names match Android's
- Document the conditional behavior (suggestion).
FSyncTree'ssetQueryInactive:has the same name asFPersistenceManager's but only runs when no view needs the query, whilesetQueryActive:just passes through.- A short doc comment in FSyncTree.h would help, e.g. "no-op while an observer or keepSynced view still needs the query."
- It should also explain why
loadsAllDatachecks for any complete view (the normalization above).
- Test nits
- Unordered results: after the write or removal, the tests only check
trackedQueries.firstObject(L157, L183, L213). ButloadTrackedQueriesreturns dictionaryallValues, which has no fixed order. If a second tracked query ever appears,firstObjectcould be the wrong one. Re-assertcount == 1, or look the query up by spec. - Unneeded drains: the empty
dispatch_syncblocks (L152, L178) don't change anything.setValue:,removeObserverWithHandle:andkeepSynced:all queue onto the same serial queue, so they already run in order. Harmless, but a named helper would read better.
- Unordered results: after the write or removal, the tests only check
- Follow-up (existing issue, not a blocker). The fix stops
getDatafrom clearing an observer's active flag, but not the other way round. The get's own "active" mark isn't reference-counted.- If an observer is removed mid-request, or two gets overlap, the query goes inactive while the get is still in flight.
- If a cache prune then removes it before a failed get finishes, the final call hits this
NSAssert. The old code had the same risk.
- CHANGELOG (optional). Mentioning the visible symptom would help users match their crash: the debug assertion message above, or the active query's disk cache getting pruned.
Document conditional setQueryInactive: behavior and loadsAllData normalization on FSyncTree, look up tracked queries by FQuerySpec in FRepoGetDataPersistenceTests instead of relying on unordered dictionary values, and clarify symptoms in CHANGELOG.md.
Summary
Follow-up to #16722. Routes
getDataWithCompletionBlock:query-activity updates throughFSyncTree(-setQueryActive:/-setQueryInactive:, named after Android'sSyncTree.setQueryActive/setQueryInactive, with-setQueryInactive:intentionally guarded while a matching view remains active since iOS calls it ongetDatacompletion) instead of callingFPersistenceManagerdirectly fromFRepo.Previously, when
getDatamissed the in-memory cache,-[FRepo getData:withCompletionBlock:]unconditionally called[self.persistenceManager setQueryInactive:querySpec]on completion (both success and error paths), even whenself.serverSyncTreestill held an active observer orkeepSynced:YESview for that query:!loadsAllData) withpersistenceEnabled = YES, subsequent writes or server updates adding/removing tracked keys in that active view trippedNSAssert(trackedQuery.isActive)in-[FPersistenceManager updateTrackedQueryKeysWithAddedKeys:removedKeys:forQuery:]and allowed cache pruning to evict the active query.loadsAllData), it prematurely marked an actively observed location inactive/prunable.Testing
FRepoGetDataPersistenceTestscovering succeeded and failedgetDatacalls alongside active observers andkeepSynced:YESon both filtered and default queries.