Heuristic:AI QL Tuui Idle Timeout Race
| Knowledge Sources | |
|---|---|
| Domains | MCP, Timeout, Reliability |
| Last Updated | 2026-10-24 20:38 GMT |
Overview
Race the MCP stdio client initialization against an idle-activity timeout to prevent hanging on unresponsive servers.
Description
When spawning an MCP server as a stdio child process, the server may hang silently — accepting the connection but never completing the MCP handshake or producing any output. The idle-timeout race pattern wraps the stdio connection promise together with a timer that resets on any stderr activity. If no data is received within the timeout window (default 90 seconds), the promise rejects with a timeout error. The timer is reset whenever the server emits stderr data, distinguishing "slow but working" from "completely stuck."
Usage
Apply this heuristic when initializing MCP stdio clients that may hang indefinitely. The symptom is a server initialization that never resolves or rejects — the application appears frozen during startup.
The Insight (Rule of Thumb)
- Action: Wrap the stdio connection promise in a
Promise.race()against an idle timeout. - Value: Default
idleTimeout = 90seconds. Reset the timer on anystderrdata event. - Trade-off: Servers that produce no stderr output during startup will hit the timeout even if they are working. Long-running initialization may be falsely aborted.
- Compatibility: Works with any
StdioClientTransportthat pipes stderr.
Reasoning
A plain await on client.connect(transport) has no escape hatch if the child process hangs. The race pattern ensures the init promise always settles. Resetting the timer on stderr activity prevents premature timeout for servers that log progress during startup. The 90-second default is generous enough for most MCP servers (typically Python or Node processes) while catching genuinely stuck processes.
Code Evidence
Idle timeout race from `src/main/mcp/client.ts:14-42`:
export async function initializeClient(
name: string,
serverConfig: McpServerConfig,
callback?: McpProgressCallback,
idleTimeout: number = 90
): Promise<McpClientTransport> {
let idleTimer: NodeJS.Timeout
let rejectFn: (_reason: Error) => void
const resetTimer = () => {
clearTimeout(idleTimer)
idleTimer = setTimeout(() => {
rejectFn(new Error(
`Initialization of client for ${name} timed out after ${idleTimeout} seconds of inactivity`
))
}, idleTimeout * 1000)
}
const timeoutPromise = new Promise<McpClientTransport>((_, reject) => {
rejectFn = reject
resetTimer()
})
const stdioPromise = initializeStdioClient(name, serverConfig, callback, resetTimer)
return Promise.race([stdioPromise, timeoutPromise])
}
Timer reset on stderr from `src/main/mcp/client.ts:68-73`:
if (transport.stderr) {
transport.stderr.on('data', (chunk) => {
if (onData) onData() // resets idle timer
if (callback) callback(name, chunk.toString(), 'pending')
})
}