{"record":{"id":"5750a91f3a24b8c5","repo":"NationalSecurityAgency/ghidra","slug":"already-connected-5750a9","errorCode":null,"errorMessage":"Already connected","messagePattern":"Already connected","errorType":"validation","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/commands.py","lineNumber":103,"sourceCode":"    def require_rm(self) -> arch.DefaultRegisterMapper:\n        if self.register_mapper is None:\n            raise RuntimeError(\"No register mapper\")\n        return self.register_mapper\n\n\nclass State(object):\n\n    def __init__(self) -> None:\n        self.reset_client()\n\n    def require_client(self) -> Client:\n        if self.client is None:\n            raise RuntimeError(\"Not connected\")\n        return self.client\n\n    def require_no_client(self) -> None:\n        if self.client is not None:\n            raise RuntimeError(\"Already connected\")\n\n    def reset_client(self) -> None:\n        self.client: Optional[Client] = None\n        self.reset_trace()\n\n    def require_trace(self) -> Trace[Extra]:\n        if self.trace is None:\n            raise RuntimeError(\"No trace active\")\n        return self.trace\n\n    def require_no_trace(self) -> None:\n        if self.trace is not None:\n            raise RuntimeError(\"Trace already started\")\n\n    def reset_trace(self) -> None:\n        self.trace: Optional[Trace[Extra]] = None\n        util.set_convenience_variable('_ghidra_tracing', \"false\")\n        self.reset_tx()","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/commands.py#L85-L121","documentation":"Raised by State.require_no_client in the Ghidra LLDB agent when STATE.client is already set — i.e. a connection to Ghidra already exists. Commands that start a fresh connection (the connect command itself, at commands.py:278 and :329) call require_no_client to prevent stacking two connections; an existing client is rejected with RuntimeError.","triggerScenarios":"Running the connect command (ghidra_trace_connect) a second time without first disconnecting. The connect command asserts require_no_client before opening a new Client.","commonSituations":"User runs 'ghidra_trace_connect' twice; previous connection was not cleaned up (disconnect not run); script reconnects without teardown.","solutions":["Run the disconnect command (ghidra_trace_disconnect) before connecting again.","Guard scripts: if STATE.client is not None, disconnect first.","Catch RuntimeError in command wrappers and hint 'already connected; run ghidra_trace_disconnect first'.","Ensure reset_client/disconnect is called on script exit or on connection error."],"exampleFix":"// before\nSTATE.require_no_client()\nclient = Client(host, port)\n// after\nif STATE.client is not None:\n    STATE.require_client().close()\n    STATE.reset_client()\nSTATE.require_no_client()\nclient = Client(host, port)","handlingStrategy":"validation","validationCode":"if STATE.client is not None:\n    STATE.require_client().close()\n    STATE.reset_client()\nSTATE.require_no_client()\nclient = Client(host, port)","typeGuard":"def is_disconnected() -> bool:\n    return STATE.client is None","tryCatchPattern":"try:\n    STATE.require_no_client()\nexcept RuntimeError:\n    print(\"Already connected; run ghidra_trace_disconnect first\")\n    return","preventionTips":["disconnect before reconnecting","guard reconnection scripts","call reset_client on connection error or script exit"],"tags":["ghidra","lldb","runtime-error","state-machine","connection"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}