go-delve/delve · error

invalid process ID: %d

Error message

invalid process ID: %d

What it means

When creating a debugger in attach mode, a negative AttachPid is invalid (0 means 'not attaching'). New() validates this before attempting any process operation and rejects the configuration.

Source

Thrown at service/debugger/debugger.go:177

	DisableASLR bool

	RrOnProcessPid int
	RrDelOnDetach  bool
}

// New creates a new Debugger. ProcessArgs specify the commandline arguments for the
// new process.
func New(config *Config, processArgs []string) (*Debugger, error) {
	logger := logflags.DebuggerLogger()
	d := &Debugger{
		config:      config,
		processArgs: processArgs,
		log:         logger,
	}

	// Validate AttachPid if specified
	if d.config.AttachPid != 0 && d.config.AttachPid < 0 {
		return nil, fmt.Errorf("invalid process ID: %d", d.config.AttachPid)
	}

	// Create the process by either attaching or launching.
	switch {
	case d.config.AttachPid > 0 || d.config.AttachWaitFor != "":
		d.log.Infof("attaching to pid %d", d.config.AttachPid)
		path := ""
		if len(d.processArgs) > 0 {
			path = d.processArgs[0]
		}
		var waitFor *proc.WaitFor
		if d.config.AttachWaitFor != "" {
			waitFor = &proc.WaitFor{
				Name:     d.config.AttachWaitFor,
				Interval: time.Duration(d.config.AttachWaitForInterval * float64(time.Millisecond)),
				Duration: time.Duration(d.config.AttachWaitForDuration * float64(time.Millisecond)),
			}
		}

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Pass a real positive PID from `ps` or `pgrep`
  2. Check the script/variable feeding the PID for empty/invalid values
  3. Leave AttachPid at 0 (unset) when launching instead of attaching
  4. Guard the value before calling New/debugger creation

Example fix

// before
cfg.AttachPid = pid // pid may be -1
// after
if pid <= 0 { return fmt.Errorf("need a positive pid, got %d", pid) }
cfg.AttachPid = pid
Defensive patterns

Strategy: validation

Validate before calling

func validateAttachPid(pid int) error { if pid < 0 { return fmt.Errorf("invalid process ID: %d", pid) }; return nil }

Type guard

func isAttachablePid(pid int) bool { return pid > 0 }

Try / catch

if _, err := debugger.New(config, logger); err != nil { if strings.Contains(err.Error(), "invalid process ID") { /* resolve a real pid and retry */ } return err }

Prevention

When it happens

Trigger: dlv attach with a negative PID, or programmatic construction of service.Config with AttachPid set to a negative value.

Common situations: Scripts passing shell variables that are empty or negative into attach commands; arithmetic producing negative PIDs; UI clients sending -1 as a placeholder.

Related errors


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