commit e9cda413a4f7795e5f7b8fe3f7aad4fa193af537
parent c9f7e090b7f8b42e7266380638112e41a40662cf
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Mon, 9 Nov 2026 23:23:03 -0500
jobs: the media-stall hold heals itself, says it is still holding, and follows a moved channel
Review of lane drive-stall: a hold released only when the health record
cleared, so with no pass keeping a location's record a held batch idled for
ever. Now, after 5 min held (and at most once per 5 min), the hold asks the
root itself out of process (a child stat raced against the probe timeout,
probeLocationHealth). A root that answers clears a STALE record (no pass
refreshed it for three pass intervals) and the batch resumes; a record a pass
still keeps counts the answer once, like /storage's Refresh, and the pass
decides (a child stat can be answered from cache while the counters say
stalled). "Still holding: … has not answered after N min" every 10 min. The
channel's targets are re-read in the background (at most every 10 s), so a
channel moved mid-batch is held by the drive its media is on now.
pruneLocationHealth already drops a removed location's record; pinned by a
test that a hold on it releases.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
4 files changed, 263 insertions(+), 7 deletions(-)
diff --git a/common/controller/operationBatch.ts b/common/controller/operationBatch.ts
@@ -1607,7 +1607,12 @@ export async function runOperationBatch(
// is not answering (lib/mediaStallHold.ts); the digest lane reads text only.
const stallHold = run.digest
? null
- : mediaStallHold(await readChannelMediaConfig(opts.paths, opts.channelSlug), log, "backfill unit");
+ : mediaStallHold(
+ await readChannelMediaConfig(opts.paths, opts.channelSlug),
+ log,
+ "backfill unit",
+ { reloadConfig: () => readChannelMediaConfig(opts.paths, opts.channelSlug) },
+ );
const dataDir = path.join(opts.paths.channelsDir, opts.channelSlug, "data");
const allDirs = await readdir(dataDir).catch(() => [] as string[]);
const onDisk = new Set(allDirs);
diff --git a/common/controller/whisperBatch.ts b/common/controller/whisperBatch.ts
@@ -228,11 +228,14 @@ export async function runWhisperBatch({
let idx = 0;
// HELD WHILE THE CHANNEL'S MEDIA DRIVE IS NOT ANSWERING (lib/mediaStallHold.ts):
// the limit is 0, the pool pauses, and it resumes when the drive answers.
- // The config is read once; the check is memory.
+ // The check is memory; the config is re-read in the background.
const stallHold = mediaStallHold(
await readChannelMediaConfig(paths, channelSlug),
log,
"transcription",
+ // Re-read in the background (at most every 10 s): a move mid-batch is
+ // held by the drive the media is on now.
+ { reloadConfig: () => readChannelMediaConfig(paths, channelSlug) },
);
const runOne = async (videoDir: string, runSignal: AbortSignal): Promise<void> => {
const res = await transcribeOneFromQueue({
diff --git a/common/lib/mediaStallHold.test.ts b/common/lib/mediaStallHold.test.ts
@@ -48,3 +48,125 @@ test("a channel in place, or on another location, is never held", () => {
test("the refusal sentence names the drive and since when", () => {
assert.match(mediaDriveHeldText({ label: "USB drive", since: Date.now() }), /^the media drive "USB drive" is not answering \(since \d\d:\d\d\)$/);
});
+
+// ── the hold heals itself (review of lane drive-stall) ──────────────────────
+
+const tick = () => new Promise((r) => setImmediate(r));
+
+test("held past reprobeAfterMs with no pass keeping the record: one out-of-process probe answers and clears it", async () => {
+ const { locationHealth } = await import("./storageHealth");
+ let t = 1_000_000;
+ registerLocationHealth([USB], t);
+ // Recorded once, long ago — no pass refreshes it (a CLI, an idle record).
+ recordLocationHealth(USB, "stalled", { now: t });
+ const probed: string[] = [];
+ const lines: string[] = [];
+ const hold = mediaStallHold({ mediaDir: "/mnt/usb/chan/media" }, (l) => lines.push(l), "transcription", {
+ now: () => t,
+ reprobeAfterMs: 60_000,
+ stillHoldingEveryMs: 600_000,
+ reprobe: async (root) => {
+ probed.push(root);
+ return "ok";
+ },
+ });
+ assert.ok(hold.check());
+ t += 30_000;
+ assert.ok(hold.check());
+ await tick();
+ assert.deepEqual(probed, [], "not before reprobeAfterMs");
+ t += 31_000;
+ assert.ok(hold.check(), "the probe is asynchronous: this pull is still held");
+ await tick();
+ await tick();
+ assert.deepEqual(probed, ["/mnt/usb"]);
+ assert.notEqual(locationHealth("usb")?.state, "stalled", "a stale record is cleared by the answer");
+ assert.equal(hold.check(), null, "and the next pull resumes");
+ assert.match(lines.join("\n"), /answered a stat of its root after 1 min held — no health pass was keeping its stall record/);
+ assert.equal(lines.at(-1), `The media drive "USB drive" answers again — resuming.`);
+});
+
+test("a record a pass still keeps: the probe's answer counts once, and the pass decides", async () => {
+ const { locationHealth } = await import("./storageHealth");
+ let t = Date.now();
+ registerLocationHealth([USB], t);
+ recordLocationHealth(USB, "stalled", { now: t });
+ const hold = mediaStallHold({ mediaDir: "/mnt/usb/c/media" }, () => {}, "x", {
+ now: () => t,
+ reprobeAfterMs: 1_000,
+ reprobe: async () => "ok",
+ });
+ hold.check();
+ t += 1_500;
+ // A pass answered moments ago (the counters still say stalled).
+ recordLocationHealth(USB, "stalled", { now: t });
+ hold.check();
+ await tick();
+ await tick();
+ const h = locationHealth("usb");
+ assert.equal(h?.state, "stalled", "one clean answer does not clear (two are needed)");
+ assert.equal(h?.cleanStreak, 1);
+});
+
+test("a drive that does not answer the probe stays held, and 'still holding' is logged every interval", async () => {
+ let t = 5_000_000;
+ registerLocationHealth([USB], t);
+ recordLocationHealth(USB, "stalled", { now: t });
+ const lines: string[] = [];
+ let probes = 0;
+ const hold = mediaStallHold({ mediaDir: "/mnt/usb/c/media" }, (l) => lines.push(l), "x", {
+ now: () => t,
+ reprobeAfterMs: 300_000,
+ stillHoldingEveryMs: 600_000,
+ reprobe: async () => {
+ probes++;
+ return "stalled";
+ },
+ });
+ for (let i = 0; i < 25; i++) {
+ hold.check();
+ await tick();
+ t += 60_000;
+ }
+ assert.ok(hold.check());
+ const still = lines.filter((l) => l.startsWith("Still holding"));
+ assert.deepEqual(still, [
+ `Still holding: the media drive "USB drive" has not answered after 10 min.`,
+ `Still holding: the media drive "USB drive" has not answered after 20 min.`,
+ ]);
+ // Once per reprobeAfterMs (5 min) after the first 5 min: at 5, 10, 15, 20, 25.
+ assert.equal(probes, 5);
+});
+
+test("the config is re-read: a channel moved off the stalled drive mid-batch resumes", async () => {
+ let t = 0;
+ registerLocationHealth([USB], t);
+ recordLocationHealth(USB, "stalled", { now: t });
+ let config = { mediaDir: "/mnt/usb/c/media" };
+ const hold = mediaStallHold(config, () => {}, "x", {
+ now: () => t,
+ reloadEveryMs: 10_000,
+ reloadConfig: async () => config,
+ });
+ assert.ok(hold.check());
+ config = { mediaDir: "/mnt/other/c/media" };
+ t += 5_000;
+ hold.check();
+ await tick();
+ assert.ok(hold.check(), "not re-read before reloadEveryMs");
+ t += 6_000;
+ hold.check();
+ await tick();
+ assert.equal(hold.check(), null);
+});
+
+test("pruning a location no longer configured clears its record, and a hold on it releases", async () => {
+ const { pruneLocationHealth, locationHealth } = await import("./storageHealth");
+ registerLocationHealth([USB]);
+ recordLocationHealth(USB, "stalled");
+ const hold = mediaStallHold({ mediaDir: "/mnt/usb/c/media" }, () => {}, "x");
+ assert.ok(hold.check());
+ pruneLocationHealth(["some-other-location"]);
+ assert.equal(locationHealth("usb"), undefined);
+ assert.equal(hold.check(), null);
+});
diff --git a/common/lib/mediaStallHold.ts b/common/lib/mediaStallHold.ts
@@ -1,5 +1,13 @@
import { channelMediaStall, type ChannelMediaConfig } from "./channelMedia";
-import { sinceText, type LocationHealth } from "./storageHealth";
+import {
+ healthTimings,
+ locationHealth,
+ recordLocationHealth,
+ sinceText,
+ type LocationHealth,
+ type LocationHealthState,
+} from "./storageHealth";
+import { probeLocationHealth } from "./storageVolumes";
// A BATCH THAT OPENS MEDIA HOLDS WHILE ITS CHANNEL'S MEDIA DRIVE IS NOT
// ANSWERING (lane drive-stall).
@@ -15,33 +23,151 @@ import { sinceText, type LocationHealth } from "./storageHealth";
// The units already in flight finish or fail on their own; nothing cancels a
// read the kernel holds.
//
-// NO I/O: `channelMediaStall` reads the process's health record (the health
-// pass and the watchdog write it), never the drive. Edge-logged, once when
-// the hold begins and once when it ends.
+// THE CHECK IS MEMORY: `channelMediaStall` reads the process's health record
+// (the health pass and the watchdog write it), never the drive. Edge-logged,
+// once when the hold begins and once when it ends, and "still holding" every
+// `stillHoldingEveryMs` (10 min) between.
+//
+// IT HEALS ITSELF (review of lane drive-stall). The record is cleared by the
+// health pass; a process with no pass covering the location (a CLI, a stall
+// recorded for a location the pass no longer covers) would otherwise hold for
+// ever. So once the hold has lasted `reprobeAfterMs` (5 min), and at most once
+// per that interval, it asks the drive itself — OUT OF PROCESS, a child `stat`
+// of the root raced against the probe timeout, which holds no thread of ours.
+// When the root answers:
+// - a record no pass has refreshed for three pass intervals is stale: the
+// answer clears it (as many clean answers as the clear rule needs), and
+// the batch resumes;
+// - a record a pass still keeps counts the answer once, as /storage's
+// Refresh does, and the pass decides: a child `stat` can be answered from
+// the kernel's cache while the device's own counters say stalled.
+//
+// THE CONFIG IS RE-READ (`reloadConfig`, at most every `reloadEveryMs`, 10 s,
+// in the background): a channel moved to another location mid-batch is held
+// by the drive its media is on now.
export type MediaStallHold = {
// The stalled location now, or null. Logs on each edge.
check: () => LocationHealth | null;
};
+export type MediaStallHoldOptions = {
+ // Re-read the channel's targets (default: never). The check uses the last
+ // answer; a read that fails keeps the one before.
+ reloadConfig?: () => Promise<ChannelMediaConfig | null | undefined>;
+ reloadEveryMs?: number;
+ // The out-of-process probe of a root (default: a child `stat`,
+ // `probeLocationHealth`). Test seam.
+ reprobe?: (root: string) => Promise<LocationHealthState>;
+ reprobeAfterMs?: number;
+ stillHoldingEveryMs?: number;
+ now?: () => number;
+};
+
+export const MEDIA_HOLD_REPROBE_AFTER_MS = 5 * 60_000;
+export const MEDIA_HOLD_STILL_EVERY_MS = 10 * 60_000;
+const RELOAD_EVERY_MS = 10_000;
+
+function minutesText(ms: number): string {
+ const m = Math.max(0, Math.round(ms / 60_000));
+ return `${m} min`;
+}
+
export function mediaStallHold(
- config: ChannelMediaConfig | null | undefined,
+ initialConfig: ChannelMediaConfig | null | undefined,
log: (line: string) => void,
what: string,
+ opts: MediaStallHoldOptions = {},
): MediaStallHold {
+ const now = opts.now ?? Date.now;
+ const reprobeAfter = opts.reprobeAfterMs ?? MEDIA_HOLD_REPROBE_AFTER_MS;
+ const stillEvery = opts.stillHoldingEveryMs ?? MEDIA_HOLD_STILL_EVERY_MS;
+ const reloadEvery = opts.reloadEveryMs ?? RELOAD_EVERY_MS;
+ const reprobe = opts.reprobe ?? ((root: string) => probeLocationHealth({ root }));
+ let config = initialConfig;
+ let lastReload = now();
+ let reloading = false;
let holding: LocationHealth | null = null;
+ let heldAt = 0;
+ let lastStill = 0;
+ let lastReprobe = 0;
+ let reprobing = false;
+
+ const maybeReload = () => {
+ if (!opts.reloadConfig || reloading || now() - lastReload < reloadEvery) return;
+ reloading = true;
+ lastReload = now();
+ void opts
+ .reloadConfig()
+ .then(
+ (next) => {
+ if (next !== undefined) config = next;
+ },
+ () => {},
+ )
+ .finally(() => {
+ reloading = false;
+ });
+ };
+
+ const maybeReprobe = (stall: LocationHealth) => {
+ const t = now();
+ if (reprobing || t - heldAt < reprobeAfter || t - lastReprobe < reprobeAfter) return;
+ reprobing = true;
+ lastReprobe = t;
+ void reprobe(stall.root)
+ .then(
+ (answer) => {
+ if (answer === "stalled") return;
+ const current = locationHealth(stall.id);
+ if (!current || current.root !== stall.root || current.state !== "stalled") return;
+ const loc = { id: stall.id, label: stall.label, root: stall.root };
+ const stale = now() - current.checkedAt >= healthTimings().passIntervalMs * 3;
+ // Stale: no pass is keeping it, and the drive answered — enough
+ // clean answers to clear it. Kept by a pass: one answer.
+ const answers = stale ? healthTimings().clearAfterCleanPasses : 1;
+ for (let i = 0; i < answers; i++) {
+ recordLocationHealth(loc, answer, { detector: "stat", device: null });
+ }
+ log(
+ `The media drive "${stall.label}" answered a stat of its root after ` +
+ `${minutesText(t - heldAt)} held` +
+ (stale
+ ? " — no health pass was keeping its stall record, so the stall is cleared."
+ : "; its health pass decides when the stall ends."),
+ );
+ },
+ () => {},
+ )
+ .finally(() => {
+ reprobing = false;
+ });
+ };
+
return {
check: () => {
+ maybeReload();
const stall = channelMediaStall(config);
+ const t = now();
if (stall && !holding) {
+ heldAt = t;
+ lastStill = t;
log(
`Holding: the media drive "${stall.label}" is not answering (${sinceText(stall.since)}) — ` +
`no new ${what} starts until it answers again.`,
);
+ } else if (stall && holding) {
+ if (t - lastStill >= stillEvery) {
+ lastStill = t;
+ log(
+ `Still holding: the media drive "${stall.label}" has not answered after ${minutesText(t - heldAt)}.`,
+ );
+ }
} else if (!stall && holding) {
log(`The media drive "${holding.label}" answers again — resuming.`);
}
holding = stall;
+ if (stall) maybeReprobe(stall);
return stall;
},
};