Heuristic:AI Shell Team Aish MCP Reconnect Backoff
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Distributed_Systems, MCP |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Bounded exponential backoff ladder (500ms → 1000ms → 2000ms → 4000ms) with a two-ladder ceiling prevents MCP server reconnection storms while keeping recovery responsive.
Description
When an MCP server connection drops, the `McpManager` driver attempts reconnection using a fixed four-step backoff ladder. Each step is one full connect attempt; a failed step doubles the delay before the next try. After exhausting all four steps, the driver either succeeds, retries the entire ladder (up to `MAX_RECOVERY_LADDERS = 2`), or gives up and marks the server as `Failed`. This prevents an offline server from generating infinite reconnect traffic while still allowing reasonable recovery for transient network blips.
Usage
Use this heuristic when tuning MCP server reconnection behavior or diagnosing servers stuck in Failed state. Apply it when a server repeatedly disconnects, when reconnect attempts appear too aggressive or too conservative, or when designing custom MCP transport configurations.
The Insight (Rule of Thumb)
- Action: The reconnect backoff ladder is a compile-time constant: `[500ms, 1000ms, 2000ms, 4000ms]`.
- Value: `MAX_RECOVERY_LADDERS = 2` — at most 2 full ladder walks (8 total attempts) before giving up.
- Trade-off: Faster recovery for transient failures vs. avoiding reconnect storms on permanently-down servers. A server that stays down after 8 attempts (~11.5s total backoff + connect time) is left `Failed` rather than probed forever.
- Compatibility: Each reconnect step is interruptible — a `reload()` or `shutdown()` can cancel a pending reconnect by invalidating the `(generation, epoch)` guard.
Reasoning
An unbounded reconnect loop would generate constant network traffic against a dead server, wasting resources and potentially hammering the provider's rate limits. The exponential backoff gives the server time to recover between attempts (doubling delay), while the hard ceiling (`MAX_RECOVERY_LADDERS`) ensures the system eventually gives up and surfaces the failure to the user instead of silently retrying forever. The four-step ladder provides a good balance: total backoff time sums to 7.5s per ladder, and two ladders give a second chance for transient failures without excessive delay.
Code Evidence
Backoff ladder definition from `crates/aish-mcp/src/manager.rs:51-60`:
const RECONNECT_BACKOFF: [Duration; 4] = [
Duration::from_millis(500),
Duration::from_millis(1000),
Duration::from_millis(2000),
Duration::from_millis(4000),
];
const MAX_RECOVERY_LADDERS: usize = 2;
Give-up logic from `crates/aish-mcp/src/manager.rs:1194-1202`:
if ladders >= MAX_RECOVERY_LADDERS {
tracing::warn!(
server = %name,
"mcp: giving up automatic reconnection after {} attempts",
MAX_RECOVERY_LADDERS * RECONNECT_BACKOFF.len()
);
self.finish_driver(&name, generation, epoch);
return;
}