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: never trust a truncated lsof listing when retiring staged weights (#13124)

Adam Eivy8df7e16765fd
diff --git a/server/lib/openFileHolders.js b/server/lib/openFileHolders.js
index e5e1ae596b..726b279398 100644
--- a/server/lib/openFileHolders.js
+++ b/server/lib/openFileHolders.js
@@ -37,9 +37,11 @@ export async function findOpenFileHolders(files, { canary, platform = process.pl
const handle = await open(canary, 'r').catch(() => null);
if (!handle) return { known: false, reason: 'canary file unreadable' };
// lsof exits 1 when any named file is not open (the expected case for most
- // of them) while still printing what it found; judge the listing by content.
+ // of them) while still printing what it found. Any other failure — a
+ // timeout, a signal, a maxBuffer overflow — can leave a listing truncated
+ // after this PID but before a holder's, so it is never trusted.
const out = await run('lsof', ['-F', 'pn', '--', canary, ...files], { timeout: LSOF_TIMEOUT_MS, maxBuffer: LSOF_MAX_BUFFER })
- .then((r) => r.stdout, (err) => err?.stdout ?? null)
+ .then((r) => r.stdout, (err) => (err?.code === 1 && !err.killed && !err.signal ? err.stdout ?? null : null))
.finally(() => handle.close().catch(() => {}));
const pids = out == null ? [] : parseLsofPids(out);
if (!pids.includes(selfPid)) return { known: false, reason: 'lsof unavailable or listing incomplete' };
diff --git a/server/lib/openFileHolders.test.js b/server/lib/openFileHolders.test.js
index 643a75ed32..afa469b90a 100644
--- a/server/lib/openFileHolders.test.js
+++ b/server/lib/openFileHolders.test.js
@@ -20,7 +20,7 @@ describe('findOpenFileHolders', () => {
afterAll(() => rm(dir, { recursive: true, force: true }))
it('reports the other processes holding the files, never itself', async () => {
- const run = async () => { throw Object.assign(new Error('exit 1'), { stdout: `p100\nn${canary}\np200\nn${staged}\np200\nn${staged}\n` }) }
+ const run = async () => { throw Object.assign(new Error('exit 1'), { code: 1, stdout: `p100\nn${canary}\np200\nn${staged}\np200\nn${staged}\n` }) }
await expect(findOpenFileHolders([staged], { canary, platform: 'linux', selfPid: 100, run }))
.resolves.toEqual({ known: true, pids: [200] })
})
@@ -28,6 +28,8 @@ describe('findOpenFileHolders', () => {
it.each([
['the listing omits the canary this process holds', { platform: 'darwin', run: listing('p200\nn/staging/x\n') }],
['lsof is missing', { platform: 'linux', run: async () => { throw Object.assign(new Error('spawn lsof ENOENT'), { code: 'ENOENT' }) } }],
+ ['lsof timed out after printing only this process', { platform: 'linux', run: async () => { throw Object.assign(new Error('timed out'), { code: null, killed: true, signal: 'SIGTERM', stdout: `p100\nn${canary}\n` }) } }],
+ ['the listing overflowed its buffer', { platform: 'linux', run: async () => { throw Object.assign(new Error('maxBuffer'), { code: 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER', stdout: `p100\nn${canary}\n` }) } }],
['the platform has no lsof', { platform: 'win32', run: listing('p100\n') }]
])('fails closed when %s', async (_label, opts) => {
await expect(findOpenFileHolders([staged], { canary, selfPid: 100, ...opts })).resolves.toMatchObject({ known: false })
diff --git a/server/services/ollamaManager.test.js b/server/services/ollamaManager.test.js
index 0e40a31a40..390e143ada 100644
--- a/server/services/ollamaManager.test.js
+++ b/server/services/ollamaManager.test.js
@@ -867,7 +867,7 @@ describe('ollamaManager curated Hugging Face Safetensors import', () => {
if (!lsof.available) return cb(Object.assign(new Error('spawn lsof ENOENT'), { code: 'ENOENT' }))
const [canary, ...staged] = args.slice(args.indexOf('--') + 1)
const lines = [`p${process.pid}`, `n${canary}`, ...lsof.holders.flatMap((pid) => [`p${pid}`, `n${staged[0]}`])]
- return cb(Object.assign(new Error('lsof exit 1'), { stdout: `${lines.join('\n')}\n` }))
+ return cb(Object.assign(new Error('lsof exit 1'), { code: 1, stdout: `${lines.join('\n')}\n` }))
}
return cb(new Error(`unexpected exec: ${cmd} ${args.join(' ')}`))
}