Repository navigation
Description
Description
After recovering from a 409 (must-refetch), if the server's LSN equals the previously stored lastCommittedLsn, the new shape handles are not persisted to the metadata table. This causes an infinite 409 loop on every page refresh until a new LSN arrives from the server.
Environment
@electric-sql/pglite-sync: 0.4.1@electric-sql/pglite: 0.3.15
Steps to Reproduce
- Create a sync subscription with a persistent
key - Complete initial sync (handles saved to
electric.subscriptions_metadata) - Server invalidates shape handles (compaction, restart, etc.)
- Refresh page → client uses old handles → server returns 409
- Data is re-fetched successfully
- Refresh page again → 409 repeats
- Loop continues until server has a new LSN
Expected Behavior
After 409 recovery, new shape handles should be persisted. Next page refresh should use the new valid handles and sync normally.
Actual Behavior
New handles are not persisted. Every page refresh triggers 409 until an unrelated data change causes a new LSN.
Root Cause
In packages/pglite-sync/src/index.ts, handles are persisted inside commitUpToLsn. The commit conditions are:
const isCommitNeeded = lowestCommittedLsn > lastCommittedLsn
const isMustRefetchAndCatchingUp =
lowestCommittedLsn >= lastCommittedLsn && truncateNeeded.size > 0
if (isCommitNeeded || isMustRefetchAndCatchingUp) {
commitUpToLsn(lowestCommittedLsn) // This persists handles
}After 409:
| Step | State | Commit? |
|---|---|---|
must-refetch received |
truncateNeeded = [shape], completeLsn = -1 |
- |
| Data arrives (LSN = X) | completeLsn = X |
✅ Yes (truncateNeeded.size > 0) |
| Inside commit | truncateNeeded.clear(), lastCommittedLsn = X |
- |
up-to-date (LSN = X) |
isUpToDate = true |
❌ No |
The problem: commitUpToLsn is called when data arrives, but at that point multiShapeStream.shapes[name].shapeHandle may not yet have the new handle from the 409 response. The handle is updated asynchronously by the ShapeStream.
When up-to-date arrives (handles now updated), no commit is triggered because:
isCommitNeeded = (X > X)= falseisMustRefetchAndCatchingUp = (X >= X && 0 > 0)= false
Result: New handles never persisted → 409 on next refresh.
Suggested Fix
Ensure a commit happens after up-to-date when recovering from must-refetch:
// Track if we had a must-refetch this session
let hadMustRefetch = false
// In must-refetch handler:
hadMustRefetch = true
// In commit condition:
const mustRefetchRecoveryComplete =
hadMustRefetch &&
multiShapeStream.isUpToDate &&
lowestCommittedLsn >= BigInt(0)
if (isCommitNeeded || isMustRefetchAndCatchingUp || mustRefetchRecoveryComplete) {
commitUpToLsn(lowestCommittedLsn)
if (mustRefetchRecoveryComplete) hadMustRefetch = false
}Workaround
None reliable. Users must wait for an unrelated data change to trigger a commit that persists the handles.
Note: Bug analysis was made with Claude Code
Activity
john-rtr commentedon Jan 21, 2026
Closing this, it looks like my issue was because of using pglite with indexDB &
relaxedDurabilitywhen refreshing the browser before DB being able to flush to indexDB.Closing pglite during
beforeunloadevent seems to fix this.barbalex commentedon Jan 26, 2026
we also seem to be encountering this
how do you close pglite beforeunload?
barbalex commentedon Jan 26, 2026
it seems this works. thanks @john-rtr