Refactor fetching credentials #220
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the
PowerSyncBackendConnector
implementation, errors thrown infetchCredentials()
are currently swallowed silently, because:join()
on aJob
instead of using aDeferrable
andawait()
.join()
just waits for the job to finish and, for supervisor scopes, doesn't rethrow exceptions.getCredentialsCached()
would silently returnnull
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 usingscope.launch
for the Rust client (the only place prefetching credentials) - so the internal async management can just be removed. This ensures errors infetchCredentials
bubble up all the way to thetry/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 theTestConnector
based on callbacks instead.Closes #219.