Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(view): a busy port names only the process lsof sees listening [N26]

Corey Olsonb4dd004bb94b
diff --git a/src/view-session.js b/src/view-session.js
index 99010acb..409c47fa 100644
--- a/src/view-session.js
+++ b/src/view-session.js
@@ -81,25 +81,23 @@ export async function runningView(port = 0, expectedScopeCommonDir = null) {
}
/**
- * Name the process listening on a loopback port, when this machine can tell: first a live view
- * recorded for that port, then lsof where the system has it. Returns null when neither knows.
+ * Name the process listening on a loopback port, when this machine can tell. Only the system's
+ * listener table (lsof) can say who owns a port: a recorded view pid that is still alive may be a
+ * reused pid, so the record only labels a listener lsof already confirmed. Without lsof, or when
+ * it names no listener, this returns null and the caller gives the fix without a pid.
* Only the pid and the executable's own name are kept, since arguments can carry secrets.
*
* @param {number} port
* @returns {{ pid: number, name: string } | null}
*/
export function portHolder(port) {
- const recorded = readSession(port);
- if (recorded) {
- try {
- process.kill(recorded.pid, 0);
- return { pid: recorded.pid, name: 'pullboard view' };
- } catch { /* The recorded view has exited; ask the system who holds the port instead. */ }
- }
const lsof = ['/usr/sbin/lsof', '/usr/bin/lsof'].find((file) => existsSync(file));
if (!lsof) return null;
const listed = spawnSync(lsof, ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN', '-Fpc'], { encoding: 'utf8', timeout: 2_000 });
- const pid = Number(/^p(\d+)$/mu.exec(listed.stdout ?? '')?.[1]);
- if (!Number.isInteger(pid) || pid < 1) return null;
+ const pids = [...String(listed.stdout ?? '').matchAll(/^p(\d+)$/gmu)].map((match) => Number(match[1]));
+ // One listener is an answer; none or several (an unusual shared socket) names no single process.
+ if (listed.status !== 0 || pids.length !== 1 || !Number.isInteger(pids[0]) || pids[0] < 1) return null;
+ const pid = pids[0];
+ if (readSession(port)?.pid === pid) return { pid, name: 'pullboard view' };
return { pid, name: /^c(.+)$/mu.exec(listed.stdout)?.[1]?.trim() || 'a program' };
}
diff --git a/test/view-port.test.js b/test/view-port.test.js
index 23a8b75f..a663571b 100644
--- a/test/view-port.test.js
+++ b/test/view-port.test.js
@@ -1,9 +1,10 @@
/** Keep the view's exact named-port refusal covered in a core-owned file [N26]. */
import assert from 'node:assert/strict';
+import { randomUUID } from 'node:crypto';
import { runFixtureChild, startFixtureChild } from './fixture-child.js';
import { createServer } from 'node:net';
import { createServer as createHttpServer } from 'node:http';
-import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync } from 'node:fs';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
@@ -128,6 +129,12 @@ test('a busy view port names a fix the hosted page can still find [N26,I1]', asy
};
delete env.AI_AGENT;
delete env.PULLBOARD_VIEW_FOREGROUND;
+ // A stale view record whose pid is alive in an unrelated process must never be named as the holder.
+ const unrelated = startFixtureChild(process.execPath, ['-e', 'setTimeout(() => {}, 60_000)'], { stdio: 'ignore' }); // budget: idle lifetime for the unrelated pid; killed when the test ends.
+ t.after(() => { unrelated.kill('SIGKILL'); });
+ mkdirSync(env.PULLBOARD_HOME, { recursive: true });
+ writeFileSync(join(env.PULLBOARD_HOME, `view-${port}.json`),
+ `${JSON.stringify({ port, secret: randomUUID(), pid: unrelated.pid, boundAt: new Date().toISOString() })}\n`, { mode: 0o600 });
const plain = runFixtureChild(process.execPath, [BIN, 'view', '--no-open'], { cwd: dir, env, encoding: 'utf8', timeout: 40_000 });
assert.equal(plain.status, 1, plain.failure ?? plain.stderr);
const json = runFixtureChild(process.execPath, [BIN, 'view', '--no-open', '--json'], { cwd: dir, env, encoding: 'utf8', timeout: 40_000 });
@@ -144,8 +151,10 @@ test('a busy view port names a fix the hosted page can still find [N26,I1]', asy
assert.ok(refusal.message.includes(`in use by node (pid ${process.pid}),`) && refusal.message.includes(`stop it with kill ${process.pid},`),
`the refusal names the process holding the port: ${refusal.message}`);
}
+ assert.ok(!refusal.message.includes(String(unrelated.pid)), `a live pid in a stale view record is not the holder: ${refusal.message}`);
assert.equal(refusal.next, 'stop the other view on that port, then run pullboard view again');
assert.equal(plain.stderr.trim(), `pullboard: [PORT_BUSY] ${refusal.message}\nnext: ${refusal.next}`);
+ rmSync(join(env.PULLBOARD_HOME, `view-${port}.json`), { force: true });
const free = runFixtureChild(process.execPath, [BIN, 'view', '--no-open', '--port', '0', '--json'], { cwd: dir, env, encoding: 'utf8', timeout: 40_000 });
assert.equal(free.status, 0, free.failure ?? free.stderr);
t.after(() => cleanupFixtureViews(env.PULLBOARD_HOME));