{"record":{"id":"2842b2ec16ddb300","repo":"go-delve/delve","slug":"could-not-connect","errorCode":null,"errorMessage":"could not connect","messagePattern":"could not connect","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/gdbserial/gdbserver.go","lineNumber":296,"sourceCode":"\t}\n\n\treturn p\n}\n\n// Listen waits for a connection from the stub.\nfunc (p *gdbProcess) Listen(listener net.Listener, path, cmdline string, pid int, debugInfoDirs []string, stopReason proc.StopReason) (*proc.TargetGroup, error) {\n\tacceptChan := make(chan net.Conn)\n\n\tgo func() {\n\t\tconn, _ := listener.Accept()\n\t\tacceptChan <- conn\n\t}()\n\n\tselect {\n\tcase conn := <-acceptChan:\n\t\tlistener.Close()\n\t\tif conn == nil {\n\t\t\treturn nil, errors.New(\"could not connect\")\n\t\t}\n\t\treturn p.Connect(conn, path, cmdline, pid, debugInfoDirs, stopReason)\n\tcase status := <-p.waitChan:\n\t\tlistener.Close()\n\t\tif err := checkRosettaExpensive(); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn nil, fmt.Errorf(\"stub exited while waiting for connection: %v\", status)\n\t}\n}\n\n// Dial attempts to connect to the stub.\nfunc (p *gdbProcess) Dial(addr string, path, cmdline string, pid int, debugInfoDirs []string, stopReason proc.StopReason) (*proc.TargetGroup, error) {\n\tfor {\n\t\tconn, err := net.Dial(\"tcp\", addr)\n\t\tif err == nil {\n\t\t\treturn p.Connect(conn, path, cmdline, pid, debugInfoDirs, stopReason)\n\t\t}","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/gdbserial/gdbserver.go#L278-L314","documentation":"Listen() launches the debugger stub (e.g. lldb-server) and waits for either an incoming TCP connection on the accept channel or process exit. If the process died before a connection could be accepted (conn == nil), 'could not connect' is returned.","triggerScenarios":"p.waitChan receives exit (stub crashed/exited) before any connection arrives, so acceptChan delivers nil and Listen returns errors.New(\"could not connect\").","commonSituations":"lldb-server binary missing or wrong architecture, port already used/firewalled, stub crashes on startup (bad DYLD/PATH env), macOS Rosetta problems, or the target binary fails to exec inside the stub.","solutions":["Run delve with --log-output=dewire to see stub stdout/stderr and why it exited.","Verify the target binary and lldb-server match the host architecture (e.g. not running x86 via Rosetta unexpectedly).","Ensure the chosen listen port/host is free and reachable (check with lsof/netstat).","Check that LLDB backend (lldb-server) is installed and on PATH.","Try a different listen address (e.g. 127.0.0.1:0) to rule out networking issues."],"exampleFix":"// before\ndlv connect localhost:4040   // fails: could not connect\n// after\nlsof -i :4040                # find conflicting process\ndlv connect 127.0.0.1:0      # let delve pick a free port","handlingStrategy":"retry","validationCode":"ln, err := net.Listen(\"tcp\", addr)\nif err != nil { return fmt.Errorf(\"port %s unavailable: %w\", addr, err) }\nln.Close()","typeGuard":null,"tryCatchPattern":"conn, err := gdbserial.Listen(addr, ...)\nif err != nil {\n    if strings.Contains(err.Error(), \"could not connect\") {\n        // retry or inspect stub logs\n    }\n}","preventionTips":["Run with --log-output=dewire to capture stub stderr when launches fail.","Confirm lldb-server is installed and matches the binary architecture.","Verify the listen port is free before launching.","Avoid mismatched architectures (x86 vs arm64, Rosetta) between target and stub."],"tags":["gdbserial","lldb","tcp","process-exit"],"backgroundTag":"debugger-stub-connection-failed","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}