Skip to content

Commit 254353d

Browse files
Copilotpubkey
andauthored
Preserve live-query re-evaluation when writes race _execOverDatabase() (#8536)
* fix: replace infinite retry loop in _execOverDatabase with counterBefore signal for eventual consistency Agent-Logs-Url: https://github.com/pubkey/rxdb/sessions/e3b3eb19-a9bb-4a2e-ade7-4a0d97dab92f Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com> * test: add regression test for #7067 first-emission stall under concurrent writes Agent-Logs-Url: https://github.com/pubkey/rxdb/sessions/18b5bac4-27b1-4b4f-a295-1f30c3108f7a Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com> * fix: restore fixed wait in query rerun path Agent-Logs-Url: https://github.com/pubkey/rxdb/sessions/e5ba458c-8692-4a6e-9c4c-fc4c9a0c0251 Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com> * test: remove complex #7067 regression case Agent-Logs-Url: https://github.com/pubkey/rxdb/sessions/7f3e4920-b04a-47bf-9b2c-a369a840a9ee Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com> * docs: add changelog entry for issue 8444 Agent-Logs-Url: https://github.com/pubkey/rxdb/sessions/e10a28bf-0e68-41a2-ad64-b8fc905af251 Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com> * docs: correct changelog wording for issue 8444 Agent-Logs-Url: https://github.com/pubkey/rxdb/sessions/698d5cce-d56a-45ec-8dd7-34d782157358 Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com>
1 parent dcfc90e commit 254353d

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- FIX live-query subscriptions being delayed under concurrent writes, by using a fixed `promiseWait(20)` in the `_execOverDatabase()` rerun path instead of increasing the wait time with each rerun [#8444](https://github.com/pubkey/rxdb/issues/8444)

src/rx-query.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,9 @@ export class RxQueryBase<
283283
};
284284

285285
/**
286-
* @performance
287-
* Instead of subscribing to eventBulks$ to detect concurrent writes,
288-
* we snapshot the change event counter before and after the query.
289-
* If the counter changed, a write happened during execution and
290-
* we must re-run the query to ensure correct results.
291-
* This avoids the overhead of RxJS Subject subscribe/unsubscribe per query.
292-
*
293-
* @link https://github.com/pubkey/rxdb/issues/7067
286+
* Snapshot the counter before running the query.
287+
* If a write commits during the query, the counter will be higher
288+
* when we check it below, and we need to signal re-evaluation.
294289
*/
295290
const counterBefore = this.collection._changeEventBuffer.getCounter();
296291

@@ -343,8 +338,22 @@ export class RxQueryBase<
343338
};
344339
}
345340

341+
/**
342+
* If a write committed concurrently during our query, our result may be
343+
* stale: the write's change event was already buffered so getCounter()
344+
* inside queryCollection() advanced _latestChangeEvent past the event,
345+
* which would cause subsequent _ensureEqual() calls to see
346+
* _isResultsInSync()=true and skip re-evaluation entirely.
347+
*
348+
* Re-run the query when a write commits concurrently. We still yield
349+
* with a fixed wait so non-async storages do not monopolize the event
350+
* loop, but we avoid the old growing backoff that could delay
351+
* convergence more and more under write pressure.
352+
*
353+
* @link https://github.com/pubkey/rxdb/issues/7067
354+
*/
346355
if (this.collection._changeEventBuffer.getCounter() !== counterBefore) {
347-
await promiseWait(rerunCount * 20);
356+
await promiseWait(20);
348357
return this._execOverDatabase(rerunCount + 1);
349358
}
350359

0 commit comments

Comments
 (0)