Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: make an unreadable detached control file surface instead of reading as not-running (#3342)

[._.]/ Adam Eivyd11febee2e03
diff --git a/client/src/pages/DataManager.jsx b/client/src/pages/DataManager.jsx
index edabd495e9..517f4e96e0 100644
--- a/client/src/pages/DataManager.jsx
+++ b/client/src/pages/DataManager.jsx
@@ -376,32 +376,40 @@ export default function DataManager() {
useEffect(() => { fetchOverview(); }, [fetchOverview]);
- // Detail fetches are per-row and can land out of order — expanding A then B
- // could leave A's items (and, since #3342, A's busy state) rendered under B.
- // Every fetch takes a token and only the newest one is allowed to write.
+ // Detail fetches are per-row and can land out of order — expanding A then B,
+ // or an archive/purge on A finishing after the user moved to B, could leave
+ // A's items (and, since #3342, A's busy state) rendered under B. Two guards:
+ // a token so only the newest fetch writes, and a ref mirroring the open row so
+ // a handler holding a stale `expandedCat` closure can't fetch for a row that
+ // is no longer open.
const detailRequestRef = useRef(0);
+ const expandedCatRef = useRef(null);
+
+ const openCategory = (key) => {
+ expandedCatRef.current = key;
+ setExpandedCat(key);
+ setDetail(null);
+ };
const loadDetail = async (key) => {
const token = (detailRequestRef.current += 1);
const d = await api.getDataCategory(key).catch(() => null);
- if (detailRequestRef.current === token) setDetail(d);
+ if (detailRequestRef.current !== token || expandedCatRef.current !== key) return;
+ setDetail(d);
};
const handleExpand = async (key) => {
- if (expandedCat === key) {
- detailRequestRef.current += 1;
- setExpandedCat(null);
- setDetail(null);
+ if (expandedCatRef.current === key) {
+ openCategory(null);
return;
}
- setExpandedCat(key);
- setDetail(null);
+ openCategory(key);
await loadDetail(key);
};
const refreshAfterAction = async (key) => {
fetchOverview();
- if (expandedCat === key) await loadDetail(key);
+ if (expandedCatRef.current === key) await loadDetail(key);
};
const handleArchive = async (key) => {
diff --git a/client/src/pages/DataManager.test.jsx b/client/src/pages/DataManager.test.jsx
index c22475fd3f..162870e81c 100644
--- a/client/src/pages/DataManager.test.jsx
+++ b/client/src/pages/DataManager.test.jsx
@@ -217,6 +217,37 @@ describe('DataManager busy categories (#3342)', () => {
expect(screen.queryByText(BUSY_REASON)).not.toBeInTheDocument();
});
+ // A purge started on one row finishes after the user has moved to another. Its
+ // post-action refresh holds a stale `expandedCat` closure and must not repaint
+ // the row that is open now.
+ it('drops the post-action detail refresh for a row the user has since left', async () => {
+ getDataOverview.mockResolvedValue({
+ ...busyOverview,
+ categories: [{ ...busyOverview.categories[0], busy: false, busyReason: null }, busyOverview.categories[1]],
+ });
+ getDataCategory.mockImplementation((key) => Promise.resolve(key === 'training-runs'
+ ? { key, items: [], busy: true, busyReason: BUSY_REASON }
+ : { key, items: [], busy: false, busyReason: null }));
+ let resolvePurge;
+ purgeDataCategory.mockImplementation(() => new Promise((res) => { resolvePurge = res; }));
+
+ render(<DataManager />);
+ await waitFor(() => expect(screen.getByText('Messages')).toBeInTheDocument());
+
+ expandRow('Messages');
+ await waitFor(() => expect(screen.getByRole('button', { name: /Purge/ })).toBeInTheDocument());
+ fireEvent.click(screen.getByRole('button', { name: /Purge/ }));
+ fireEvent.click(screen.getByRole('button', { name: 'Purge' }));
+
+ expandRow('LoRA Training Runs');
+ await waitFor(() => expect(screen.getByText(BUSY_REASON)).toBeInTheDocument());
+
+ resolvePurge({ category: 'messages', subPath: null });
+ await waitFor(() => expect(getDataOverview).toHaveBeenCalledTimes(2));
+ expect(screen.getByText(BUSY_REASON)).toBeInTheDocument();
+ expect(screen.queryByRole('button', { name: /Purge/ })).not.toBeInTheDocument();
+ });
+
// Older servers omit `busy` entirely — treating anything but an explicit true
// as busy would strip the button from every category on an older peer.
it('treats a missing busy flag as idle', async () => {
diff --git a/server/lib/detachedSpawn.js b/server/lib/detachedSpawn.js
index 39421aecc1..1ea2619aaf 100644
--- a/server/lib/detachedSpawn.js
+++ b/server/lib/detachedSpawn.js
@@ -446,14 +446,25 @@ export async function reattachDetached(controlDir, { pollMs = DEFAULT_POLL_MS, c
* the NEW tailer's sentinel check and prematurely close the new handle with
* the old job's status (the stale-late-write race reapDetached guards against).
*
+ * Both callers read a `false` as permission to act — start a second update
+ * (updateExecutor), or purge the directory the job is writing into
+ * (dataManagerBusy, #3342). So an ABSENT control file reads as "never launched",
+ * but any other read failure is rethrown rather than silently synthesized into
+ * "not running": "we could not look" must not answer as "nothing is there".
+ *
* @param {string} controlDir - the job's spawnDetached control dir
* @returns {Promise<boolean>}
*/
+const readControlFile = (path) => readFile(path, 'utf8').catch((err) => {
+ if (err?.code === 'ENOENT') return '';
+ throw err;
+});
+
export async function isDetachedRunning(controlDir) {
- const pidRaw = await readFile(join(controlDir, 'pid'), 'utf8').catch(() => '');
+ const pidRaw = await readControlFile(join(controlDir, 'pid'));
const pid = Number.parseInt(pidRaw, 10);
if (!Number.isFinite(pid) || pid <= 0) return false;
- const exitWritten = (await readFile(join(controlDir, 'exit'), 'utf8').catch(() => '')).length > 0;
+ const exitWritten = (await readControlFile(join(controlDir, 'exit'))).length > 0;
return !exitWritten && isAlive(pid);
}
diff --git a/server/lib/detachedSpawn.test.js b/server/lib/detachedSpawn.test.js
index 6ce156fa6b..7cb8985c40 100644
--- a/server/lib/detachedSpawn.test.js
+++ b/server/lib/detachedSpawn.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect, afterEach } from 'vitest';
-import { mkdtemp, rm, stat, writeFile } from 'fs/promises';
+import { mkdir, mkdtemp, rm, stat, writeFile } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
import { execFile } from 'child_process';
@@ -294,6 +294,17 @@ describe('isDetachedRunning', () => {
await writeFile(join(controlDir, 'pid'), '2147483647');
expect(await isDetachedRunning(controlDir)).toBe(false);
});
+
+ // Callers treat `false` as permission to act (spawn a second update, purge the
+ // job's directory), so an unreadable control file must surface rather than
+ // masquerade as "never launched" (#3342).
+ it('rejects when a control file exists but cannot be read', async () => {
+ const controlDir = await tmpControlDir();
+ // A directory where the file belongs reads back EISDIR, not ENOENT — and
+ // unlike chmod it behaves the same when the suite runs as root.
+ await mkdir(join(controlDir, 'pid'), { recursive: true });
+ await expect(isDetachedRunning(controlDir)).rejects.toThrow();
+ });
});
describe('reattachDetached', () => {