{"record":{"id":"c1a52ea627039389","repo":"NationalSecurityAgency/ghidra","slug":"already-connected-c1a52e","errorCode":null,"errorMessage":"Already connected","messagePattern":"Already connected","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Debugger-agent-x64dbg/src/main/py/src/ghidraxdbg/commands.py","lineNumber":123,"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 != 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 != 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":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger-agent-x64dbg/src/main/py/src/ghidraxdbg/commands.py#L105-L141","documentation":"Raised by State.require_no_client when STATE.client is not None (already connected). This guard prevents duplicate connections; ghidra_trace_connect and ghidra_trace_listen both call require_no_client to ensure a connection does not already exist before opening a new one.","triggerScenarios":"ghidra_trace_connect or ghidra_trace_listen is called when STATE.client already holds an active Client. The require_no_client guard fires because a connection already exists.","commonSituations":"User ran 'ghidra trace connect' twice without disconnecting in between. A previous connection was not properly reset (e.g. the script re-ran without cleanup). The user switched from connect to listen mode without disconnecting first.","solutions":["Call 'ghidra_trace_disconnect' before establishing a new connection.","Check STATE.client is None before calling ghidra_trace_connect or ghidra_trace_listen.","Add STATE.reset_client() in error recovery paths to clean up stale connections."],"exampleFix":"# before: connect while already connected\nghidra_trace_connect('127.0.0.1:12345')  # first connection\nghidra_trace_connect('127.0.0.1:9999')   # Already connected\n\n# after: disconnect first\nif STATE.client is not None:\n    ghidra_trace_disconnect()\nghidra_trace_connect('127.0.0.1:9999')","handlingStrategy":"validation","validationCode":"if STATE.client is not None:\n    raise RuntimeError(\"Already connected; run 'ghidra trace disconnect' first\")","typeGuard":"def is_not_connected() -> bool:\n    return STATE.client is None","tryCatchPattern":"try:\n    ghidra_trace_connect(address)\nexcept RuntimeError:\n    if 'Already connected' in str(e):\n        ghidra_trace_disconnect()\n        ghidra_trace_connect(address)\n    else:\n        raise","preventionTips":["Call 'ghidra trace disconnect' before establishing a new connection.","Check STATE.client is None before calling ghidra_trace_connect or ghidra_trace_listen.","Use STATE.reset_client() in cleanup paths to clear stale connections."],"tags":["x64dbg","debugger-agent","state-machine","runtimeerror","connection","duplicate"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}