Skip to content

fix: check transport liveness before idle-timeout shutdown#21

Merged
ForLoopCodes merged 1 commit intoForLoopCodes:mainfrom
overtimepog:fix/idle-timeout-transport-check
Mar 28, 2026
Merged

fix: check transport liveness before idle-timeout shutdown#21
ForLoopCodes merged 1 commit intoForLoopCodes:mainfrom
overtimepog:fix/idle-timeout-transport-check

Conversation

@overtimepog
Copy link
Copy Markdown
Contributor

Problem

The idle monitor in process-lifecycle.ts fires after 15 minutes (default) of no tool calls and unconditionally triggers process.exit(). This kills the MCP server even when the agent connection (stdio transport) is still alive — the agent may simply be idle (thinking, waiting for user input, reading a long response, etc).

The abrupt exit breaks the stdio pipe, which manifests as an SSE stream break to the calling agent, crashing the agent session.

Root Cause

createIdleMonitor has no awareness of whether the transport is still connected. It treats "no tool calls for N minutes" as "nobody is using me" — but that's wrong when the agent is just idle between requests.

The existing shutdown guards (parent-exit monitor, stdin-end, stdin-close, SIGINT/SIGTERM/SIGHUP, broken-pipe detection) already cover all legitimate shutdown scenarios. The idle timeout is a false positive that kills healthy connections.

Fix

Added an optional isTransportAlive callback to IdleMonitorOptions:

interface IdleMonitorOptions {
  timeoutMs: number;
  onIdle: () => void;
  isTransportAlive?: () => boolean;  // NEW
}

When the idle timer fires:

  1. If isTransportAlive is provided and returns truereschedule the timer (don't shutdown)
  2. If isTransportAlive returns false → proceed with shutdown (transport is dead)
  3. If isTransportAlive is not provided → original behavior (unconditional shutdown, backward compat)

In index.ts, the callback checks process.stdin.readable && !process.stdin.destroyed.

Changes

  • src/core/process-lifecycle.ts: Added isTransportAlive? to IdleMonitorOptions, check it before firing onIdle
  • src/index.ts: Pass isTransportAlive that checks stdin liveness
  • test/main/idle-timeout-spawn.test.mjs: 7 new tests (5 unit + 2 spawn-level integration)

Test Results

All 15 tests pass (8 original + 7 new):

✔ idle-timeout transport-aware fix (7 tests)
  ✔ does NOT fire onIdle when isTransportAlive returns true
  ✔ fires onIdle when isTransportAlive returns false
  ✔ fires onIdle normally when no isTransportAlive provided (backward compat)
  ✔ reschedules then fires when transport dies after initial alive check
  ✔ touch resets the idle timer even with transport check
  ✔ spawn: without isTransportAlive, server exits on idle with stdin open
  ✔ spawn: with isTransportAlive, server survives idle when stdin is open

✔ process-lifecycle (8 original tests - all still pass)

The spawn-level tests prove the bug at the process level: a child process with stdin: 'pipe' (open, readable) gets killed by idle-timeout without the fix, and survives with it.

The idle monitor fires after 15 minutes of no tool calls and triggers
process.exit(). This kills the MCP server even when the agent connection
(stdio transport) is still alive - the agent may just be idle (thinking,
waiting for user input, etc). The abrupt exit breaks the SSE stream for
the calling agent.

The fix adds an optional isTransportAlive callback to createIdleMonitor.
When the idle timer fires, it checks whether the transport (stdin) is
still readable. If so, the timer reschedules instead of shutting down.
When the transport is actually dead, shutdown proceeds normally.

In index.ts, the callback checks process.stdin.readable && !destroyed.

Backward compatible: when isTransportAlive is not provided, the original
behavior (unconditional shutdown) is preserved.

Includes 7 new tests:
- 5 unit tests for createIdleMonitor with isTransportAlive
- 2 spawn-level integration tests proving the bug (without fix) and
  the fix (with isTransportAlive) at the process level
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 27, 2026

@overtimepog is attempting to deploy a commit to the ForLoopCodes' projects Team on Vercel.

A member of the Team first needs to authorize it.

@ForLoopCodes
Copy link
Copy Markdown
Owner

lgtm

@ForLoopCodes ForLoopCodes merged commit b731912 into ForLoopCodes:main Mar 28, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants