{"record":{"id":"5a9a4c3734936abd","repo":"sgl-project/sglang","slug":"connection-closed-while-reading-message-body","errorCode":null,"errorMessage":"Connection closed while reading message body","messagePattern":"Connection closed while reading message body","errorType":"error_code","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/weight_cache/protocol.py","lineNumber":227,"sourceCode":"\ndef send_msg(sock, obj: Any) -> None:\n    \"\"\"Send a length-prefixed pickled message over a socket.\"\"\"\n    data = pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)\n    header = struct.pack(\"!I\", len(data))\n    sock.sendall(header + data)\n\n\ndef recv_msg(sock) -> Any:\n    \"\"\"Receive a length-prefixed pickled message from a socket.\"\"\"\n    header = _recv_exact(sock, 4)\n    if header is None:\n        raise ConnectionError(\"Connection closed while reading message header\")\n    length = struct.unpack(\"!I\", header)[0]\n    if length > MAX_MSG_SIZE:\n        raise ValueError(f\"Message size {length} exceeds {MAX_MSG_SIZE} byte cap\")\n    data = _recv_exact(sock, length)\n    if data is None:\n        raise ConnectionError(\"Connection closed while reading message body\")\n    return safe_pickle_loads(data)\n\n\ndef _recv_exact(sock, n: int) -> Optional[bytes]:\n    \"\"\"Receive exactly n bytes from a socket.\"\"\"\n    buf = bytearray()\n    while len(buf) < n:\n        chunk = sock.recv(n - len(buf))\n        if not chunk:\n            return None\n        buf.extend(chunk)\n    return bytes(buf)\n\n\ndef compute_env_stamp() -> Dict[str, str]:\n    \"\"\"Local environment fingerprint for the IPC weight cache.\n\n    Returns the device compute capability and torch version of the current","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/weight_cache/protocol.py#L209-L245","documentation":"recv_msg read the length prefix but _recv_exact for the body returned None — the peer closed the connection mid-message. The framed message was truncated.","triggerScenarios":"Daemon killed or crashing while sending a fetch-state response; socket reset partway through a large pickled body.","commonSituations":"Daemon OOM-killed during large weight state transfer; network/unix socket closed mid-send.","solutions":["Check daemon liveness and logs; restart it","Ensure daemon has enough memory for the response payload","Retry the fetch after a clean restart"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    msg = recv_msg(sock)\nexcept ConnectionError as e:\n    if 'message body' in str(e): reconnect_and_retry()","preventionTips":["Give the daemon enough memory for large responses","Treat mid-body EOF as 'restart and retry'"],"tags":["protocol","socket","eof","truncated"],"backgroundTag":"connection-reset-mid-message","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}