# What happens when an instance resumes an MCP SSE stream after a server-side cancellation and the Last-Event-ID cursor lands on a tombstoned request **Date of Record:** 2026 November 22 **Scope:** Applies to MCP servers and clients using the HTTP-native transport with `text/event-stream` responses and `Last-Event-ID` resumption, where the server emits `notifications/cancelled` and tracks per-session request state. Does not apply to stdio transport, nor to stateless HTTP servers that do not persist an event log across reconnects. ## Core Distinction The failure is not "the stream dropped." Stream drops are routine and the resumption protocol is designed for them. The failure is the *semantic mismatch between the transport's replay cursor and the application's request lifecycle*. The SSE `Last-Event-ID` cursor is a transport-level position in an append-only byte log. The MCP request lifecycle (pending → in-flight → cancelled → completed) is an application-level state machine. These two orderings are not the same, and the server is free to append cancellation events to the log at a position *after* the request they cancel was already consumed by a client that has not yet acknowledged the cancellation. When a client reconnects with a `Last-Event-ID` that points into the window between "request dispatched" and "cancellation appended," the server must decide what to replay. If it replays naively from the cursor, it re-emits the original `tools/call` request event to a client that already handled it — or, worse, re-emits the *cancellation* for a request the reconnected client never saw in this connection generation, producing an orphaned cancellation that the client cannot correlate to any outstanding request. The invariant that must hold is: **every replayed event must be interpretable against the client's current per-connection request table, and every cancellation must reference a request the client believes is live.** The transport cursor does not enforce this; only the server's tombstone bookkeeping can. ## What Was Observed A server exposed a `tools/call` that shells out to a long-running subprocess. Client A opened an SSE stream, dispatched request `r-7f3a`, and the server began streaming progress `notifications/progress` events with monotonically increasing SSE `id:` fields. At event id `1042`, the client's TCP connection was reset (mobile network handoff). The server, per its cancellation policy, did not detect the drop immediately and continued the subprocess; at id `1047` it emitted `notifications/cancelled` for `r-7f3a` because an internal idle-timeout watchdog fired (the client had not sent a `ping` since before the drop). Client A reconnected with `Last-Event-ID: 1042`. The server's replay logic walked its event log from 1043 onward and re-emitted events 1043–1047, including the `notifications/cancelled` for `r-7f3a`. But Client A's new connection generation had an empty request table — it had not re-dispatched `r-7f3a` (it assumed the request survived the reconnect, since the transport spec says resumption preserves the session). The client received a cancellation for a request it had no record of. Two divergent outcomes were observed depending on client implementation: 1. **Silent drop:** the client ignored the orphaned cancellation. It then waited indefinitely for the `tools/call` result, which the server had already discarded because it considered `r-7f3a` cancelled. The request hung until the client's own timeout — a 90-second stall on every reconnect that straddled a cancellation. 2. **Over-correction:** the client, seeing a cancellation for an unknown id, treated its *entire* request table as corrupt and failed all outstanding requests on that session, including unrelated in-flight calls `r-8a11` and `r-9c02`. One cancellation poisoned three requests. A second variant surfaced with `id:` field reuse: a server that restarted its event log (common after a deploy) emitted ids starting again at `1001`. A client reconnecting with `Last-Event-ID: 1042` received a `400` from the server's cursor-validation, then — because the client's retry logic treated `400` as retryable — looped reconnecting with the same stale cursor. The session never recovered until manual client restart. ## Condition of Validity This failure requires all of the following to co-occur, which is why it is intermittent and hard to reproduce: - The transport is HTTP-native SSE with `Last-Event-ID` resumption enabled and an event log persisted across connections. - The server independently cancels requests (idle watchdog, client `ping` timeout, capacity eviction) rather than only cancelling on explicit client `notifications/cancelled`. - The cancellation is appended to the event log *after* the request's last delivered event, so the replay window `(Last-Event-ID, head]` contains a cancellation whose target is not in the reconnected client's request table. - The client does not re-establish its request table from a separate durable store on reconnect (i.e., it relies solely on transport replay for continuity). If the server cancels only on explicit client request, the client's cancellation and the transport cursor are causally ordered and the orphan cannot occur. If the client rehydrates its request table from a durable checkpoint keyed by request id (not by SSE id), the orphaned cancellation can be correlated and either acknowledged or ignored safely. If the server does not persist an event log and instead forces a fresh stream on reconnect (no resumption), the failure degrades to a plain dropped request, which is recoverable by retry. ## Decision Boundary The server must not replay a cancellation event to a connection generation that did not witness the corresponding request dispatch. The correct guardrail is a **two-key cursor**: the SSE `Last-Event-ID` plus a `session-generation` token that increments on every new SSE connection. The server maintains, per session, a map from request id to the generation in which it was dispatched. On replay, a `notifications/cancelled` event is emitted only if the reconnecting generation is the same generation that dispatched the target request; otherwise the cancellation is *suppressed* and the request is instead re-announced as `notifications/request-expired` (a terminal, idempotent, non-actionable event) so the client can drop its wait without ambiguity. Symmetrically, the client must treat `Last-Event-ID` as a *transport hint*, not as authoritative request state: on reconnect it should re-assert any request it still believes live via an idempotent `request/status` probe, and treat a `request-expired` as a definitive terminal state distinct from `cancelled`. The boundary is: **replay is safe for progress and partial-result events (they are additive and idempotent) but unsafe for lifecycle-terminal events (cancellation, completion) whose meaning depends on the receiver having observed the corresponding start.** Terminal events require generation matching; additive events do not. ## Operational Effect Without the guardrail, every reconnect that straddles a server-side cancellation produces either a hung request (client waits for a result the server has discarded) or a cascade failure (client invalidates its whole request table on an uncorrelatable cancellation). The hung-request case is the more insidious because it presents as a latency anomaly, not an error: the client's health checks pass, the session is "connected," and the only symptom is a single request that never resolves until a coarser timeout fires. The cascade case presents as a burst of unrelated request failures immediately after a reconnect, which operators frequently misattribute to the reconnect itself rather than to the single orphaned cancellation that triggered the table invalidation. The fix is cheap — a generation counter and a tombstone map of bounded size (pruned when the generation that dispatched a request is retired) — but it must be applied on the server side, because the client cannot distinguish "cancellation for a request I dispatched in a prior generation" from "cancellation for a request I never dispatched" using the SSE cursor alone. Clients that attempt to paper over this by suppressing all cancellations for unknown ids trade the cascade failure for the hung-request failure; there is no client-only fix. ```python import time from dataclasses import dataclass, field from typing import Dict, Optional class OrphanedCancellationError(RuntimeError): """Raised when a cancellation is replayed to a generation that never dispatched the target request, and the server has no tombstone to convert it into a terminal request-expired event.""" @dataclass class RequestTombstone: request_id: str dispatched_generation: int terminal: bool = False @dataclass class SessionEventLog: session_id: str generation: int = 0 events: list = field(default_factory=list) # (sse_id, kind, request_id) tombstones: Dict[str, RequestTombstone] = field(default_factory=dict) def new_connection(self) -> int: self.generation += 1 return self.generation def dispatch(self, request_id: str) -> None: self.tombstones[request_id] = RequestTombstone( request_id=request_id, dispatched_generation=self.generation ) def cancel(self, request_id: str) -> None: ts = self.tombstones.get(request_id) if ts is None: raise OrphanedCancellationError( f"cancel for undispatched request {request_id}" ) ts.terminal = True self.events.append((len(self.events) + 1, "cancelled", request_id)) def replay(self, last_event_id: int) -> list: """Return events safe to replay to the CURRENT generation. Additive events (progress) replay unconditionally. Terminal events (cancelled) replay only if the current generation dispatched the target; otherwise they are rewritten to request-expired, which is idempotent and does not require the client to hold request state. """ out = [] for sse_id, kind, req_id in self.events: if sse_id <= last_event_id: continue if kind == "cancelled": ts = self.tombstones.get(req_id) if ts is None: raise OrphanedCancellationError( f"tombstone missing for {req_id} during replay" ) if ts.dispatched_generation != self.generation: out.append((sse_id, "request-expired", req_id)) continue out.append((sse_id, kind, req_id)) return out def handle_reconnect(log: SessionEventLog, last_event_id: int) -> list: if last_event_id > len(log.events): # stale cursor from a pre-restart log; force fresh stream log.new_connection() return [] log.new_connection() try: return log.replay(last_event_id) except OrphanedCancellationError as exc: # fail closed: never emit a cancellation we cannot correlate raise OrphanedCancellationError( f"session {log.session_id} gen {log.generation}: refusing to " f"replay uncorrelatable terminal event: {exc}" ) from exc ```