A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: fix infinite sync loop for items modified in the future (#10525)
Abdullah Atta6ffc90f45625
diff --git a/packages/core/src/api/sync/__tests__/collector.test.js b/packages/core/src/api/sync/__tests__/collector.test.jsindex 70c34dc81..99b86cc02 100644--- a/packages/core/src/api/sync/__tests__/collector.test.js+++ b/packages/core/src/api/sync/__tests__/collector.test.js@@ -59,6 +59,41 @@ test("synced property should be true after getting collected by the collector",expect(items[1].type).toBe("note");}));+test("note with a future dateModified should be synced after getting collected", () =>+ databaseTest().then(async (db) => {+ await loginFakeUser(db);+ const collector = new Collector(db);+ const noteId = await db.notes.add(TEST_NOTE);+ await db+ .sql()+ .updateTable("notes")+ .set({ dateModified: Date.now() + 1000 * 60 * 60 * 24 })+ .where("id", "=", noteId)+ .execute();++ await iteratorToArray(collector.collect(100, false));++ expect(await collector.hasUnsyncedChanges()).toBe(false);+ }));++test("note modified while being collected should stay unsynced", () =>+ databaseTest().then(async (db) => {+ await loginFakeUser(db);+ const collector = new Collector(db);+ const noteId = await db.notes.add(TEST_NOTE);++ for await (const _ of collector.collect(100, false)) {+ await db+ .sql()+ .updateTable("notes")+ .set({ title: "Edited", dateModified: Date.now() + 1, synced: false })+ .where("id", "=", noteId)+ .execute();+ }++ expect(await collector.hasUnsyncedChanges()).toBe(true);+ }));+test("edited note should get included in collector", () =>databaseTest().then(async (db) => {await loginFakeUser(db);diff --git a/packages/core/src/api/sync/collector.ts b/packages/core/src/api/sync/collector.tsindex 8cc2a7868..2a3a69404 100644--- a/packages/core/src/api/sync/collector.ts+++ b/packages/core/src/api/sync/collector.ts@@ -61,8 +61,8 @@ class Collector {for (const itemType of SYNC_ITEM_TYPES) {const collectionKey = SYNC_COLLECTIONS_MAP[itemType];const collection = this.db[collectionKey].collection;- let pushTimestamp = Date.now();for await (const chunk of collection.unsynced(chunkSize, isForceSync)) {+ const modified = chunk.map((i) => [i.id, i.dateModified] as const);const { ids, items: syncableItems } = filterSyncableItems(chunk);if (!ids.length) continue;const ciphers = await this.db@@ -78,19 +78,28 @@ class Collector {await this.db.sql().updateTable(collection.type)- .where("id", "in", ids)// EDGE CASE:// Sometimes an item can get updated while it's being pushed.// The result is that its `synced` property becomes true even// though it's modification wasn't yet synced.// In order to prevent that, we only set the `synced` property- // to true for items that haven't been modified since we last ran- // the push. Everything else will be collected again in the next- // push.- .where("dateModified", "<=", pushTimestamp)+ // to true for items whose `dateModified` is still the same as+ // the one we pushed. Everything else will be collected again in+ // the next push. Comparing against the pushed value (instead of+ // the current time) also handles items with a `dateModified` in+ // the future.+ .where((eb) =>+ eb.or(+ modified.map(([id, dateModified]) =>+ eb.and([+ eb("id", "=", id),+ eb("dateModified", "=", dateModified)+ ])+ )+ )+ ).set({ synced: true }).execute();- pushTimestamp = Date.now();}}}