fix(review): address latest CodeRabbit comments

This commit is contained in:
2026-03-19 23:49:55 -07:00
parent 42028d0a4d
commit 24667ad6c9
4 changed files with 92 additions and 3 deletions

View File

@@ -438,6 +438,40 @@ test('stats command aborts pending response wait when app exits before startup r
assert.equal(aborted, true);
});
test('stats command aborts pending response wait when attached app fails to spawn', async () => {
const context = createContext();
context.args.stats = true;
const spawnError = new Error('spawn failed');
let aborted = false;
await assert.rejects(
async () => {
await runStatsCommand(context, {
createTempDir: () => '/tmp/subminer-stats-test',
joinPath: (...parts) => parts.join('/'),
runAppCommandAttached: async () => {
throw spawnError;
},
waitForStatsResponse: async (_responsePath, signal) =>
await new Promise((resolve) => {
signal?.addEventListener(
'abort',
() => {
aborted = true;
resolve({ ok: false, error: 'aborted' });
},
{ once: true },
);
}),
removeDir: () => {},
});
},
(error: unknown) => error === spawnError,
);
assert.equal(aborted, true);
});
test('stats cleanup command aborts pending response wait when app exits before startup response', async () => {
const context = createContext();
context.args.stats = true;

View File

@@ -34,6 +34,11 @@ type StatsResponseWait = {
promise: Promise<{ kind: 'response'; response: StatsCommandResponse }>;
};
type StatsStartupResult =
| { kind: 'response'; response: StatsCommandResponse }
| { kind: 'exit'; status: number }
| { kind: 'spawn-error'; error: unknown };
const defaultDeps: StatsCommandDeps = {
createTempDir: (prefix) => fs.mkdtempSync(path.join(os.tmpdir(), prefix)),
joinPath: (...parts) => path.join(...parts),
@@ -72,11 +77,19 @@ async function performStartupHandshake(
attachedExitPromise: Promise<number>,
): Promise<boolean> {
const responseWait = createResponseWait();
const startupResult = await Promise.race([
const startupResult = await Promise.race<StatsStartupResult>([
responseWait.promise,
attachedExitPromise.then((status) => ({ kind: 'exit' as const, status })),
attachedExitPromise.then(
(status) => ({ kind: 'exit' as const, status }),
(error) => ({ kind: 'spawn-error' as const, error }),
),
]);
if (startupResult.kind === 'spawn-error') {
responseWait.controller.abort();
throw startupResult.error;
}
if (startupResult.kind === 'exit') {
if (startupResult.status !== 0) {
responseWait.controller.abort();