Skip to content

[Fix] CRUD Upload on Reconnect #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 31, 2024
Merged

[Fix] CRUD Upload on Reconnect #203

merged 16 commits into from
Oct 31, 2024

Conversation

stevensJourney
Copy link
Contributor

Overview

This fixes an issue where CRUD uploads are not triggered when the SDK reconnects to the PowerSync service after being offline.

Previously uploads were only triggered when an initial call to .connect has been made or when changes to the ps_crud table have occurred. If an application called .connect successfully, then went offline and made CRUD operations - the SDK would not trigger a CRUD upload once it had re-established a connection.

This now triggers a CRUD upload whenever the SDK establishes a connection or a change has been made to ps_crud.

Testing

This was tested locally and unit tests have been added using a mock PowerSync service.

@stevensJourney stevensJourney marked this pull request as ready for review October 30, 2024 08:28
Co-authored-by: Mughees Khan <mugikhan@gmail.com>
mugikhan
mugikhan previously approved these changes Oct 30, 2024
Copy link
Contributor

@mugikhan mugikhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@stevensJourney
Copy link
Contributor Author

Some unit tests are broken. Will investigate.

@rkistner
Copy link
Contributor

For my own reference, since I was confused with how this could happen:

  1. If a crud uploads fails with just an upload error, it is automatically retried indefinitely.
  2. If the user explicitly disconnects and reconnects, the crud loop is restarted, and should try again.
  3. If however a crud upload fails and isConnected == false, then the current crud iteration stops, and waits for more changes. This prevents continuous retrying of uploads while the user is offline.

The fix here targets (3), by basically triggering the "there are more changes" stream when the sync connection is established again.

@lucastex
Copy link

@stevensJourney thanks for being working on this. We're following the PR because it got a huge impact in our last update here. Thanks.

@stevensJourney
Copy link
Contributor Author

Some unit tests are broken. Will investigate.

Tests pass more consistently now. The main issues were from the full powersync reconnect test timing out: this test calls the SDK close method which performs a disconnect and closes the SQLite DB. A race condition caused the SQLite DB to close before the CRUD loop exited. This caused an exception to be thrown when the CRUD loop checked for CRUD operations - the caught exception added a retry delay which kept the sync isolate alive for an additional 5 seconds.

I've shuffled around the CRUD loop exit checks in order to exit the loop faster when disconnecting or closing.

@stevensJourney stevensJourney marked this pull request as ready for review October 31, 2024 08:02
rkistner
rkistner previously approved these changes Oct 31, 2024
mugikhan
mugikhan previously approved these changes Oct 31, 2024
 - powersync@1.8.9
 - powersync_attachments_helper@0.6.13
 - powersync_flutter_libs@0.4.2
@stevensJourney stevensJourney dismissed stale reviews from mugikhan and rkistner via c92e7f5 October 31, 2024 08:50
@stevensJourney stevensJourney merged commit 4230188 into main Oct 31, 2024
8 checks passed
@stevensJourney stevensJourney deleted the fix/crud-upload branch October 31, 2024 09:02
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.

4 participants