Skip to content

Conversation

simolus3
Copy link
Contributor

@simolus3 simolus3 commented Jul 16, 2025

In the PowerSyncBackendConnector implementation, errors thrown in fetchCredentials() are currently swallowed silently, because:

  1. We use a supervisor scope to launch jobs fetching credentials.
  2. We then call join() on a Job instead of using a Deferrable and await(). join() just waits for the job to finish and, for supervisor scopes, doesn't rethrow exceptions.
  3. So, getCredentialsCached() would silently return null on errors, without this information being surfaced to the user.

On Android, it also looks like errors in root coroutines that aren't handled will unconditionally kill the app, regardless of the supervisor scope present (I can reproduce #219 by simply throwing in a connector's fetchCredentials on Android).

This PR refactors the implementation to avoid a global coroutine scope. This scope, with a Job to fetch credentials essentially had two purposes: First, to avoid fetching credentials concurrently (we want to re-use results instead), and second to implement prefetching credentials asynchronously.
For the first requirement, using a proper mutex is both easier and safer. For the second requirement, it's much better to let callers bring their own CoroutineScope that will properly forward errors. We are indeed already using scope.launch for the Rust client (the only place prefetching credentials) - so the internal async management can just be removed. This ensures errors in fetchCredentials bubble up all the way to the try/catch in the sync client which will retry after a delay.

Introducing a final method makes PowerSyncBackendConnector impossible to mock, so I've migrated tests to use the TestConnector based on callbacks instead.

Closes #219.

@simolus3 simolus3 requested a review from stevensJourney July 16, 2025 10:40
@simolus3 simolus3 merged commit 7f58185 into main Jul 16, 2025
8 of 15 checks passed
@simolus3 simolus3 deleted the refactor-fetching-tokens branch July 16, 2025 15:47
@Radiokot
Copy link
Contributor

Thank you!

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.

PowerSyncBackendConnector.fetchCredentials() error crashes an Android app

3 participants