Repository navigation
Description
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.
-
The truncation of a refetched table is committed on its own.
lastCommittedLsnis aconst(subState?.last_lsn ?? BigInt(-1)). The code never advances it after a commit. Without a stored subscription it stays-1for the lifetime of the subscription. Onmust-refetchthe shape's complete LSN is reset to-1. At that pointisMustRefetchAndCatchingUp(lowestCommittedLsn >= lastCommittedLsn && truncateNeeded.size > 0) is-1 >= -1, which is already true, socommitUpToLsn(-1)runs and commits theDELETEwith 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 aShapeStreamthat ended on a non-retried error, such as a response body that broke off (FetchError"Failed to fetch" on a 200). BecauselastCommittedLsnnever advances,isCommitNeeded(lowestCommittedLsn > lastCommittedLsn) also becomes true on every batch, which starts a commit even when nothing new is complete. -
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.onErroris never called, and the subscription keeps applying later changes on top of the gap. -
A
must-refetchthat arrives while a commit is queued truncates under that commit.truncateNeededis read inside the transaction, not whencommitUpToLsncollects its messages. Suppose commit A has collected the rows up to LSN n and is waiting for the database (another transaction is running), and amust-refetcharrives 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 withduplicate 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-refetchfor both shapes, plus the refetched row andup-to-datefor 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 anintegercolumn). Actual:Unhandled Rejection: invalid input syntax for type integer, andonErroris never called. Expected:onErroris called and the subscription stops. - (3) Hold the database with
pg.transaction(...). Feedinsert id 1+up-to-date, thenmust-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-sync0.4.1, and the code is unchanged onmain(0.6.9)@electric-sql/pglite0.3.x / 0.5.x- Electric 1.8.1,
@electric-sql/client1.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