{"record":{"id":"59a5bbec1c4ec25c","repo":"go-delve/delve","slug":"error-while-waiting-after-adding-process-d-s","errorCode":null,"errorMessage":"error while waiting after adding process: %d %s","messagePattern":"error while waiting after adding process: (.+?) (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/native/proc_freebsd.go","lineNumber":243,"sourceCode":"}\n\n// Used by RequestManualStop\nfunc (dbp *nativeProcess) requestManualStop() (err error) {\n\treturn sys.Kill(dbp.pid, sys.SIGSTOP)\n}\n\n// Attach to a newly created thread, and store that thread in our list of\n// known threads.\nfunc (dbp *nativeProcess) addThread(tid int, attach bool) (*nativeThread, error) {\n\tif thread, ok := dbp.threads[tid]; ok {\n\t\treturn thread, nil\n\t}\n\n\tvar err error\n\tdbp.execPtraceFunc(func() { err = sys.PtraceLwpEvents(dbp.pid, 1) })\n\tif err == syscall.ESRCH {\n\t\tif _, _, err = dbp.wait(dbp.pid, 0); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error while waiting after adding process: %d %s\", dbp.pid, err)\n\t\t}\n\t}\n\n\tdbp.threads[tid] = &nativeThread{\n\t\tID:  tid,\n\t\tdbp: dbp,\n\t\tos:  new(osSpecificDetails),\n\t}\n\n\tif dbp.memthread == nil {\n\t\tdbp.memthread = dbp.threads[tid]\n\t}\n\n\treturn dbp.threads[tid], nil\n}\n\n// Used by initialize\nfunc (dbp *nativeProcess) updateThreadList() error {","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/native/proc_freebsd.go#L225-L261","documentation":"When registering a new LWP on FreeBSD, addThread enables PTRACE_LWP_EVENTS; if that ptrace returns ESRCH (pid vanished), Delve performs a compensating wait(dbp.pid, 0) to reap the event. If that wait also fails, it returns \"error while waiting after adding process: <pid> <err>\" — the process state could not be reconciled while adding the thread.","triggerScenarios":"Thread creation/attach flow on FreeBSD where sys.PtraceLwpEvents fails with ESRCH and the follow-up wait in addThread returns a non-nil error (child already dead, wait event already consumed, or ECHILD).","commonSituations":"Debuggee exits while Delve is still discovering threads (short-lived programs); another tracer consumed the wait status; attach to a dying process; wait events consumed by a prior uncoordinated wait.","solutions":["Check whether the debuggee exited concurrently (`ps -p <pid>`); if so, restart the session.","Ensure no other tracer (or nested dlv) is attached — only one wait can reap events.","Retry the operation; EINTR/transient wait failures may clear.","Update Delve if this reproduces reliably on multi-threaded programs (LWP event handling bugs have been fixed historically)."],"exampleFix":"// before\ndlv attach $(pgrep -n shortlived)   // process exits during attach\n// after\n./shortlived &          # keep it alive, or add a sleep in the target\nsleep 1\ndlv attach $(pgrep shortlived)","handlingStrategy":"try-catch","validationCode":"// Ensure no other tracer is attached and the pid is alive:\nif _, err := os.FindProcess(pid); err != nil {\n    return err\n}\nif _, err := os.Stat(fmt.Sprintf(\"/proc/%d\", pid)); !os.IsNotExist(err) && !isFreeBSD {\n    // (FreeBSD uses procstat) verify with: procstat -b <pid>\n}","typeGuard":null,"tryCatchPattern":"thr, err := dbp.addThread(tid, true)\nif err != nil && strings.Contains(err.Error(), \"error while waiting after adding process\") {\n    // process likely exited mid-attach; verify liveness and restart the session\n}","preventionTips":["Attach only to processes expected to stay alive (add sleep/liveness in tests).","Ensure exactly one tracer is attached to the debuggee.","Avoid coordinating raw waits on the traced pid outside Delve.","Keep Delve updated for FreeBSD LWP-event handling fixes."],"tags":["freebsd","threads","ptrace","wait"],"backgroundTag":"ptrace-wait-failed","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}