Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): require two absent group probes before reporting held stdio

Alfredo Lopezcc795e690ca6
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 691b61787..d589c42bd 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.555",
+ "version": "0.1.556",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.555",
+ "version": "0.1.556",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index b007a7164..4ea1e8ea3 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.555",
+ "version": "0.1.556",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/tools/__tests__/shell.detachedStdio.test.ts b/mcp-host/src/core/tools/__tests__/shell.detachedStdio.test.ts
index ded045544..063129f85 100644
--- a/mcp-host/src/core/tools/__tests__/shell.detachedStdio.test.ts
+++ b/mcp-host/src/core/tools/__tests__/shell.detachedStdio.test.ts
@@ -289,7 +289,7 @@ describe('ShellTool with stdio held by a process outside its group (#1028 P1)',
)
it(
- 'P1-c: a command that exits 0 after starting an escaped holder settles about one drain interval after exit, not at the execution timeout',
+ 'P1-c: a command that exits 0 after starting an escaped holder settles about two drain intervals after exit, not at the execution timeout',
async () => {
const { prefix, pidFile } = await escapedHolder()
const tool = new ShellTool(workspacePath, NATURAL_EXIT_EXECUTION_TIMEOUT_MS, ['PATH'])
@@ -304,7 +304,7 @@ describe('ShellTool with stdio held by a process outside its group (#1028 P1)',
const result = await execution
const elapsed = performance.now() - startedAt
- expect(elapsed).toBeGreaterThanOrEqual(SHELL_STDIO_DRAIN_MS)
+ expect(elapsed).toBeGreaterThanOrEqual(2 * SHELL_STDIO_DRAIN_MS)
expect(elapsed).toBeLessThan(NATURAL_EXIT_EXECUTION_TIMEOUT_MS / 3)
expect(result.is_error).toBe(true)
expect(result.content).toBe(`${notice('exited')}stdout:\nown-beforeREADY\nown-after`)
@@ -717,4 +717,78 @@ describe('ShellTool single finalizer races (#1028 P1, hermetic child)', () => {
expect(JSON.stringify(warn.mock.calls)).not.toContain('probe failed')
expect(vi.getTimerCount()).toBe(0)
})
+
+ /** Intercepts process.kill; every group probe confirms absence. */
+ function groupAlreadyGone() {
+ const signals: Array<{ pid: number; signal: unknown; at: number }> = []
+ vi.spyOn(process, 'kill').mockImplementation((pid: number, signal?: string | number) => {
+ signals.push({ pid, signal, at: performance.now() })
+ if (signal === 0) throw groupGone()
+ return true
+ })
+ return signals
+ }
+
+ it('P1-i: a pipe EOF that arrives after the first absent group probe keeps the natural-exit result', async () => {
+ useFakeClock()
+ const child = hermeticChild()
+ vi.mocked(spawn).mockImplementationOnce(() => child as never)
+ const signals = groupAlreadyGone()
+ const startedAt = performance.now()
+ const execution = new ShellTool(workspacePath, 30_000, []).execute({
+ command: 'hermetic command',
+ })
+ child.stdout.emit('data', Buffer.from('early'))
+ child.exitCode = 0
+ child.emit('exit', 0, null)
+
+ await vi.advanceTimersByTimeAsync(SHELL_STDIO_DRAIN_MS)
+ // Witness: the drain poll already saw the group gone before the EOF.
+ expect(signals).toEqual([{ pid: -FAKE_PID, signal: 0, at: startedAt + SHELL_STDIO_DRAIN_MS }])
+ // The EOF misses the poll phase: anything that probe scheduled runs first.
+ await vi.advanceTimersByTimeAsync(1)
+ child.stdout.emit('data', Buffer.from('late'))
+ child.emit('close', 0)
+ await vi.advanceTimersByTimeAsync(SHELL_STDIO_DRAIN_MS)
+ const result = await execution
+
+ expect(result).toEqual({
+ content: 'stdout:\nearlylate',
+ duration_ms: expect.any(Number),
+ is_error: false,
+ })
+ expect(heldWarnings()).toEqual([])
+ expect(vi.getTimerCount()).toBe(0)
+ })
+
+ it('P1-j: a group gone on two consecutive probes without a pipe EOF reports the detached holder at the second probe', async () => {
+ useFakeClock()
+ const child = hermeticChild()
+ vi.mocked(spawn).mockImplementationOnce(() => child as never)
+ const signals = groupAlreadyGone()
+ const startedAt = performance.now()
+ let settledAt: number | undefined
+ const execution = new ShellTool(workspacePath, 30_000, []).execute({
+ command: 'hermetic command',
+ })
+ void execution.then(() => {
+ settledAt = performance.now()
+ })
+ child.stdout.emit('data', Buffer.from('natural output'))
+ child.exitCode = 0
+ child.emit('exit', 0, null)
+
+ await vi.advanceTimersByTimeAsync(2 * SHELL_STDIO_DRAIN_MS)
+ // The fake clock runs the setImmediate a probe schedules on the next tick.
+ await vi.advanceTimersByTimeAsync(1)
+ const result = await execution
+
+ // A publication at the first absent probe would record one drain interval + 1.
+ expect(settledAt).toBe(startedAt + 2 * SHELL_STDIO_DRAIN_MS + 1)
+ expect(signals.filter(entry => entry.signal === 0)).toHaveLength(2)
+ expect(result.is_error).toBe(true)
+ expect(result.content).toBe(`${notice('exited')}stdout:\nnatural output`)
+ expect(heldWarnings()).toEqual([expect.objectContaining({ reason: 'exited' })])
+ expect(vi.getTimerCount()).toBe(0)
+ })
})
diff --git a/mcp-host/src/core/tools/shell.ts b/mcp-host/src/core/tools/shell.ts
index ff3ef9120..f459ffb59 100644
--- a/mcp-host/src/core/tools/shell.ts
+++ b/mcp-host/src/core/tools/shell.ts
@@ -545,8 +545,19 @@ export class ShellTool implements Tool {
child.once('exit', () => {
const processGroupId = child.pid
if (killed || finalizing || !isValidProcessId(processGroupId)) return
+ let absentProbes = 0
drainTimer = setInterval(() => {
- if (finalizing || processGroupExists(processGroupId)) return
+ if (finalizing) return
+ if (processGroupExists(processGroupId)) {
+ absentProbes = 0
+ return
+ }
+ // The last in-group writer can exit before its pipe EOF reaches this
+ // process; under load that EOF can miss the poll phase below. Only a
+ // second consecutive absent probe without `close` means a process
+ // outside the group holds stdio.
+ absentProbes += 1
+ if (absentProbes < 2) return
// A pipe EOF pending in this poll phase delivers `close` first.
setImmediate(() => {
if (!claimFinalization()) return
diff --git a/mcp-host/src/core/tools/shellTimeouts.ts b/mcp-host/src/core/tools/shellTimeouts.ts
index f53f0b64b..e47077ae0 100644
--- a/mcp-host/src/core/tools/shellTimeouts.ts
+++ b/mcp-host/src/core/tools/shellTimeouts.ts
@@ -3,8 +3,10 @@ export const SHELL_SIGKILL_GRACE_MS = 5000
/**
* Interval at which a shell whose leader exited without a cause checks whether
- * its process group is gone. When the group is gone but stdout/stderr are still
- * held open by a process outside it, output capture stops and the call settles.
+ * its process group is gone. When two consecutive checks find the group gone and
+ * stdout/stderr are still held open by a process outside it, output capture stops
+ * and the call settles. One check is not enough: the last in-group writer's pipe
+ * EOF can still be in flight when its group disappears.
*/
export const SHELL_STDIO_DRAIN_MS = 1000
diff --git a/scripts/ci/pinned-vitest-cases/mcp-host.json b/scripts/ci/pinned-vitest-cases/mcp-host.json
index 3b845707d..489ac2624 100644
--- a/scripts/ci/pinned-vitest-cases/mcp-host.json
+++ b/scripts/ci/pinned-vitest-cases/mcp-host.json
@@ -121,12 +121,20 @@
},
{
"suite": "shell.detachedStdio.test.ts",
- "fullName": "ShellTool with stdio held by a process outside its group (#1028 P1) P1-c: a command that exits 0 after starting an escaped holder settles about one drain interval after exit, not at the execution timeout"
+ "fullName": "ShellTool with stdio held by a process outside its group (#1028 P1) P1-c: a command that exits 0 after starting an escaped holder settles about two drain intervals after exit, not at the execution timeout"
},
{
"suite": "shell.detachedStdio.test.ts",
"fullName": "ShellTool with stdio held by a process outside its group (#1028 P1) P1-g: an asynchronous start failure reports only the error code and publishes one result"
},
+ {
+ "suite": "shell.detachedStdio.test.ts",
+ "fullName": "ShellTool single finalizer races (#1028 P1, hermetic child) P1-i: a pipe EOF that arrives after the first absent group probe keeps the natural-exit result"
+ },
+ {
+ "suite": "shell.detachedStdio.test.ts",
+ "fullName": "ShellTool single finalizer races (#1028 P1, hermetic child) P1-j: a group gone on two consecutive probes without a pipe EOF reports the detached holder at the second probe"
+ },
{
"suite": "shell.detachedStdio.test.ts",
"fullName": "ShellTool single finalizer races (#1028 P1, hermetic child) R1-H1: a close delivered 5900ms after SIGTERM with a group probe that never confirms absence settles within the cleanup budget and reports process_group_termination_failed"