mirror of https://github.com/garrytan/gstack.git
fix(browse): 'browse stop' no longer restarts the daemon it was asked to stop
The stop handler awaited shutdown() — which ends in process.exit — before returning, so the acknowledgement never egressed. The CLI's fetch reset, which its crash path reasonably interpreted as a dead daemon: it relaunched Chromium, re-sent stop, watched the daemon exit again, and errored 'Server crashed twice in a row'. Every stop cost a wasted Chromium launch and a nonzero exit. The ack now returns first; shutdown fires on a 25ms unref'd timer. Same fix for restart. Fork's test pins ack-before-teardown for both. Ported from time-attack/gstack (GStack 2). Co-authored-by: Sina Matian <sina@time-attack.dev> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
a30ca53c1a
commit
492fc5b9dd
|
|
@ -421,14 +421,17 @@ export async function handleMetaCommand(
|
|||
}
|
||||
|
||||
case 'stop': {
|
||||
await shutdown();
|
||||
// Return the acknowledgement before closing the listener. Shutting down
|
||||
// inline resets the CLI's fetch, which it reasonably interprets as a
|
||||
// crash and then restarts the daemon it was asked to stop.
|
||||
setTimeout(() => { void shutdown(); }, 25).unref?.();
|
||||
return 'Server stopped';
|
||||
}
|
||||
|
||||
case 'restart': {
|
||||
// Signal that we want a restart — the CLI will detect exit and restart
|
||||
console.log('[browse] Restart requested. Exiting for CLI to restart.');
|
||||
await shutdown();
|
||||
setTimeout(() => { void shutdown(); }, 25).unref?.();
|
||||
return 'Restarting...';
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
import { describe, expect, test } from 'bun:test';
|
||||
import { handleMetaCommand } from '../src/meta-commands';
|
||||
|
||||
describe('server control acknowledgement ordering', () => {
|
||||
for (const [command, acknowledgement] of [
|
||||
['stop', 'Server stopped'],
|
||||
['restart', 'Restarting...'],
|
||||
] as const) {
|
||||
test(`${command} acknowledges before closing the listener`, async () => {
|
||||
let shutdownCalls = 0;
|
||||
const manager = { getActiveSession: () => ({}) } as any;
|
||||
|
||||
const result = await handleMetaCommand(command, [], manager, async () => {
|
||||
shutdownCalls += 1;
|
||||
});
|
||||
|
||||
expect(result).toBe(acknowledgement);
|
||||
expect(shutdownCalls).toBe(0);
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
expect(shutdownCalls).toBe(1);
|
||||
});
|
||||
}
|
||||
});
|
||||
Loading…
Reference in New Issue