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: clear pullRepo timeout timer and guard child process lifecycle (#10102)

Adam Eivy77743ee30cc5
diff --git a/server/services/repoCloner.js b/server/services/repoCloner.js
index b929588871..f236c63f75 100644
--- a/server/services/repoCloner.js
+++ b/server/services/repoCloner.js
@@ -229,6 +229,8 @@ export async function pullRepo(localPath) {
console.log(`🔄 Pulling latest for ${localPath}...`);
return new Promise((resolve, reject) => {
+ let settled = false;
+
const child = spawn('git', ['pull', '--ff-only'], {
cwd: localPath,
env: process.env,
@@ -246,22 +248,36 @@ export async function pullRepo(localPath) {
stderr += data.toString();
});
+ const timeout = setTimeout(() => {
+ if (!settled) {
+ settled = true;
+ child.kill();
+ reject(new Error('Pull timed out after 2 minutes'));
+ }
+ }, 120000);
+
+ timeout.unref?.();
+
child.on('close', (code) => {
- if (code === 0) {
- console.log(`✅ Pulled latest for ${localPath}`);
- resolve({ stdout, stderr, success: true });
- } else {
- console.error(`❌ Failed to pull ${localPath}: ${stderr}`);
- reject(new Error(`Git pull failed: ${stderr || `exit code ${code}`}`));
+ clearTimeout(timeout);
+ if (!settled) {
+ settled = true;
+ if (code === 0) {
+ console.log(`✅ Pulled latest for ${localPath}`);
+ resolve({ stdout, stderr, success: true });
+ } else {
+ console.error(`❌ Failed to pull ${localPath}: ${stderr}`);
+ reject(new Error(`Git pull failed: ${stderr || `exit code ${code}`}`));
+ }
}
});
- child.on('error', reject);
-
- // Timeout after 2 minutes
- setTimeout(() => {
- child.kill();
- reject(new Error('Pull timed out after 2 minutes'));
- }, 120000);
+ child.on('error', (err) => {
+ clearTimeout(timeout);
+ if (!settled) {
+ settled = true;
+ reject(err);
+ }
+ });
});
}
diff --git a/server/services/repoCloner.test.js b/server/services/repoCloner.test.js
index 2b0f864835..14ad8660ce 100644
--- a/server/services/repoCloner.test.js
+++ b/server/services/repoCloner.test.js
@@ -19,7 +19,7 @@ vi.mock('../lib/fileUtils.js', () => ({
import { existsSync } from 'fs';
import { access, mkdtemp, readdir, rename, rm } from 'fs/promises';
import { spawn } from '../lib/childProcess.js';
-import { cloneRepo, reapStaleCloneStaging } from './repoCloner.js';
+import { cloneRepo, reapStaleCloneStaging, pullRepo } from './repoCloner.js';
const REPOS_DIR = '/repos';
const OWNER_DIR = join(REPOS_DIR, 'acme');
@@ -292,3 +292,101 @@ describe('cloneRepo across hosts', () => {
expect(spawn).not.toHaveBeenCalled();
});
});
+
+describe('pullRepo', () => {
+ const localPath = '/local/repo';
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ existsSync.mockReturnValue(true);
+ });
+
+ const createChildWithStdout = () => {
+ const child = new EventEmitter();
+ child.stdout = new EventEmitter();
+ child.stderr = new EventEmitter();
+ child.kill = vi.fn();
+ return child;
+ };
+
+ it('clears the timeout when git pull exits successfully', async () => {
+ const child = createChildWithStdout();
+ spawn.mockReturnValue(child);
+
+ const resultPromise = pullRepo(localPath);
+ await vi.waitFor(() => expect(spawn).toHaveBeenCalled());
+
+ // Verify spawn was called with the correct git command
+ expect(spawn).toHaveBeenCalledWith('git', ['pull', '--ff-only'], expect.objectContaining({
+ cwd: localPath,
+ shell: false
+ }));
+
+ child.emit('close', 0);
+
+ const result = await expect(resultPromise).resolves.toMatchObject({
+ success: true
+ });
+ // Verify that child.kill() was NOT called by the timeout
+ expect(child.kill).not.toHaveBeenCalled();
+ });
+
+ it('clears the timeout when git pull encounters an error', async () => {
+ const child = createChildWithStdout();
+ spawn.mockReturnValue(child);
+
+ const resultPromise = pullRepo(localPath);
+ await vi.waitFor(() => expect(spawn).toHaveBeenCalled());
+
+ child.stderr.emit('data', Buffer.from('fatal: not a git repository'));
+ child.emit('close', 128);
+
+ await expect(resultPromise).rejects.toThrow('Git pull failed');
+ // Verify that child.kill() was NOT called by the timeout
+ expect(child.kill).not.toHaveBeenCalled();
+ });
+
+ it('clears the timeout when the child process emits an error', async () => {
+ const child = createChildWithStdout();
+ spawn.mockReturnValue(child);
+
+ const resultPromise = pullRepo(localPath);
+ await vi.waitFor(() => expect(spawn).toHaveBeenCalled());
+
+ const testError = new Error('spawn failed');
+ child.emit('error', testError);
+
+ await expect(resultPromise).rejects.toThrow('spawn failed');
+ // Verify that child.kill() was NOT called by the timeout
+ expect(child.kill).not.toHaveBeenCalled();
+ });
+
+ it('rejects if the path is not a git repository', async () => {
+ existsSync.mockReturnValue(false);
+
+ await expect(pullRepo(localPath)).rejects.toThrow('Not a git repository');
+ expect(spawn).not.toHaveBeenCalled();
+ });
+
+ it('does not settle the promise twice when timeout fires after close event', async () => {
+ const child = createChildWithStdout();
+ spawn.mockReturnValue(child);
+
+ const resultPromise = pullRepo(localPath);
+ await vi.waitFor(() => expect(spawn).toHaveBeenCalled());
+
+ // Get a reference to the timeout so we can verify it was called
+ const timeoutHandle = vi.useFakeTimers();
+
+ // Simulate close event
+ child.emit('close', 0);
+
+ // Attempt to fire the timeout (it should be cleared and not call reject)
+ // The real promise should resolve with the close event, not be rejected by timeout
+ const result = await resultPromise;
+ expect(result.success).toBe(true);
+ expect(child.kill).not.toHaveBeenCalled();
+
+ vi.useRealTimers();
+ });
+});