{"record":{"id":"3a9d5c62bc38f4a9","repo":"CoplayDev/unity-mcp","slug":"connection-closed-before-receiving-data","errorCode":null,"errorMessage":"Connection closed before receiving data","messagePattern":"Connection closed before receiving data","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Server/src/transport/legacy/unity_connection.py","lineNumber":220,"sourceCode":"                        f\"Received framed response ({len(payload)} bytes)\")\n                    return payload\n            except socket.timeout as exc:\n                logger.warning(\"Socket timeout during framed receive\")\n                raise TimeoutError(\"Timeout receiving Unity response\") from exc\n            except TimeoutError:\n                raise\n            except Exception as exc:\n                logger.error(f\"Error during framed receive: {exc}\")\n                raise\n\n        chunks = []\n        # Respect the socket's currently configured timeout\n        try:\n            while True:\n                chunk = sock.recv(buffer_size)\n                if not chunk:\n                    if not chunks:\n                        raise Exception(\n                            \"Connection closed before receiving data\")\n                    break\n                chunks.append(chunk)\n\n                # Process the data received so far\n                data = b''.join(chunks)\n                decoded_data = data.decode('utf-8')\n\n                # Check if we've received a complete response\n                try:\n                    # Special case for ping-pong\n                    if decoded_data.strip().startswith('{\"status\":\"success\",\"result\":{\"message\":\"pong\"'):\n                        logger.debug(\"Received ping response\")\n                        return data\n\n                    # Handle escaped quotes in the content\n                    if '\"content\":' in decoded_data:\n                        # Find the content field and its value","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/Server/src/transport/legacy/unity_connection.py#L202-L238","documentation":"Raised in the legacy (non-framing) receive loop (unity_connection.py:220): the very first sock.recv() returned empty (TCP EOF) and no chunks had been collected yet, so the peer closed before sending any data.","triggerScenarios":"receive_full_response in legacy mode (config.require_framing=False) when Unity closes the socket before writing a response — domain reload, editor quit, or connecting to a port whose listener immediately drops the connection.","commonSituations":"Same as the framed EOF (error 182) but in legacy interop mode; also seen when the port is held by a process that accepts then closes.","solutions":["Ensure Unity and the MCPForUnity bridge are still running and responsive.","Prefer framing mode (default) where possible — it is more robust to mid-stream drops.","Let send_command retry after reconnecting."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"# Check the bridge is registered before relying on a legacy connection\nfrom pathlib import Path\n\ndef bridge_registered() -> bool:\n    return bool(list(Path.home().joinpath('.unity-mcp').glob('unity-mcp-status-*.json')))","typeGuard":"def is_legacy_eof(e: BaseException) -> bool:\n    return (isinstance(e, Exception)\n            and 'Connection closed before receiving data' in str(e))","tryCatchPattern":"try:\n    resp = conn.send_command(cmd, params)\nexcept Exception as e:\n    if 'Connection closed before receiving data' in str(e):\n        conn.disconnect()\n        resp = conn.send_command(cmd, params)\n    else:\n        raise","preventionTips":["Prefer framing mode (default) over legacy for robustness.","Keep Unity running for the full command duration.","Reconnect after EOF rather than reusing the closed socket."],"tags":["connection","legacy","eof","socket"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}