# Which invariants prevent MCP resource subscription starvation under HTTP/2 stream-window collapse **Date of Record:** 2026 November 21 **Scope:** Applies to MCP hosts that multiplex multiple `resources/subscribe` streams over a single HTTP/2 connection to one server, where the server pushes `notifications/resources/updated` frames. Does not apply to single-stream stdio transports or to servers that emit one update per request-response cycle. ## Core Distinction The failure here is not "the server sent too many notifications." It is that an MCP server's outbound `notifications/resources/updated` frames share a single HTTP/2 connection-level flow-control window (default 65,535 bytes) with every other stream on that connection, including the *responses to the agent's own tool calls and `resources/read` requests*. When a subscribed resource mutates faster than the agent's per-stream consumer drains it, the server's writes to that stream block on the stream window — but crucially, the HTTP/2 server implementation typically still allocates connection-window credit to *other* streams on a round-robin or FIFO basis. The observable symptom is not a clean stall on the hot subscription; it is that *unrelated* MCP requests (a `tools/call`, a `resources/read` for a different URI) start timing out because the connection window is pinned by a stream whose consumer is slow, while the slow stream itself appears healthy in the server's logs. The distinction that matters operationally: backpressure is a *connection-scoped* resource in HTTP/2, not a *subscription-scoped* one, and the MCP layer has no vocabulary to express that. ## What Was Observed A host agent maintained three subscriptions over one HTTP/2 connection to an MCP server: `file:///var/log/app.log` (high churn, ~200 updates/sec during bursts), `db://metrics/cpu` (steady 1/sec), and a `tools/call` to `search_index` issued periodically. Trace of the collapse: 1. `app.log` enters a burst. Server emits `notifications/resources/updated` frames at 200/sec. Each frame is a small JSON-RPC notification (~180 bytes) but each carries HTTP/2 framing overhead and consumes stream-window credit on stream 5. 2. The agent's subscription consumer for `app.log` is single-threaded and does a synchronous re-read (`resources/read file:///var/log/app.log`) on each notification to fetch content. That read is a *new* request on a *new* stream (stream 7), which competes for the same connection window. 3. At T+1.2s the connection-level window (`SETTINGS_INITIAL_WINDOW_SIZE` 65,535, no `WINDOW_UPDATE` coming back fast enough from the agent's HTTP/2 client because its read loop is blocked waiting on the synchronous `resources/read` round-trip) hits zero. The server's `send()` for *all* streams blocks. 4. The `tools/call search_index` issued at T+1.1s never gets its response written. The agent's tool-call timeout (30s) fires. The agent retries. The retry also queues behind the pinned window. 5. The `resources/read` for `app.log` that the consumer is blocked on is itself stuck in the same window. Deadlock: the consumer cannot drain the subscription because the drain requires a read that cannot be written because the window is full of undrained subscription notifications. The server logs showed no errors — `notifications/resources/updated` were "sent" (buffered in the server's HTTP/2 write queue). The agent logs showed `search_index` timeouts and eventually a `resources/read` timeout. Neither side reported the actual cause: a connection-window starvation loop between a subscription and the reads that subscription's own handler depends on. ## Condition of Validity This failure requires all of the following simultaneously: - **Multiplexed transport.** Multiple logical MCP streams share one HTTP/2 connection. Over stdio, each subscription is a separate pipe and the failure does not occur. - **Read-amplifying subscription handler.** The agent's handler for `notifications/resources/updated` performs a synchronous request (typically `resources/read` on the same URI) rather than consuming an inline payload. MCP's `resources/updated` notification carries only the URI, not the content — so this pattern is the *default* correct implementation, which is why the failure is common. - **No per-stream flow-control isolation.** The server does not reserve connection-window credit per stream, and the agent's HTTP/2 client does not aggressively return `WINDOW_UPDATE` on the hot stream independent of application-level consumption. - **Burst rate exceeding consumer drain rate.** If the subscription consumer keeps up, the window never pins. If any one condition is absent, the system degrades gracefully (slow but progressing) rather than deadlocking. The deadlock specifically requires the *cyclic dependency*: draining stream A needs a write on stream B, and stream B's write needs window credit that stream A is holding. ## Decision Boundary The invariant that must hold to prevent this is a **separation of the notification-drain path from the request-response path at the flow-control layer**, expressed as one of two concrete contracts: - **Contract A (server-side reservation):** The MCP server MUST NOT allow a single subscription stream to consume more than `floor(connection_window / active_streams)` bytes of connection-level credit without that stream's consumer having acknowledged prior notifications. In practice: the server tracks outstanding un-acked notifications per subscription and stops writing to that stream when the count exceeds a bound, *while continuing to serve other streams*. This requires the server to implement application-level backpressure (a `notifications/resources/updated` ack, or a subscription-level `WINDOW_UPDATE` discipline) rather than relying on HTTP/2's connection window. - **Contract B (agent-side decoupling):** The agent MUST NOT issue a synchronous `resources/read` from within the notification handler. It MUST enqueue the URI to a bounded local queue and drain it from a separate goroutine/task that owns its own request budget, so that a pinned subscription stream never blocks the request path. The queue bound is the backpressure signal: on overflow, the agent drops (and logs) notifications rather than blocking. The boundary decision: **if the agent cannot guarantee Contract B, the server must guarantee Contract A, or the deployment must pin each high-churn subscription to its own connection.** There is no configuration of the HTTP/2 window that substitutes for one of these — raising `SETTINGS_INITIAL_WINDOW_SIZE` only moves the deadlock threshold, it does not remove the cyclic dependency. ## Operational Effect Under the invariant, the observable behavior changes from *correlated timeouts across unrelated streams* to *bounded, localized degradation*. With Contract B, a 200/sec burst on `app.log` produces queue drops in the agent's subscription drain (visible, counted, attributable) while `search_index` and `db://metrics/cpu` continue at normal latency. With Contract A, the server stops emitting on the hot stream once the un-acked bound is hit, the connection window stays available for request-response traffic, and the burst is shed at the server with a `notifications/resources/updated` gap the agent can detect via a sequence number (if the server provides one) or via a subsequent full read. The critical operational lesson for an agent: **do not treat a successful `resources/subscribe` as a guarantee of delivery, and do not treat a `tools/call` timeout as evidence that the tool is broken.** In a multiplexed MCP connection, a timeout on an unrelated stream is a flow-control symptom, and the diagnostic move is to check whether any active subscription's consumer is blocked on a request. The guardrail is to instrument the subscription handler's queue depth and the request path's latency as a *single* signal, because in this failure mode they move together. ```python import asyncio from collections import deque class SubscriptionStarvationError(RuntimeError): """Raised when a subscription drain path would block the request path.""" pass class BoundedSubscriptionDrain: """ Contract B implementation: notification handler never issues a synchronous read. It enqueues URIs; a separate task drains them under its own budget. """ def __init__(self, uri: str, max_queue: int = 256, drain_batch: int = 16): self.uri = uri self._queue: deque[str] = deque() self._max = max_queue self._batch = drain_batch self.dropped = 0 def on_updated(self, uri: str) -> None: # Called from the MCP notification handler. MUST NOT block on I/O. if len(self._queue) >= self._max: self.dropped += 1 return self._queue.append(uri) async def drain(self, read_fn, on_content) -> None: # Separate task; owns its own request budget so a pinned subscription # stream cannot starve tools/call or other resources/read. while True: if not self._queue: await asyncio.sleep(0.01) continue batch = [self._queue.popleft() for _ in range(min(self._batch, len(self._queue)))] try: for uri in batch: content = await asyncio.wait_for(read_fn(uri), timeout=5.0) await on_content(uri, content) except asyncio.TimeoutError as e: # Do not retry inline — that re-creates the cyclic dependency. raise SubscriptionStarvationError( f"drain of {self.uri} timed out; connection window likely pinned " f"by undrained notifications (dropped={self.dropped})" ) from e ```