commit 9a2dab368683f21330cb0a911196b922ff63cafd
parent e9877855e779c7bce967ee8da72c838a87a5b96c
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Mon, 9 Nov 2026 21:29:17 -0500
storage: the watch's auto-pause write takes the settings lock
The five-minute storage watch read settings and wrote channelPriority with
no lock, while the priority writer (which also clears autoPaused on a manual
tier change) now holds the per-process settings.json lock for its whole
read-modify-write (lane editor-gaps). A pass writing inside that window was
written over, or wrote over the operator's change. The pass's one write now
runs under the same lock (withJsonFileLock on settings.json; not re-entrant,
and nothing inside it takes it). Test: a writer holding the lock is not
interleaved, and both its tier and the pass's pause survive.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 71 insertions(+), 2 deletions(-)
diff --git a/common/controller/storageWatch.test.ts b/common/controller/storageWatch.test.ts
@@ -661,3 +661,50 @@ test("DT: a changed pass interval re-arms the armed pass; an explicit interval f
}
});
});
+
+// THE PASS'S WRITE WAITS FOR THE SETTINGS LOCK (lane drive-stall). The
+// editor's priority writer holds the per-process lock on settings.json for its
+// whole read-modify-write (editor/app/settings/saveSettings.ts,
+// `withSettingsLock`); a pass that wrote in the middle of it would be written
+// over by the writer's stale document, and the pause lost.
+test("the pass writes under the settings lock: a writer holding it is not interleaved", async () => {
+ const { withJsonFileLock } = await import("../lib/jsonFile-server");
+ const { getPaths } = await import("../lib/paths");
+ await withTmp(async (h) => {
+ await seedRelocated(h, "gone", { targetExists: false });
+ await seedRelocated(h, "other", { targetExists: true });
+ const pass = () => runStorageWatchPass({ paths: h.paths, io: h.io, bins: h.paths });
+ await pass(); // the suspicion
+ let release!: () => void;
+ const gate = new Promise<void>((r) => (release = r));
+ const order: string[] = [];
+ // The "priority writer": reads, waits while holding the lock, writes.
+ const writer = withJsonFileLock(getPaths().settingsFile, async () => {
+ const before = h.io.read();
+ await gate;
+ order.push("writer");
+ await h.io.write({
+ ...before,
+ channelPriority: sanitizeChannelPriority({
+ ...before.channelPriority,
+ channels: { ...before.channelPriority.channels, other: { tier: "low" } },
+ }),
+ });
+ });
+ const confirming = pass().then((r) => {
+ order.push("pass");
+ return r;
+ });
+ // Give the pass every chance to write while the lock is held.
+ await new Promise((r) => setTimeout(r, 50));
+ assert.equal(h.writes, 0, "the pass did not write while the lock was held");
+ release();
+ await writer;
+ const second = await confirming;
+ assert.deepEqual(second.paused, ["gone"]);
+ assert.deepEqual(order, ["writer", "pass"]);
+ // Both survive: the writer's tier and the pass's pause.
+ assert.equal(tierOf(h, "other"), "low");
+ assert.equal(tierOf(h, "gone"), "paused");
+ });
+});
diff --git a/common/controller/storageWatch.ts b/common/controller/storageWatch.ts
@@ -1,4 +1,5 @@
import { getPaths, type Paths } from "../lib/paths";
+import { withJsonFileLock } from "../lib/jsonFile-server";
import {
getSettings,
writeSettings,
@@ -297,6 +298,29 @@ export async function runStorageWatchPass(
if (out.paused.length === 0 && out.restored.length === 0) return out;
if (opts.write === false) return out;
+ // UNDER THE SETTINGS LOCK (lane drive-stall, after lane editor-gaps). The
+ // editor's other read-modify-write of `channelPriority` — the priority
+ // writer, which also clears `autoPaused` on a manual tier change — runs inside
+ // `withSettingsLock` (editor/app/settings/saveSettings.ts), which is this same
+ // per-process lock on settings.json. Without it the two interleave: the
+ // operator's tier change lands between this pass's read and its write and is
+ // written over, or the pass's pause is. The lock is not re-entrant, and
+ // nothing called inside it takes it.
+ await withJsonFileLock(getPaths().settingsFile, () =>
+ writePassResult(io, paths, configs, out, pauseCauses),
+ );
+ out.wrote = true;
+ return out;
+}
+
+// THE PASS'S ONE WRITE, with the settings lock held by the caller.
+async function writePassResult(
+ io: NonNullable<StorageWatchOpts["io"]>,
+ paths: Paths,
+ configs: ReadonlyArray<{ slug: string }>,
+ out: StorageWatchResult,
+ pauseCauses: ReadonlyMap<string, AutoPauseCause>,
+): Promise<void> {
// ONE WRITE PER PASS, whatever the pass found. Ten channels on one drive that
// vanished is one settings write and one pulse bump, not ten.
//
@@ -349,8 +373,6 @@ export async function runStorageWatchPass(
}
}
await io.write({ ...latest, channelPriority: merged, autoQueue });
- out.wrote = true;
- return out;
}
// Five minutes. A drive does not come and go on a timescale a person would