Skip to content

Build your personal knowledge base with Trilium Notes

client/db backup: fix an issue

Adorian Doran48b053967107
diff --git a/apps/client/src/services/options.spec.ts b/apps/client/src/services/options.spec.ts
index 7bd09cfa1f..e48e293682 100644
--- a/apps/client/src/services/options.spec.ts
+++ b/apps/client/src/services/options.spec.ts
@@ -104,6 +104,25 @@ describe("Options service", () => {
expect(options.get(k("str"))).toBe("before");
});
+ it("save() leaves a newer value alone when an older save fails after it", async () => {
+ options.load({ str: "before" });
+ let failTheSlowOne = (_: Error) => {};
+ server.put = vi.fn((_url: string, payload: unknown) => {
+ const value = (payload as Record<string, string>).str;
+ return value === "slow"
+ ? new Promise((_resolve, reject) => { failTheSlowOne = reject; })
+ : Promise.resolve({});
+ }) as unknown as typeof server.put;
+
+ const slow = options.save(k("str"), "slow");
+ await options.save(k("str"), "fast");
+ failTheSlowOne(new Error("HTTP 500"));
+
+ await expect(slow).rejects.toThrow("HTTP 500");
+ // The rollback belongs to the value that failed, not to whatever has since taken its place.
+ expect(options.get(k("str"))).toBe("fast");
+ });
+
it("saveMany() PUTs the whole record verbatim", async () => {
const put = (server.put = vi.fn(async () => ({})) as typeof server.put);
const payload = { a: "1", b: "2" } as unknown as Record<OptionNames, string>;
diff --git a/apps/client/src/services/options.ts b/apps/client/src/services/options.ts
index 8b752d2575..28ab8ecd49 100644
--- a/apps/client/src/services/options.ts
+++ b/apps/client/src/services/options.ts
@@ -84,7 +84,12 @@ class Options {
// Set ahead of the request so a read straight after the call sees the new value, but put
// back when the server refuses it: a cache that keeps reporting a value which was never
// stored makes the failure invisible to whoever asks afterwards.
- this.set(key, previous as OptionValue);
+ //
+ // Only while this call's own value is still the one held, so that a save which fails
+ // slowly cannot undo a later one that has already succeeded.
+ if (this.arr?.[key] === value) {
+ this.set(key, previous as OptionValue);
+ }
throw e;
}
}