Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: add a timeout to execGh so a stalled gh CLI call can't hang scheduled jobs

[._.]/ Adam Eivy0482ae082d19
diff --git a/server/services/github.js b/server/services/github.js
index d1d39988f0..f792c018bd 100644
--- a/server/services/github.js
+++ b/server/services/github.js
@@ -35,24 +35,45 @@ async function save(data) {
cacheTimestamp = Date.now();
}
+const DEFAULT_EXEC_GH_TIMEOUT_MS = 60000;
+
/**
- * Execute a gh CLI command safely using spawn
+ * Execute a gh CLI command safely using spawn. `gh` hits the network, so a
+ * stalled call (bad credentials prompt, hung network) would otherwise leave
+ * the returned promise pending forever and wedge scheduled jobs (prWatcher,
+ * issueReconcile, branchReconcile, updateChecker) while orphaning the child
+ * process. `timeoutMs` kills the child and rejects with a clear error; it's
+ * cleared on normal exit so it never fires for a completed run.
*/
-export function execGh(args) {
+export function execGh(args, timeoutMs = DEFAULT_EXEC_GH_TIMEOUT_MS) {
return new Promise((resolve, reject) => {
const child = spawn('gh', args, { shell: false, windowsHide: true });
let stdout = '';
let stderr = '';
+ let timedOut = false;
+ const timer = setTimeout(() => {
+ timedOut = true;
+ // setTimeout callback boundary — guard so a kill() throw can't crash
+ // the process (e.g. the child already exited between checks).
+ try { child.kill('SIGKILL'); } catch (err) { console.error(`❌ execGh: failed to kill timed-out 'gh ${args.join(' ')}': ${err.message}`); }
+ reject(new Error(`gh ${args.join(' ')} timed out after ${timeoutMs}ms`));
+ }, timeoutMs);
child.stdout.on('data', (d) => { stdout += d.toString(); });
child.stderr.on('data', (d) => { stderr += d.toString(); });
child.on('close', (code) => {
+ if (timedOut) return;
+ clearTimeout(timer);
if (code !== 0) {
reject(new Error(stderr.trim() || `gh exited with code ${code}`));
} else {
resolve(stdout.trim());
}
});
- child.on('error', (err) => reject(err));
+ child.on('error', (err) => {
+ if (timedOut) return;
+ clearTimeout(timer);
+ reject(err);
+ });
});
}
diff --git a/server/services/github.test.js b/server/services/github.test.js
new file mode 100644
index 0000000000..2035779d98
--- /dev/null
+++ b/server/services/github.test.js
@@ -0,0 +1,86 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { EventEmitter } from 'events';
+
+vi.mock('child_process', async (importOriginal) => {
+ const actual = await importOriginal();
+ return { ...actual, spawn: vi.fn() };
+});
+
+import { spawn } from 'child_process';
+import { execGh } from './github.js';
+
+const makeChild = () => {
+ const child = new EventEmitter();
+ child.stdout = new EventEmitter();
+ child.stderr = new EventEmitter();
+ child.kill = vi.fn();
+ return child;
+};
+
+describe('execGh', () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ vi.clearAllMocks();
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ it('rejects with a timeout error and kills the child when it never closes', async () => {
+ const child = makeChild();
+ spawn.mockReturnValue(child);
+ const promise = execGh(['api', 'slow'], 50);
+ // Suppress unhandled-rejection noise until we await below.
+ promise.catch(() => {});
+ vi.advanceTimersByTime(50);
+ await expect(promise).rejects.toThrow(/timed out after 50ms/);
+ expect(child.kill).toHaveBeenCalledWith('SIGKILL');
+ });
+
+ it('resolves with trimmed stdout on a successful close', async () => {
+ const child = makeChild();
+ spawn.mockReturnValue(child);
+ const promise = execGh(['api', 'repos'], 5000);
+ child.stdout.emit('data', Buffer.from(' {"ok":true} \n'));
+ child.emit('close', 0);
+ await expect(promise).resolves.toBe('{"ok":true}');
+ });
+
+ it('rejects with stderr on a non-zero close', async () => {
+ const child = makeChild();
+ spawn.mockReturnValue(child);
+ const promise = execGh(['api', 'bad'], 5000);
+ child.stderr.emit('data', Buffer.from('not found'));
+ child.emit('close', 1);
+ await expect(promise).rejects.toThrow(/not found/);
+ });
+
+ it('falls back to a generic error message when stderr is empty on non-zero close', async () => {
+ const child = makeChild();
+ spawn.mockReturnValue(child);
+ const promise = execGh(['api', 'bad'], 5000);
+ child.emit('close', 7);
+ await expect(promise).rejects.toThrow(/gh exited with code 7/);
+ });
+
+ it('does not fire the timeout timer on a fast normal completion', async () => {
+ const child = makeChild();
+ spawn.mockReturnValue(child);
+ const promise = execGh(['api', 'fast'], 5000);
+ child.stdout.emit('data', Buffer.from('done'));
+ child.emit('close', 0);
+ await expect(promise).resolves.toBe('done');
+ // Advancing well past the timeout must not reject/kill after settling.
+ vi.advanceTimersByTime(10000);
+ expect(child.kill).not.toHaveBeenCalled();
+ });
+
+ it('rejects on a child spawn error', async () => {
+ const child = makeChild();
+ spawn.mockReturnValue(child);
+ const promise = execGh(['api', 'x'], 5000);
+ child.emit('error', new Error('spawn gh ENOENT'));
+ await expect(promise).rejects.toThrow(/ENOENT/);
+ });
+});