go-delve/delve · error

could not attach to pid

Error message

could not attach to pid %d: already being debugged by pid %d

What it means

The target process is already being traced by another debugger: /proc/<pid>/status shows a non-zero TracerPid. A process can only have one ptrace tracer at a time, so the attach is rejected with the tracing debugger's pid.

Solutions

  1. Detach or close the other debugger session (its pid is in the error message)
  2. Kill the other tracer process: kill <tracerPid>
  3. Check with: cat /proc/<pid>/status | grep TracerPid before attaching
  4. Retry attach once the TracerPid becomes 0

Example fix

// before
dlv attach 12345 // already traced by pid 999
// after (shell)
kill 999
dlv attach 12345
Defensive patterns

Strategy: validation

Validate before calling

out, _ := os.ReadFile(fmt.Sprintf("/proc/%d/status", pid))
alreadyTraced := strings.Contains(string(out), "TracerPid:\t0") == false

Try / catch

err := debugger.Attach(pid, []string{}, nil)
if err != nil && strings.Contains(err.Error(), "already being debugged") {
    // detach/kill the tracer pid parsed from the message, then retry once
}

Prevention

When it happens

Trigger: 'dlv attach <pid>' while another delve/gdb/strace session already has the process attached (TracerPid: <tpid> in /proc/<pid>/status).

Common situations: Two developers attaching to the same dev server; a forgotten strace or previous delve session left attached; IDE debug session still open holding the process.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/be2ccfc8361af61a. Report an issue: GitHub.

Appendix: source

Thrown at service/debugger/debugger_linux.go:52

			}
			if fi.Sys().(*syscall.Stat_t).Uid != uint32(os.Getuid()) {
				return fmt.Errorf("Could not attach to pid %d: current user does not own the process", pid)
			}

			// check if the process is already being traced
			statusfh, err := os.Open(fmt.Sprintf("/proc/%d/status", pid))
			if err != nil {
				return fallbackerr
			}
			defer statusfh.Close()
			scan := bufio.NewScanner(statusfh)
			const tracerPidPrefix = "TracerPid:"
			for scan.Scan() {
				line := scan.Text()
				if strings.HasPrefix(line, tracerPidPrefix) {
					tpid, _ := strconv.Atoi(strings.TrimSpace(line[len(tracerPidPrefix):]))
					if tpid != 0 {
						return fmt.Errorf("could not attach to pid %d: already being debugged by pid %d", pid, tpid)
					}
				}
			}
		}
	}
	return fallbackerr
}

View on GitHub (pinned to a23773e6c3)