Skip to content

Cached credentials are reused after expiresAt, so the first reconnect after a long outage gets a 401 #1165

Open

Description

@amehmeto

Summary

Since #604, AbstractRemote.getCredentials() returns the cached credentials whenever they exist and never reads PowerSyncCredentials.expiresAt. When the sync stream stays down for longer than the token lifetime, the next connection attempt sends a token the connector already declared expired. The service answers 401 PSYNC_S2103 JWT has expired, the SDK invalidates the credentials, and the attempt after that succeeds.

The SDK recovers on its own, so the impact is small: one failed attempt and one Sync stream failed error per long outage. But the connector told the SDK when the token expires, and the SDK drops that information.

Environment

  • @powersync/react-native 2.3.3, @powersync/common 2.4.0, @powersync/shared-internals 1.4.0
  • Firebase Auth ID tokens, which live one hour. fetchCredentials returns { endpoint, token, expiresAt }, with expiresAt taken from the token's exp claim.

What happens

  1. The stream is open and the token is cached. token_expires_in keeps it fresh while the stream stays open.
  2. The stream drops on network errors (unexpected end of stream, Software caused connection abort) and keeps retrying for more than one hour.
  3. The next attempt calls getCredentials(), which returns the cached, expired token.
  4. The service returns 401 PSYNC_S2103 JWT has expired. The SDK calls invalidateCredentials(), and the next retry fetches a new token and connects.

We see this exact sequence in our production error tracking.

Cause

packages/shared-internals/src/client/sync/stream/AbstractRemote.ts on main:

async getCredentials(signal?: AbortSignal): Promise<PowerSyncCredentials | null> {
  if (this.credentials) {
    return this.credentials;
  }

  return this.prefetchCredentials(signal);
}

Before #604, this method returned the cache only while expiresAt was more than 30 seconds ahead. #604 moved invalidation to token_expires_in and to 401 responses, which also removed the extra fetches reported in #528. Neither signal arrives while the stream is down, and nothing in the client reads the credentials' expiresAt anymore, although the field is still part of the public PowerSyncCredentials type (see #1134).

Proposed fix

Keep the #604 behaviour when expiresAt is absent, and stop reusing the cache once expiresAt has passed:

async getCredentials(signal?: AbortSignal): Promise<PowerSyncCredentials | null> {
  const expiresAt = this.credentials?.expiresAt;
  const hasExpired = expiresAt != null && expiresAt.getTime() <= Date.now();
  if (this.credentials && !hasExpired) {
    return this.credentials;
  }

  return this.prefetchCredentials(signal);
}

Connectors that do not return expiresAt see no change, so the #528 concern does not come back. We ship this change as a local patch on 1.4.0, with a test that fails without it. I can open a PR with the change and a test if the approach suits you.

Activity

  1. simolus3 commented on Oct 24, 2026

    @simolus3
    Contributor

    Thanks for the report! expiresAt is an optional field we don't use in any other SDK either (and I don't think we should start decoding the JWT).

    We are working on an update to the backend connector system that allows interfaces where it would be your responsibility to cache tokens, which makes it easier to take their expiration time into account.

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