Fix requestSnapshot publish ordering#4282
Open
KyleAMathews wants to merge 3 commits intomainfrom
Open
Conversation
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4282 +/- ##
===========================================
+ Coverage 58.64% 70.21% +11.56%
===========================================
Files 218 53 -165
Lines 22112 7007 -15105
Branches 5698 1981 -3717
===========================================
- Hits 12968 4920 -8048
+ Misses 9140 2084 -7056
+ Partials 4 3 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix
requestSnapshot()so callers can safely read subscribed state immediately after awaiting it.Previously,
requestSnapshot()fetched and injected snapshot data into the stream but did not await the async subscriber publication chain. This meant consumers such as TanStack DB on-demand loading could observe stale state immediately afterawait requestSnapshot().Root Cause
requestSnapshot()called#onMessages(...)without awaiting it. That method publishes through the stream’s serialized subscriber chain, so async subscribers could still be processing the injected snapshot batch afterrequestSnapshot()had already resolved.Adding the await exposed a second edge case: if a subscriber reentrantly called and awaited
requestSnapshot(), the nested publish could be appended behind the currently-running subscriber callback, causing a deadlock.Approach
#onMessages(...)inrequestSnapshot()so the promise resolves only after the injected snapshot batch has been delivered to subscribers.#publish()reentrant-safe by delivering nested publishes immediately when already inside subscriber delivery, avoiding self-deadlock.Shapevisibility, and reentrantrequestSnapshot()calls.Key Invariants
await requestSnapshot()means subscriber callbacks for the injected snapshot batch have completed.snapshot-endandsubset-endare delivered beforerequestSnapshot()resolves.requestSnapshot()calls from subscriber callbacks do not deadlock.Non-goals
Trade-offs
The fix keeps the main publish path serialized while special-casing reentrant delivery to avoid deadlock. Throwing on reentrant
requestSnapshot()would have avoided the deadlock too, but it would introduce a new API restriction. Supporting it is safer and more ergonomic for subscribers.Verification
Results:
Also red/green verified the reentrant regression: the new reentrant test fails with the old chained publish implementation and passes with the reentrant-safe publish fix.
Files changed
packages/typescript-client/src/client.tsrequestSnapshot().#publish()reentrant-safe.packages/typescript-client/test/stream.test.tsrequestSnapshot()regression coverage.packages/typescript-client/test/client.test.tsawait requestSnapshot()are verified..changeset/fix-requestsnapshot-publish-ordering.md@electric-sql/client.