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: guard command runner child process against duplicate completion on spawn error (#10100)

Adam Eivyde6b56229a88
diff --git a/server/routes/commands.test.js b/server/routes/commands.test.js
index 9578c38b0a..8e4132aaff 100644
--- a/server/routes/commands.test.js
+++ b/server/routes/commands.test.js
@@ -398,6 +398,53 @@ describe('commands routes', () => {
output: 'workspace outputworkspace warning',
}));
});
+
+ it('invokes onComplete exactly once with error details when child process fails to spawn, even if close emits after error', async () => {
+ allowWorkspace();
+ const child = createChildProcess();
+ spawnMock.mockReturnValue(child);
+ const { app, io } = createApp();
+ const { logAction } = await import('../services/history.js');
+
+ const response = await request(app)
+ .post('/api/commands/execute')
+ .send({ command: 'pwd', workspacePath: WORKSPACE_PATH });
+
+ expect(response.status).toBe(202);
+ const commandId = response.body.commandId;
+ const eventPrefix = `command:${commandId}`;
+
+ // Simulate spawn error: when a binary is not found, the error event fires
+ const spawnError = new Error('ENOENT: no such file or directory');
+ child.emit('error', spawnError);
+
+ // Node.js guarantees that close always emits after error. Verify we handle it safely.
+ child.emit('close', null);
+
+ // Verify onComplete was called exactly once with the error details
+ expect(io.emit).toHaveBeenCalledWith(`${eventPrefix}:complete`, {
+ success: false,
+ error: 'ENOENT: no such file or directory',
+ exitCode: 1,
+ });
+
+ // Count completion events to ensure only one was emitted
+ const completionCalls = io.emit.mock.calls.filter(
+ call => call[0] === `${eventPrefix}:complete`
+ );
+ expect(completionCalls).toHaveLength(1);
+
+ // Verify history was logged exactly once
+ expect(logAction).toHaveBeenCalledTimes(1);
+ expect(logAction).toHaveBeenCalledWith(
+ 'command',
+ null,
+ expect.any(String),
+ expect.objectContaining({ command: 'pwd' }),
+ false,
+ 'ENOENT: no such file or directory'
+ );
+ });
});
describe('POST /api/commands/:id/stop', () => {
diff --git a/server/services/commands.js b/server/services/commands.js
index 5feb6856a6..19d066ce5b 100644
--- a/server/services/commands.js
+++ b/server/services/commands.js
@@ -31,6 +31,7 @@ export function executeCommand(command, workspacePath, onData, onComplete) {
const { baseCommand, args } = validation;
const startTime = Date.now();
let output = '';
+ let settled = false;
// Security: Use spawn with array of args (shell:false) to prevent shell injection.
// validateCommand has already rejected shell metacharacters AND parsed quoted args
@@ -64,6 +65,8 @@ export function executeCommand(command, workspacePath, onData, onComplete) {
});
child.on('close', (code) => {
+ if (settled) return;
+ settled = true;
activeCommands.delete(commandId);
const runtime = Date.now() - startTime;
const success = code === 0;
@@ -85,6 +88,8 @@ export function executeCommand(command, workspacePath, onData, onComplete) {
});
child.on('error', (err) => {
+ if (settled) return;
+ settled = true;
activeCommands.delete(commandId);
logAction('command', null, command.substring(0, 50), { command, workspacePath }, false, err.message);
onComplete?.({