Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

[Fix] Watched Queries Race Condition #23

Merged
merged 1 commit into from
Jan 16, 2024

Conversation

stevensJourney
Copy link
Collaborator

@stevensJourney stevensJourney commented Jan 16, 2024

This PR fixes a race condition which is possible when initialising multiple PowerSync instances simultaneously.

For example: Initialising PowerSync in a React.useState method will initialize two clients if React strict mode is enabled.

If the web environment is in a fresh state, having no previous DB initialized, the DB opening will take a few seconds. This will create multiple DB connections instead of the shared worker using a single cached connection. Table change updates are triggered per connection and will thus fail if an update is executed on a different connection.

This PR caches the promise of initialising the DB connection instead of the resolved connection. This should ensure that multiple open requests will still use the same DB connection.

This PR also reverts the listener change from #21 as this race condition was the root cause of the issue.

@stevensJourney stevensJourney marked this pull request as ready for review January 16, 2024 07:38
@stevensJourney stevensJourney merged commit 412937f into main Jan 16, 2024
2 checks passed
@stevensJourney stevensJourney deleted the fix/shared-db-open-race-condition branch January 16, 2024 07:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants