Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: acquire sync lock during backup restore

thecodrr4b9453d91b89
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index 136986d71..32969a42a 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -52,6 +52,7 @@ export default class Sync {
this._tokenManager = new TokenManager(this._db.storage);
this._autoSyncTimeout = 0;
this._autoSyncInterval = 5000;
+ this._locked = false;
this.syncMutex = withTimeout(
new Mutex(),
@@ -71,7 +72,51 @@ export default class Sync {
.finally(() => this._afterSync());
}
+ async remoteSync() {
+ if (this.syncMutex.isLocked()) {
+ this.hasNewChanges = true;
+ return;
+ }
+ await this.syncMutex
+ .runExclusive(async () => {
+ this.stopAutoSync();
+ this.hasNewChanges = false;
+ if (await this._sync(true, false))
+ EV.publish(EVENTS.appRefreshRequested);
+ })
+ .finally(() => this._afterSync());
+ }
+
+ async startAutoSync() {
+ if (!(await checkIsUserPremium(CHECK_IDS.databaseSync))) return;
+ this.databaseUpdatedEvent = EV.subscribe(
+ EVENTS.databaseUpdated,
+ this._scheduleSync.bind(this)
+ );
+ }
+
+ stopAutoSync() {
+ clearTimeout(this._autoSyncTimeout);
+ if (this.databaseUpdatedEvent) this.databaseUpdatedEvent.unsubscribe();
+ }
+
+ acquireLock() {
+ this.stopAutoSync();
+ this._locked = true;
+ }
+
+ async releaseLock() {
+ this._locked = false;
+ await this.startAutoSync();
+ }
+
+ isLocked() {
+ return this._locked;
+ }
+
async _sync(full, force) {
+ if (this.isLocked()) return false;
+
let { lastSynced } = await this._performChecks();
if (force) lastSynced = 0;
@@ -117,34 +162,6 @@ export default class Sync {
return true;
}
- async remoteSync() {
- if (this.syncMutex.isLocked()) {
- this.hasNewChanges = true;
- return;
- }
- await this.syncMutex
- .runExclusive(async () => {
- this.stopAutoSync();
- this.hasNewChanges = false;
- await this._sync(true, false);
- EV.publish(EVENTS.appRefreshRequested);
- })
- .finally(() => this._afterSync());
- }
-
- async startAutoSync() {
- if (!(await checkIsUserPremium(CHECK_IDS.databaseSync))) return;
- this.databaseUpdatedEvent = EV.subscribe(
- EVENTS.databaseUpdated,
- this._scheduleSync.bind(this)
- );
- }
-
- stopAutoSync() {
- clearTimeout(this._autoSyncTimeout);
- if (this.databaseUpdatedEvent) this.databaseUpdatedEvent.unsubscribe();
- }
-
async _afterSync() {
if (!this.hasNewChanges) {
this.startAutoSync();
diff --git a/packages/core/database/backup.js b/packages/core/database/backup.js
index 54d67c9d2..797c363e8 100644
--- a/packages/core/database/backup.js
+++ b/packages/core/database/backup.js
@@ -140,9 +140,16 @@ export default class Backup {
},
];
- if (await this._migrator.migrate(collections, (id) => data[id], version)) {
- await this._db.notes.repairReferences();
- await this._db.notebooks.repairReferences();
+ try {
+ this._db.syncer.acquireLock();
+ if (
+ await this._migrator.migrate(collections, (id) => data[id], version)
+ ) {
+ await this._db.notes.repairReferences();
+ await this._db.notebooks.repairReferences();
+ }
+ } finally {
+ await this._db.syncer.releaseLock();
}
}