{"record":{"id":"d8ebb170415ef224","repo":"microsoft/aspire","slug":"not-connected-to-apphost-pythonmodulebuilder","errorCode":null,"errorMessage":"Not connected to AppHost","messagePattern":"Not connected to AppHost","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs","lineNumber":1119,"sourceCode":"                    else:\n                        _logger.info(\"-> %s\", message)\n                message_str = json.dumps(message, cls=_AspireJSONEncoder)\n                message_bytes = message_str.encode(\"utf-8\")\n                content_length = len(message_bytes)\n\n                # Send with HTTP-style headers (HeaderDelimitedMessageHandler format)\n                header = f\"Content-Length: {content_length}\\r\\n\\r\\n\"\n                header_bytes = header.encode(\"utf-8\")\n                with self._lock:\n                    typing.cast(_PipeSocket, self._socket).sendall(header_bytes + message_bytes)\n\n            def _check_connection(self) -> None:\n                '''Check if connected and raise stored connection error if present.'''\n                with self._lock:\n                    if self._connection_error:\n                        raise self._connection_error\n                    if not self._connected:\n                        raise RuntimeError(\"Not connected to AppHost\")\n\n            def ping(self) -> str:\n                '''Ping the server'''\n                self._check_connection()\n                return self._send_request(\"ping\")\n\n            def authenticate(self, token: str) -> None:\n                '''Authenticate to the AppHost server with a session token.'''\n                if not bool(self._send_request(\"authenticate\", token)):\n                    raise RuntimeError(\"Failed to authenticate to the AppHost server.\")\n\n            def invoke_capability(\n                self,\n                capability_id: str,\n                args: dict[str, typing.Any] | None = None,\n                kwargs: typing.Mapping[str, typing.Any] | None = None\n            ) -> typing.Any:\n                '''","sourceCodeStart":1101,"sourceCodeEnd":1137,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs#L1101-L1137","documentation":"The client's _check_connection guard raises RuntimeError('Not connected to AppHost') when an API method (ping, authenticate, invoke_capability, etc.) is called before a connection to the AppHost server has been established, and no stored connection error exists. The client tracks a _connected flag under its lock; calling methods on a fresh or disconnected client hits this path.","triggerScenarios":"Calling ping(), authenticate(), or invoke_capability() on a client that was constructed but whose connect() was never called, failed silently, or whose connection was closed/disconnected beforehand.","commonSituations":"Instantiating the client and immediately calling methods; a prior connection drop set _connected=False; tests constructing the client without a running AppHost; calling methods after close/disconnect.","solutions":["Call connect() (or start the AppHost and connect) before issuing any API calls.","Check the AppHost is actually running and its endpoint/port is reachable.","Recreate the client after a disconnect instead of reusing the stale instance.","Wrap usage in a helper that asserts client connectivity before calls."],"exampleFix":"// before\nclient = AspireClient(host, port)\nclient.ping()\n// after\nclient = AspireClient(host, port)\nclient.connect()\nclient.ping()","handlingStrategy":"validation","validationCode":"def ensure_connected(client):\n    if not getattr(client, '_connected', False):\n        raise RuntimeError('call connect() before using the client')\n    return client","typeGuard":"def is_ready(client) -> bool:\n    return bool(getattr(client, '_connected', False))","tryCatchPattern":null,"preventionTips":["Always call connect() immediately after constructing the client","Recreate the client after disconnect instead of reusing it","Assert connectivity in shared test fixtures before issuing calls"],"tags":["python","connection","lifecycle","state"],"backgroundTag":"connection-refused","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}