Skip to content

fix(electric-db-collection): guard markReady against post-cleanup onError race#1485

Open
prasadpilla wants to merge 1 commit intoTanStack:mainfrom
prasadpilla:fix/on-error-mark-ready-after-cleanup
Open

fix(electric-db-collection): guard markReady against post-cleanup onError race#1485
prasadpilla wants to merge 1 commit intoTanStack:mainfrom
prasadpilla:fix/on-error-mark-ready-after-cleanup

Conversation

@prasadpilla
Copy link
Copy Markdown

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 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:

onError: (errorParams) => {
  markReady()  // ← unconditional; throws if collection is already cleaned-up
  ...
}

abortController is already in scope here — its abort() is already called during cleanup() — but there was no guard before calling markReady().

Fix

// Before
markReady()

// After
if (!abortController.signal.aborted) markReady()

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 AbortController into the mock, calls cleanup(), then fires onError and 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 remaining onError → markReady() path that #1174 did not address.

Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally.
  • This change affects published code, and I have generated a changeset (if required).

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant