Skip to content

[BUG]: PGLite Sync - commitUpToLsn not called after 409 must-refetch when up-to-date LSN equals lastCommittedLsn #879

Closed

Description

@john-rtr

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

  1. Create a sync subscription with a persistent key
  2. Complete initial sync (handles saved to electric.subscriptions_metadata)
  3. Server invalidates shape handles (compaction, restart, etc.)
  4. Refresh page → client uses old handles → server returns 409
  5. Data is re-fetched successfully
  6. Refresh page again → 409 repeats
  7. 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) = false
  • isMustRefetchAndCatchingUp = (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

  1. added
    bug
    on Jan 20, 2026
  2. john-rtr commented on Jan 21, 2026

    @john-rtr
    Author

    Closing this, it looks like my issue was because of using pglite with indexDB & relaxedDurability when refreshing the browser before DB being able to flush to indexDB.
    Closing pglite during beforeunload event seems to fix this.

  3. barbalex commented on Jan 26, 2026

    @barbalex

    we also seem to be encountering this
    how do you close pglite beforeunload?

  4. barbalex commented on Jan 26, 2026

    @barbalex

    it seems this works. thanks @john-rtr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions