Skip to content

fix(api-graphql): trigger WebSocket reconnection on auth errors - #14569

Merged
bobbor merged 3 commits into
aws-amplify:mainfrom
anivar:fix/datastore-sync-processor-retry-12954
Mar 23, 2026
Merged

fix(api-graphql): trigger WebSocket reconnection on auth errors#14569
bobbor merged 3 commits into
aws-amplify:mainfrom
anivar:fix/datastore-sync-processor-retry-12954

Conversation

@anivar

@anivar anivar commented Sep 26, 2025

Copy link
Copy Markdown
Contributor

Description of changes

After extended idle periods or device sleep/wake cycles (~12-24h), WebSocket subscriptions receive auth errors when tokens expire but nothing triggers reconnection — subscriptions silently die while the app reports everything is connected.

Fix: Detect auth errors in the WebSocket provider's subscription error handler using exact errorType matching and close the socket. The existing onclose_errorDisconnect(CONNECTION_CLOSED)ConnectionStateMonitorReconnectionMonitor flow then reconnects with fresh tokens automatically.

What changed:

  • Added auth error detection in AWSWebSocketProvider._handleIncomingSubscriptionMessage() for both EVENT_SUBSCRIBE_ERROR and GQL_ERROR message types
  • Matches exact error types: UnauthorizedException, Unauthorized, NotAuthorizedException
  • Also matches Token expired in error message field
  • Closes socket on auth error to trigger existing reconnection flow
  • No changes needed in DataStore — the socket close already produces CONNECTION_CLOSED which DataStore's disconnectionHandler handles

What was removed vs original PR:

  • Removed "401" / "403" substring matching (false positive risk — would match modelIds like "item-4013")
  • Removed duplicate auth detection in DataStore's disconnectionHandler (redundant — WebSocket close already triggers CONNECTION_CLOSED)

Issue #, if available

Fixes #12954

Description of how you validated changes

  • All 42 AWSAppSyncRealTimeProvider tests pass
  • All 40 DataStore connectivityHandling tests pass

Checklist

  • PR description included
  • yarn test passes
  • Unit Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@anivar
anivar requested a review from a team as a code owner September 26, 2025 16:25
@anivar anivar changed the title fix(datastore): fix WebSocket reconnection after token expiration fix(api-graphql): trigger WebSocket reconnection on auth errors Sep 26, 2025
@bobbor bobbor self-assigned this Dec 3, 2025
@anivar
anivar force-pushed the fix/datastore-sync-processor-retry-12954 branch from c7e92eb to a5e1fbb Compare March 7, 2026 13:07
@changeset-bot

changeset-bot Bot commented Mar 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 31df4fd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@aws-amplify/api-graphql Patch
@aws-amplify/api Patch
@aws-amplify/datastore Patch
aws-amplify Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Detect authentication errors (UnauthorizedException, NotAuthorizedException,
Token expired) in subscription error responses using exact errorType matching
and close the WebSocket to trigger reconnection with fresh tokens.

Handles both EVENT_SUBSCRIBE_ERROR (structured errors array) and GQL_ERROR
(payload errors) message types.

The socket close fires the existing CONNECTION_CLOSED → ConnectionDisrupted →
ReconnectionMonitor flow, so no changes needed in DataStore's disconnectionHandler.

Fixes aws-amplify#12954
@anivar
anivar force-pushed the fix/datastore-sync-processor-retry-12954 branch from a5e1fbb to 9377435 Compare March 7, 2026 13:50
@anivar

anivar commented Mar 7, 2026

Copy link
Copy Markdown
Contributor Author

This PR addresses the root cause described in #14587 (OIDC subscription UnauthorizedException) — WebSocket auth errors during token expiry now trigger proper reconnection instead of silently dropping subscriptions.

@bobbor bobbor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the contribution. LGTM

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.

DataStore sync processor rety error - about every 24 hours

3 participants