{"record":{"id":"ba04749e4ec1e71a","repo":"NationalSecurityAgency/ghidra","slug":"already-connected","errorCode":null,"errorMessage":"Already connected","messagePattern":"Already connected","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"warning","filePath":"Ghidra/Debug/Debugger-agent-dbgeng/src/main/py/src/ghidradbg/commands.py","lineNumber":126,"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":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger-agent-dbgeng/src/main/py/src/ghidradbg/commands.py#L108-L144","documentation":"Thrown by State.require_no_client() when a client connection already exists and a second connection is attempted. This guard prevents establishing multiple simultaneous connections to the trace server. The RuntimeError is raised if STATE.client is not None when require_no_client is called — typically at the start of ghidra_trace_connect.","triggerScenarios":"STATE.require_no_client() is called at the beginning of ghidra_trace_connect, but STATE.client is already set from a previous successful connection. Happens when the user calls ghidra_trace_connect twice without disconnecting first, or when a script reconnects without calling ghidra_trace_disconnect.","commonSituations":"The user runs the connect command again without first disconnecting. A script loops and reconnects without cleanup. The previous connection is stale but was never formally closed.","solutions":["Call ghidra_trace_disconnect() before calling ghidra_trace_connect() again.","Check connection state: call require_client() to see if already connected before connecting.","In scripts, always pair connect/disconnect in try/finally blocks.","If the connection is stale, disconnect and reconnect cleanly."],"exampleFix":"# Before: double connect\n# ghidra_trace_connect('127.0.0.1:12345')\n# ghidra_trace_connect('127.0.0.1:12345')  # throws\n#\n# After:\n# ghidra_trace_disconnect()\n# ghidra_trace_connect('127.0.0.1:12345')","handlingStrategy":"validation","validationCode":"# Before connecting, check if already connected:\nif STATE.client is not None:\n    ghidra_trace_disconnect()\nghidra_trace_connect(address)","typeGuard":"def is_already_connected(state) -> bool:\n    return state.client is not None","tryCatchPattern":"try:\n    ghidra_trace_connect(address)\nexcept RuntimeError as e:\n    if str(e) == 'Already connected':\n        ghidra_trace_disconnect()\n        ghidra_trace_connect(address)\n    else:\n        raise","preventionTips":["Always disconnect before reconnecting to avoid double-connect errors.","Use try/finally blocks to ensure clean disconnect on errors.","Check connection state before calling connect.","Idempotent scripts should disconnect first or check state."],"tags":["debugger-agent","connection","client","duplicate","python"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}