Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(calendar): ignore sync completions from stopped runs (#2447)

Henry Su92297972c6e5
diff --git a/src/helpers/calendarSyncInterval.js b/src/helpers/calendarSyncInterval.js
index 4ac7f0eec..45e1ed488 100644
--- a/src/helpers/calendarSyncInterval.js
+++ b/src/helpers/calendarSyncInterval.js
@@ -14,14 +14,18 @@ class CalendarSyncInterval {
this.timer = null;
this._consecutiveFailures = 0;
this._lastFocusSync = 0;
+ // Pending syncs must not revive a stopped timer or alter a later run.
+ this._generation = 0;
}
start() {
+ this._generation++;
this._consecutiveFailures = 0;
this._schedule();
}
stop() {
+ this._generation++;
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
@@ -32,7 +36,7 @@ class CalendarSyncInterval {
notifySuccess() {
if (this._consecutiveFailures > 0) {
this._consecutiveFailures = 0;
- this._schedule();
+ if (this.timer) this._schedule();
}
}
@@ -41,8 +45,11 @@ class CalendarSyncInterval {
if (now - this._lastFocusSync < FOCUS_SYNC_THROTTLE_MS) return;
this._lastFocusSync = now;
+ const generation = this._generation;
this.syncFn()
- .then(() => this.notifySuccess())
+ .then(() => {
+ if (generation === this._generation) this.notifySuccess();
+ })
.catch((err) =>
debugLogger.error("Focus-triggered sync failed", { error: err.message }, this.logScope)
);
@@ -58,10 +65,14 @@ class CalendarSyncInterval {
this.logScope
);
+ const generation = this._generation;
this.timer = setInterval(() => {
this.syncFn()
- .then(() => this.notifySuccess())
+ .then(() => {
+ if (generation === this._generation) this.notifySuccess();
+ })
.catch((err) => {
+ if (generation !== this._generation) return;
this._consecutiveFailures++;
debugLogger.error(
"Calendar sync failed",
diff --git a/test/helpers/calendarSyncInterval.test.js b/test/helpers/calendarSyncInterval.test.js
index a8ba30f23..d20a14cce 100644
--- a/test/helpers/calendarSyncInterval.test.js
+++ b/test/helpers/calendarSyncInterval.test.js
@@ -79,3 +79,111 @@ test("focus syncs are throttled to one per window", async () => {
await flushPromises();
runner.stop();
});
+
+test("a pending failure cannot restart a stopped runner", async (t) => {
+ t.mock.timers.enable({ apis: ["setInterval"] });
+ const pending = Promise.withResolvers();
+ let calls = 0;
+ const runner = createRunner(() => {
+ calls += 1;
+ return pending.promise;
+ });
+ t.after(() => runner.stop());
+
+ runner.start();
+ t.mock.timers.tick(1000);
+ assert.equal(calls, 1);
+ runner.stop();
+ pending.reject(new Error("database connection is not open"));
+ await flushPromises();
+
+ t.mock.timers.tick(10000);
+ await flushPromises();
+ assert.equal(calls, 1, "the stopped runner must not sync again");
+});
+
+for (const [source, outcome, restartWithoutStop] of [
+ ["interval", "failure", false],
+ ["interval", "success", false],
+ ["focus", "success", false],
+ ["interval", "failure", true],
+]) {
+ const restart = restartWithoutStop ? "started again" : "restarted";
+ test(`an old ${source} ${outcome} cannot change backoff after the runner is ${restart}`, async (t) => {
+ t.mock.timers.enable({ apis: ["setInterval"] });
+ const pending = Promise.withResolvers();
+ let calls = 0;
+ const runner = createRunner(() => {
+ calls += 1;
+ if (calls === 1) return pending.promise;
+ if (calls === 2) return Promise.reject(new Error("current run failed"));
+ return Promise.resolve();
+ });
+ t.after(() => runner.stop());
+
+ runner.start();
+ if (source === "focus") runner.syncOnFocus();
+ else t.mock.timers.tick(1000);
+ assert.equal(calls, 1);
+ if (!restartWithoutStop) runner.stop();
+ runner.start();
+ t.mock.timers.tick(1000);
+ await flushPromises();
+ assert.equal(calls, 2);
+
+ if (outcome === "failure") pending.reject(new Error("old run failed"));
+ else pending.resolve();
+ await flushPromises();
+
+ t.mock.timers.tick(1000);
+ await flushPromises();
+ assert.equal(calls, 2, "the current run must retain its 2000ms backoff");
+ t.mock.timers.tick(1000);
+ await flushPromises();
+ assert.equal(calls, 3, "the old run must not extend the current backoff");
+ });
+}
+
+test("an out-of-band success does not start a stopped runner", async (t) => {
+ t.mock.timers.enable({ apis: ["setInterval"] });
+ let calls = 0;
+ const runner = createRunner(() => {
+ calls += 1;
+ return Promise.reject(new Error("boom"));
+ });
+ t.after(() => runner.stop());
+
+ runner.start();
+ t.mock.timers.tick(1000);
+ await flushPromises();
+ runner.stop();
+ runner.notifySuccess();
+
+ t.mock.timers.tick(10000);
+ await flushPromises();
+ assert.equal(calls, 1);
+});
+
+for (const source of ["interval", "focus"]) {
+ test(`a current ${source} success restores the base interval after a failure`, async (t) => {
+ t.mock.timers.enable({ apis: ["setInterval"] });
+ let calls = 0;
+ const runner = createRunner(() => {
+ calls += 1;
+ return calls === 1 ? Promise.reject(new Error("boom")) : Promise.resolve();
+ });
+ t.after(() => runner.stop());
+
+ runner.start();
+ t.mock.timers.tick(1000);
+ await flushPromises();
+ if (source === "focus") runner.syncOnFocus();
+ else t.mock.timers.tick(2000);
+ await flushPromises();
+ assert.equal(calls, 2);
+
+ t.mock.timers.tick(1000);
+ await flushPromises();
+ assert.equal(calls, 3, "a current success must restore the base 1000ms interval");
+ });
+}