NationalSecurityAgency/ghidra · error · Exception

Socket closed

Error message

Socket closed

What it means

Raised as a generic Exception by recv_length() when recv_all() returns fewer than 4 bytes for the length-prefix, i.e. the peer closed the socket (recv returned 0) before sending a full 4-byte length word. It signals connection termination, not malformed data.

Source

Thrown at Ghidra/Debug/Debugger-rmi-trace/src/main/py/src/ghidratrace/util.py:53

    send_length(s, size)
    s.sendall(data)


def recv_all(s, size: int) -> bytes:
    buf = b''
    while len(buf) < size:
        part = s.recv(size - len(buf))
        if len(part) == 0:
            return buf
        buf += part
    return buf
    # return s.recv(size, socket.MSG_WAITALL)


def recv_length(s: socket.socket) -> int:
    buf = recv_all(s, 4)
    if len(buf) < 4:
        raise Exception("Socket closed")
    return int.from_bytes(buf, 'big')


def recv_delimited(s: socket.socket, msg: M, dbg_seq: int) -> M:
    size = recv_length(s)
    if size > MAX_MSG_LENGTH:
        raise TraceRmiError("Cannot receive TraceRmi message with excessive message length")
    buf = recv_all(s, size)
    if len(buf) < size:
        raise Exception("Socket closed")
    msg.ParseFromString(buf)
    return msg

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Check that the Ghidra TraceRmi server/target is still running and reachable.
  2. Catch the exception, log the disconnect, and re-establish the connection / re-open the trace.
  3. Inspect server-side logs for the cause of the close.

Example fix

# before
client = Client.connect(s)
... use without guarding ...
# after
try:
    client = Client.connect(s)
except Exception as e:
    if str(e) == 'Socket closed':
        log.warning('TraceRmi peer disconnected')
        reconnect()
    raise
Defensive patterns

Strategy: try-catch

Try / catch

try:
    size = recv_length(s)
except Exception as e:
    if str(e) == 'Socket closed':
        handle_disconnect()
    raise

Prevention

When it happens

Trigger: The Ghidra TraceRmi server crashed or was killed mid-session; the remote debugger process exited; network reset; orderly shutdown by the peer.

Common situations: Remote trace target terminated; Ghidra closed; VPN/SSH tunnel dropped; server hit an exception and tore down the socket.

Related errors


AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14). Data as JSON: /api/errors/9cdaf5f13f5fe63e. Report an issue: GitHub.