Skip to content

[BUG]: pglite-sync: after a must-refetch a synced table can stay empty; commit errors are swallowed #1125

Open

Description

@jeromerg

Describe the bug

syncShapesToTables (packages/pglite-sync/src/index.ts) has three defects in its commit path. Together they can turn a 409 must-refetch into an empty or partially lost table, with no error reported through onError.

  1. The truncation of a refetched table is committed on its own. lastCommittedLsn is a const (subState?.last_lsn ?? BigInt(-1)). The code never advances it after a commit. Without a stored subscription it stays -1 for the lifetime of the subscription. On must-refetch the shape's complete LSN is reset to -1. At that point isMustRefetchAndCatchingUp (lowestCommittedLsn >= lastCommittedLsn && truncateNeeded.size > 0) is -1 >= -1, which is already true, so commitUpToLsn(-1) runs and commits the DELETE with no rows. The refetched rows are committed only once every shape of the subscription has a complete LSN again. If the other shapes refetch slowly, the table is empty in the meantime. If one shape never catches up, the table stays empty while the other shapes keep polling normally. One way a shape fails to catch up is a ShapeStream that ended on a non-retried error, such as a response body that broke off (FetchError "Failed to fetch" on a 200). Because lastCommittedLsn never advances, isCommitNeeded (lowestCommittedLsn > lastCommittedLsn) also becomes true on every batch, which starts a commit even when nothing new is complete.

  2. A failed commit is an unhandled rejection. commitUpToLsn(lowestCommittedLsn) is neither awaited nor given a .catch. When the transaction fails, the promise rejection goes unhandled. The collected messages have already been removed from the buffer, so those rows are lost. onError is never called, and the subscription keeps applying later changes on top of the gap.

  3. A must-refetch that arrives while a commit is queued truncates under that commit. truncateNeeded is read inside the transaction, not when commitUpToLsn collects its messages. Suppose commit A has collected the rows up to LSN n and is waiting for the database (another transaction is running), and a must-refetch arrives for the same shape before A runs. A then performs the truncation and inserts its old rows. The next commit (the refetched snapshot) no longer truncates and fails with duplicate key value violates unique constraint. Through (2), that failure is also silent.

To Reproduce

The tests below use the package's existing MultiShapeStream mock in test/sync.test.ts. They are part of the linked PR.

  • (1) Two shapes. Sync one row into each table. Then deliver must-refetch for both shapes, plus the refetched row and up-to-date for the first shape only. Actual: both tables are empty. Expected: both keep their old rows until the second shape has refetched, and then truncation and new rows land in one transaction.
  • (2) Feed a row whose value the column type rejects (for example id: 'not-a-number' into an integer column). Actual: Unhandled Rejection: invalid input syntax for type integer, and onError is never called. Expected: onError is called and the subscription stops.
  • (3) Hold the database with pg.transaction(...). Feed insert id 1 + up-to-date, then must-refetch + the refetched snapshot (id 1, id 2) + up-to-date, then release the transaction. Actual: Unhandled Rejection: duplicate key value violates unique constraint "todo_pkey", and the table keeps the old row. Expected: the table holds the refetched snapshot.

In our application, a server-side shape invalidation (an Electric restart) left tabs showing empty tables until reload. We saw this in about 1 of 15 tab-rounds, with all shapes polling normally. With the fix applied as a local patch to 0.4.1 (Electric 1.8.1, ten forced 409 rounds × five tabs, with @electric-sql/client 1.2.0 and 1.5.28), 0 of 50 tab-rounds per client were empty or stale.

Details

  • @electric-sql/pglite-sync 0.4.1, and the code is unchanged on main (0.6.9)
  • @electric-sql/pglite 0.3.x / 0.5.x
  • Electric 1.8.1, @electric-sql/client 1.2.0 and 1.5.28
  • Browser (Chromium) and Node 22 (tests)

Additional context

Related but different: #879, which is about handles not being persisted after a 409 and was closed as caused by relaxedDurability.

One more observation from reading the code, not reproduced: when a stored subscription is resumed, shape_metadata holds each stream's current lastOffset at the time of the last commit. That offset can be ahead of the committed LSN when messages were buffered but not yet committed, and those buffered messages are not delivered again on resume.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions