Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(git): a hardened Git stream whose child exited but whose stdout stopped delivering fails stalled instead of hanging the pass (job-abort at pass-classify hang)

garrytana8f4663c5629
diff --git a/src/core/hardened-git.ts b/src/core/hardened-git.ts
index 72646748a3..9f6519ae87 100644
--- a/src/core/hardened-git.ts
+++ b/src/core/hardened-git.ts
@@ -87,7 +87,14 @@ export function hardenedGitSync(root: string, args: string[], limits: { timeoutM
}
export type HardenedGitStreamResult = { ok: true; exitCode: 0; signal: null; bytes: number; endsWithNul: boolean }
- | { ok: false; reason: 'exit' | 'signal' | 'timeout' | 'too_large' | 'unavailable'; status?: number; signal?: NodeJS.Signals };
+ | { ok: false; reason: 'exit' | 'signal' | 'timeout' | 'too_large' | 'unavailable' | 'stalled'; status?: number; signal?: NodeJS.Signals };
+
+/**
+ * How long stdout may sit idle after Git exited while it is being read (not held by `onChunk`). Bun can lose the readiness
+ * of a child's stdout that turns readable as the child exits (#7053: the one-shot poll fired and was never re-armed, the
+ * output and EOF left unread), so `close` never comes; such a run fails as `stalled` instead of waiting forever.
+ */
+const EXITED_STDOUT_IDLE_MS = 5_000;
export type HardenedGitInput = string | Buffer | Iterable<Buffer | string> | AsyncIterable<Buffer | string>;
/**
@@ -111,18 +118,27 @@ export async function hardenedGitStream(root: string, args: string[],
let stopped: 'timeout' | 'too_large' | undefined;
let failure: { error: unknown } | undefined;
let settled = false;
- const settle = (outcome: () => void): void => { if (settled) return; settled = true; clearTimeout(timer); outcome(); };
+ let exited = false;
+ let holding = false;
+ let idle: ReturnType<typeof setTimeout> | undefined;
+ const settle = (outcome: () => void): void => { if (settled) return; settled = true; clearTimeout(timer); clearTimeout(idle); outcome(); };
const fail = (error: unknown): void => { failure ??= { error }; child.kill('SIGKILL'); };
const timer = setTimeout(() => { stopped ??= 'timeout'; child.kill('SIGKILL'); }, opts.timeoutMs);
+ const watchIdle = (): void => {
+ clearTimeout(idle);
+ if (exited && !holding) idle = setTimeout(() => settle(() => failure ? reject(failure.error) : accept({ ok: false, reason: stopped ?? 'stalled' })), EXITED_STDOUT_IDLE_MS);
+ };
child.stdout?.on('data', (chunk: Buffer) => {
- if (stopped || failure) return;
+ if (settled || stopped || failure) return;
bytes += chunk.length;
if (opts.maxBytes !== undefined && bytes > opts.maxBytes) { stopped = 'too_large'; child.kill('SIGKILL'); return; }
last = chunk[chunk.length - 1] ?? last;
let held: void | Promise<void>;
try { held = opts.onChunk?.(chunk); } catch (error) { fail(error); return; }
- if (held) { child.stdout?.pause(); held.then(() => child.stdout?.resume(), fail); }
+ if (held) { holding = true; child.stdout?.pause(); held.then(() => { holding = false; watchIdle(); child.stdout?.resume(); }, fail); }
+ watchIdle();
});
+ child.on('exit', () => { exited = true; watchIdle(); });
child.on('error', () => { if (child.pid === undefined) settle(() => accept({ ok: false, reason: 'unavailable' })); });
child.on('close', (code: number | null, signal: NodeJS.Signals | null) => settle(() => {
if (failure) reject(failure.error);
diff --git a/test/hardened-git-stream-6712.test.ts b/test/hardened-git-stream-6712.test.ts
index 74f87d2837..a764842bab 100644
--- a/test/hardened-git-stream-6712.test.ts
+++ b/test/hardened-git-stream-6712.test.ts
@@ -5,8 +5,11 @@
* these reads runs the checkout's clean/process filter or textconv, while
* porcelain on the same checkout does.
*/
-import { afterAll, afterEach, describe, expect, test } from 'bun:test';
+import { afterAll, afterEach, describe, expect, spyOn, test } from 'bun:test';
+import * as childProcess from 'node:child_process';
import { execFileSync } from 'node:child_process';
+import { EventEmitter } from 'node:events';
+import { PassThrough } from 'node:stream';
import { chmodSync, existsSync, mkdtempSync, rmSync, symlinkSync, unlinkSync, utimesSync, writeFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
import { tmpdir } from 'node:os';
@@ -91,6 +94,26 @@ describe('hardenedGitStream', () => {
await expect(hardenedGitStream(root, ['status', '--porcelain'], { timeoutMs: 10_000 })).rejects.toThrow(/hardened git refuses/);
});
+ // #7053: under Bun a child's stdout that turns readable as the child exits can lose its readiness (its one-shot epoll
+ // registration fired and was never re-armed, the record and EOF left unread), so 'exit' arrives and stdout never ends or
+ // closes. The read waited for 'close' with nothing to bound it: its timeout only SIGKILLs a child that is already gone.
+ test('a child that exited while its stdout stopped delivering fails the read instead of hanging it', async () => {
+ const child = Object.assign(new EventEmitter(), { pid: 4242, stdout: new PassThrough(), stdin: null, kill: () => true });
+ const spawn = spyOn(childProcess, 'spawn').mockImplementation((() => {
+ setTimeout(() => { child.stdout.write('100644 blob 708d805e8798c0eba388e1826208be3929aabf5f'); child.emit('exit', 0, null); }, 10);
+ return child;
+ }) as unknown as typeof childProcess.spawn);
+ try {
+ const chunks: Buffer[] = [];
+ const read = hardenedGitStream('/nonexistent-root', ['ls-tree', '-r', '-z', 'HEAD', '--', '.'], { timeoutMs: 120_000, onChunk: chunk => { chunks.push(chunk); } });
+ expect(await Promise.race([read, new Promise(resolve => setTimeout(() => resolve('still waiting'), 10_000))])).toEqual({ ok: false, reason: 'stalled' });
+ expect(Buffer.concat(chunks).toString()).toBe('100644 blob 708d805e8798c0eba388e1826208be3929aabf5f');
+ child.stdout.write('\tlate\0');
+ await new Promise(resolve => setTimeout(resolve, 20));
+ expect(chunks.length).toBe(1);
+ } finally { spawn.mockRestore(); }
+ }, 15_000);
+
test('a non-zero exit, a byte cap, a timeout and an external signal are each reported as such', async () => {
const root = repo({ 'a.md': 'a\n' });
expect(await hardenedGitStream(root, ['ls-tree', '-r', '-z', 'no-such-ref'], { timeoutMs: 10_000 })).toEqual({ ok: false, reason: 'exit', status: 128 });