fix: stop waiting on recreated state files

Wait for the requested daemon pid to exit instead of treating raw state-file existence as part of shutdown completion.
This commit is contained in:
morluto 2026-03-12 19:01:19 +08:00
parent 49df376a35
commit a4f6e3f67b
2 changed files with 34 additions and 3 deletions

View File

@ -226,9 +226,7 @@ async function ensureServer(): Promise<ServerState> {
async function waitForServerStop(pid: number, timeout = MAX_START_WAIT): Promise<void> {
const start = Date.now();
while (Date.now() - start < timeout) {
const alive = isProcessAlive(pid);
const stateExists = fs.existsSync(STATE_FILE);
if (!alive && !stateExists) {
if (!isProcessAlive(pid)) {
return;
}
await Bun.sleep(100);

View File

@ -767,6 +767,39 @@ describe('CLI lifecycle', () => {
await cleanupCliState(stateFile);
}
}, 25000);
test('stop ignores recreated state file once the target pid exits', async () => {
const stateFile = `/tmp/browse-stop-recreated-state-${Date.now()}.json`;
const port = reservePort();
const env = {
BROWSE_STATE_FILE: stateFile,
BROWSE_PORT: String(port),
};
try {
const started = await runCliCommand(['status'], env);
const startedState = readJson<{ pid: number; port: number; token: string; startedAt: string; serverPath: string }>(stateFile);
expect(started.code).toBe(0);
expect(startedState?.pid).toBeTruthy();
const stopPromise = runCliCommand(['stop'], env);
await waitFor(() => !fs.existsSync(stateFile) ? true : null, 5000);
fs.writeFileSync(stateFile, JSON.stringify({
pid: 999999,
port,
token: 'fake-token',
startedAt: new Date().toISOString(),
serverPath: '/tmp/fake-server.ts',
}));
const stop = await stopPromise;
expect(stop.code).toBe(0);
expect(stop.stdout).toContain('Server stopped');
} finally {
await cleanupCliState(stateFile);
}
}, 20000);
});
// ─── Buffer bounds ──────────────────────────────────────────────