fix(electric-db-collection): guard markReady against post-cleanup onError race#1485
Open
prasadpilla wants to merge 1 commit intoTanStack:mainfrom
Open
fix(electric-db-collection): guard markReady against post-cleanup onError race#1485prasadpilla wants to merge 1 commit intoTanStack:mainfrom
prasadpilla wants to merge 1 commit intoTanStack:mainfrom
Conversation
…rror race ## Problem When `collection.cleanup()` is called (e.g. on logout or team switch), the `AbortController` is signalled to stop the `ShapeStream`. However, there is a race condition: `onError` can fire after `cleanup()` has run but before the `ShapeStream` observes the abort signal. The `onError` handler unconditionally calls `markReady()`, which throws: ``` CollectionStateError: Invalid collection status transition from "cleaned-up" to "ready" ``` This surfaces as an unhandled exception in applications (e.g. captured by PostHog exception autocapture) and pollutes error tracking dashboards with noise that has no actionable fix from the application side. ## Fix Add an `abortController.signal.aborted` guard before calling `markReady()` in the `onError` handler. `abortController` is already in scope and its `abort()` method is already called during `cleanup()`, so the guard is zero-overhead and correct: ```typescript // Before markReady() // After if (!abortController.signal.aborted) markReady() ``` ## Test Added regression test: "should not throw CollectionStateError when onError fires after cleanup" The test wires a real `AbortController` into the mock, calls `cleanup()`, then fires `onError` and asserts no exception is thrown. ## Related - PR TanStack#1174 fixed similar abort-race issues in `requestSnapshot` / `fetchSnapshot`, but the `onError` → `markReady()` path was not covered.
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.
Problem
When
collection.cleanup()is called (e.g. on logout or team switch), theAbortControlleris signalled to stop theShapeStream. However, there is a race condition:onErrorcan fire aftercleanup()has run but before theShapeStreamobserves the abort signal.The
onErrorhandler unconditionally callsmarkReady(), which throws:This surfaces as an unhandled exception in applications and pollutes error tracking dashboards (e.g. PostHog, Sentry) with noise that has no actionable fix from the application side.
Root cause
In
packages/electric-db-collection/src/electric.ts:abortControlleris already in scope here — itsabort()is already called duringcleanup()— but there was no guard before callingmarkReady().Fix
One-liner. Zero new imports or complexity.
Test
Added regression test: "should not throw CollectionStateError when onError fires after cleanup"
The test wires a real
AbortControllerinto the mock, callscleanup(), then firesonErrorand asserts no exception is thrown.Relation to PR #1174
PR #1174 fixed the same class of abort-race issue for
requestSnapshot/fetchSnapshot(in-flight network calls suppressed on abort). This PR covers the remainingonError → markReady()path that #1174 did not address.Checklist