# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-stall-on-http-native-mcp-transport-when-sse-multiplexing-backpressure-outruns-the-client-read-loop.md # Remnant Warrant: https://corpusfetch.com/c/why-does-an-instance-stall-on-http-native-mcp-transport-when-sse-multiplexing-backpressure-outruns-the-client-read-loop.warrant.json # Markdown SHA-256: 5257a1af765a6e1c828c3d34c6d2bd631ff9442b14c6b17bfd48f0f70b7fa03e # Code AST SHA-256: 4326eeb14d66469bc28a3c1fe803d93f943dda0ca5b131a6369dc08405289d72 # Verified Classes: TransportBackpressureStall, Frame, SharedChannelReader # Defensive Guards: 3 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) import asyncio from dataclasses import dataclass class TransportBackpressureStall(RuntimeError): """Raised when an awaited POST response is gated behind an unbounded SSE backlog on a shared ordered channel.""" pass @dataclass class Frame: kind: str # "response" | "notification" request_id: str | None payload: bytes class SharedChannelReader: """ Models the failure: one ordered reader serves both the response body and the SSE stream. Response resolution depends on SSE drain progress. """ def __init__(self, frames: list[Frame], dispatch_cost_per_frame: float = 0.001): self._frames = frames self._dispatch_cost = dispatch_cost_per_frame self._cursor = 0 async def read_until_response(self, request_id: str, max_frames: int) -> Frame: while self._cursor < len(self._frames): if self._cursor > max_frames: raise TransportBackpressureStall( f"response for {request_id} still queued behind " f"{self._cursor} stream frames on shared channel; " f"backlog outran the read loop" ) frame = self._frames[self._cursor] self._cursor += 1 await asyncio.sleep(self._dispatch_cost) # per-frame dispatch if frame.kind == "response" and frame.request_id == request_id: return frame raise TransportBackpressureStall(f"response for {request_id} never observed") def build_chatty_stream(request_id: str, n_progress: int) -> list[Frame]: frames = [Frame("notification", None, b"progress") for _ in range(n_progress)] frames.append(Frame("response", request_id, b'{"result": "ok"}')) return frames async def invoke_tool_shared_channel(request_id: str, n_progress: int) -> Frame: reader = SharedChannelReader(build_chatty_stream(request_id, n_progress)) # Guardrail: bound how many frames the response may be delayed behind. max_frames = 1000 return await reader.read_until_response(request_id, max_frames) async def main() -> None: # Healthy: server quiet, response arrives promptly. await invoke_tool_shared_channel("call-1", n_progress=10) # Pathological: chatty server on a shared HTTP/1.1 channel. try: await invoke_tool_shared_channel("call-2", n_progress=50_000) except TransportBackpressureStall as e: raise TransportBackpressureStall( f"demultiplex required: {e}" ) from e if __name__ == "__main__": asyncio.run(main())