Repository navigation
Description
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-native2.3.3,@powersync/common2.4.0,@powersync/shared-internals1.4.0- Firebase Auth ID tokens, which live one hour.
fetchCredentialsreturns{ endpoint, token, expiresAt }, withexpiresAttaken from the token'sexpclaim.
What happens
- The stream is open and the token is cached.
token_expires_inkeeps it fresh while the stream stays open. - The stream drops on network errors (
unexpected end of stream,Software caused connection abort) and keeps retrying for more than one hour. - The next attempt calls
getCredentials(), which returns the cached, expired token. - The service returns
401 PSYNC_S2103 JWT has expired. The SDK callsinvalidateCredentials(), 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
simolus3 commentedon Oct 24, 2026
Thanks for the report!
expiresAtis 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.