Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkgs/mcpx/internal/pool/keypidalive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package pool

import (
"errors"
"fmt"
"os"
"syscall"
"testing"
)

// #2336: keyPIDAlive treated an EPERM pid as dead, so a live foreign-owned
// caller's pid-scoped instances were reaped, never replaced, and skipped on
// restart. The probe seam makes every signal outcome deterministic.
func TestKeyPIDAliveProbed(t *testing.T) {
cases := []struct {
name string
key string
probe func(int) error
want bool
}{
{"nil error is alive", "pid:4242", func(int) error { return nil }, true},
{"EPERM is alive", "pid:4242", func(int) error { return syscall.EPERM }, true},
{"wrapped EPERM is alive", "pid:4242", func(int) error { return fmt.Errorf("signal 0: %w", syscall.EPERM) }, true},
{"ESRCH is dead", "pid:4242", func(int) error { return syscall.ESRCH }, false},
{"other error is dead", "pid:4242", func(int) error { return errors.New("boom") }, false},
{"unparseable key is alive", "global", func(int) error { return syscall.ESRCH }, true},
{"unparseable pid is alive", "pid:abc", func(int) error { return syscall.ESRCH }, true},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := keyPIDAliveProbed(c.key, c.probe); got != c.want {
t.Errorf("keyPIDAliveProbed(%q) = %v, want %v", c.key, got, c.want)
}
})
}
}

// The real probe against this test process itself: signal 0 succeeds for a
// live same-owner pid, so the un-injected path reports alive.
func TestKeyPIDAliveSelf(t *testing.T) {
key := fmt.Sprintf("pid:%d", os.Getpid())
if !keyPIDAlive(key) {
t.Errorf("keyPIDAlive(%q) = false, want true for the running test process", key)
}
}
26 changes: 21 additions & 5 deletions pkgs/mcpx/internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,18 +1455,34 @@ func (p *Pool) ReapIdle(now time.Time) int {
return len(stop)
}

// signalProcess probes pid with signal 0: it reports whether the process
// exists without affecting it. It is the seam tests inject through
// keyPIDAliveProbed so the EPERM cases are deterministic (#2336).
func signalProcess(pid int) error {
proc, err := os.FindProcess(pid)
if err != nil {
return err
}
return proc.Signal(syscall.Signal(0))
}

// keyPIDAlive reports whether the process a pid-scoped key names still exists.
// An unparseable key is treated as alive so a bug here cannot kill instances.
func keyPIDAlive(key string) bool {
return keyPIDAliveProbed(key, signalProcess)
}

// keyPIDAliveProbed is keyPIDAlive with the signal-0 probe injected. A probe
// error of EPERM means the process exists but is owned by someone else: it is
// alive, exactly as daemon.ProcessAlive already treats it (#2336). Any other
// probe error (ESRCH above all) means the process is gone.
func keyPIDAliveProbed(key string, probe func(int) error) bool {
pid, ok := config.PIDOf(key)
if !ok {
return true
}
proc, err := os.FindProcess(pid)
if err != nil {
return false
}
return proc.Signal(syscall.Signal(0)) == nil
err := probe(pid)
return err == nil || errors.Is(err, syscall.EPERM)
}

// InstanceStatus is a diagnostic snapshot.
Expand Down
Loading