Skip to content

fix(database): keep active queries marked active after getData - #17009

Merged
ncooke3 merged 2 commits into
mainfrom
nc.database.getdata-active-query
Nov 5, 2026
Merged

ncooke3 merged 2 commits into
mainfrom
nc.database.getdata-active-query

Conversation

@ncooke3

@ncooke3 ncooke3 commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #16722. Routes getDataWithCompletionBlock: query-activity updates through FSyncTree (-setQueryActive: / -setQueryInactive:, named after Android's SyncTree.setQueryActive / setQueryInactive, with -setQueryInactive: intentionally guarded while a matching view remains active since iOS calls it on getData completion) instead of calling FPersistenceManager directly from FRepo.

Previously, when getData missed the in-memory cache, -[FRepo getData:withCompletionBlock:] unconditionally called [self.persistenceManager setQueryInactive:querySpec] on completion (both success and error paths), even when self.serverSyncTree still held an active observer or keepSynced:YES view for that query:

  • For filtered queries (!loadsAllData) with persistenceEnabled = YES, subsequent writes or server updates adding/removing tracked keys in that active view tripped NSAssert(trackedQuery.isActive) in -[FPersistenceManager updateTrackedQueryKeysWithAddedKeys:removedKeys:forQuery:] and allowed cache pruning to evict the active query.
  • For unfiltered queries (loadsAllData), it prematurely marked an actively observed location inactive/prunable.

Testing

  • Added 3 unit tests in FRepoGetDataPersistenceTests covering succeeded and failed getData calls alongside active observers and keepSynced:YES on both filtered and default queries.
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.
@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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 paulb777 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comments

  1. PR description (should fix). The JS SDK has no syncTreeSetQueryActive/syncTreeSetQueryInactive methods; it has no persistence layer at all.
    • These names match Android's SyncTree, but there setQueryInactive is unconditional, and Android's Repo.getValue never 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.
  2. Document the conditional behavior (suggestion). FSyncTree's setQueryInactive: has the same name as FPersistenceManager's but only runs when no view needs the query, while setQueryActive: 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 loadsAllData checks for any complete view (the normalization above).
  3. Test nits
    • Unordered results: after the write or removal, the tests only check trackedQueries.firstObject (L157, L183, L213). But loadTrackedQueries returns dictionary allValues, which has no fixed order. If a second tracked query ever appears, firstObject could be the wrong one. Re-assert count == 1, or look the query up by spec.
    • Unneeded drains: the empty dispatch_sync blocks (L152, L178) don't change anything. setValue:, removeObserverWithHandle: and keepSynced: all queue onto the same serial queue, so they already run in order. Harmless, but a named helper would read better.
  4. Follow-up (existing issue, not a blocker). The fix stops getData from 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.
  5. 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.
@ncooke3
ncooke3 marked this pull request as ready for review November 5, 2026 02:14
@ncooke3
ncooke3 merged commit 8e9d34a into main Nov 5, 2026
82 checks passed
@ncooke3
ncooke3 deleted the nc.database.getdata-active-query branch November 5, 2026 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants