mirror of https://github.com/garrytan/gstack.git
fix: restore isProcessAlive boolean semantics, add safeUnlinkQuiet, remove unused json()
isProcessAlive now catches ALL errors and returns false (pure boolean probe). Callers use it in if/while conditions without try/catch, so throwing on EPERM was a behavior change that could crash the CLI. Windows path gets its safety catch restored. safeUnlinkQuiet added for best-effort cleanup paths where throwing on non-ENOENT errors (like EPERM during shutdown) would abort cleanup. json() removed — dead code, never imported anywhere. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
5f9246ac23
commit
6a857d41ba
|
|
@ -20,6 +20,11 @@ export function safeUnlink(filePath: string): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Remove a file, ignoring ALL errors. Use only in best-effort cleanup (shutdown, emergency). */
|
||||||
|
export function safeUnlinkQuiet(filePath: string): void {
|
||||||
|
try { fs.unlinkSync(filePath); } catch {}
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Process ───────────────────────────────────────────────────
|
// ─── Process ───────────────────────────────────────────────────
|
||||||
|
|
||||||
/** Send a signal to a process, ignoring ESRCH (already dead). Rethrows other errors. */
|
/** Send a signal to a process, ignoring ESRCH (already dead). Rethrows other errors. */
|
||||||
|
|
@ -31,33 +36,23 @@ export function safeKill(pid: number, signal: NodeJS.Signals | number): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check if a PID is alive. Returns false for ESRCH, rethrows EPERM and others. */
|
/** Check if a PID is alive. Pure boolean probe — returns false for ALL errors. */
|
||||||
export function isProcessAlive(pid: number): boolean {
|
export function isProcessAlive(pid: number): boolean {
|
||||||
if (IS_WINDOWS) {
|
if (IS_WINDOWS) {
|
||||||
// Bun's compiled binary can't signal Windows PIDs (always throws ESRCH).
|
try {
|
||||||
// Use tasklist as a fallback. Only for one-shot calls — too slow for polling loops.
|
const result = Bun.spawnSync(
|
||||||
// Bun.spawnSync may throw if tasklist binary is missing (ENOENT)
|
['tasklist', '/FI', `PID eq ${pid}`, '/NH', '/FO', 'CSV'],
|
||||||
const result = Bun.spawnSync(
|
{ stdout: 'pipe', stderr: 'pipe', timeout: 3000 }
|
||||||
['tasklist', '/FI', `PID eq ${pid}`, '/NH', '/FO', 'CSV'],
|
);
|
||||||
{ stdout: 'pipe', stderr: 'pipe', timeout: 3000 }
|
return result.stdout.toString().includes(`"${pid}"`);
|
||||||
);
|
} catch {
|
||||||
return result.stdout.toString().includes(`"${pid}"`);
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
process.kill(pid, 0);
|
process.kill(pid, 0);
|
||||||
return true;
|
return true;
|
||||||
} catch (err: any) {
|
} catch {
|
||||||
if (err?.code === 'ESRCH') return false;
|
return false;
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── HTTP ──────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
/** JSON Response constructor shorthand for Bun.serve routes. */
|
|
||||||
export function json(data: unknown, status = 200): Response {
|
|
||||||
return new Response(JSON.stringify(data), {
|
|
||||||
status,
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { describe, test, expect } from 'bun:test';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { safeUnlink, safeKill, isProcessAlive, json } from '../src/error-handling';
|
import { safeUnlink, safeKill, isProcessAlive } from '../src/error-handling';
|
||||||
|
|
||||||
describe('safeUnlink', () => {
|
describe('safeUnlink', () => {
|
||||||
test('removes an existing file', () => {
|
test('removes an existing file', () => {
|
||||||
|
|
@ -45,20 +45,3 @@ describe('isProcessAlive', () => {
|
||||||
expect(isProcessAlive(99999999)).toBe(false);
|
expect(isProcessAlive(99999999)).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('json', () => {
|
|
||||||
test('returns Response with JSON body and correct Content-Type', async () => {
|
|
||||||
const resp = json({ ok: true });
|
|
||||||
expect(resp.status).toBe(200);
|
|
||||||
expect(resp.headers.get('Content-Type')).toBe('application/json');
|
|
||||||
const body = await resp.json();
|
|
||||||
expect(body).toEqual({ ok: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
test('uses custom status code', async () => {
|
|
||||||
const resp = json({ error: 'not found' }, 404);
|
|
||||||
expect(resp.status).toBe(404);
|
|
||||||
const body = await resp.json();
|
|
||||||
expect(body).toEqual({ error: 'not found' });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue