Skip to content

Add a ProcessManager interface for managing processes by pid file #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
119 changes: 119 additions & 0 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package process
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No copyright/license info?


import (
"fmt"
"os"
"strconv"
"strings"

"github.com/shirou/gopsutil/v4/process"
)

type Manager interface {
ReadPidFile() (int, error)
Name() string
PidFilePath() string
Exists() bool
Terminate() error
Kill() error
FindProcess() (*os.Process, error)
WritePidFile(pid int) error
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Also this seem to be the Process interface. Calling it Manager is confusing.


type Process struct {
name string
pidFilePath string
executablePath string
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This nice, but do we really need private fields and accessors? We can simplify by making all fields public and remove the accessors.


func New(name, pidFilePath, executablePath string) (*Process, error) {
return &Process{name: name, pidFilePath: pidFilePath, executablePath: executablePath}, nil
}

func (p *Process) Name() string {
return p.name
}

func (p *Process) PidFilePath() string {
return p.pidFilePath
}

func (p *Process) ExecutablePath() string {
return p.executablePath
}

func (p *Process) ReadPidFile() (int, error) {
data, err := os.ReadFile(p.PidFilePath())
if err != nil {
return -1, err
}
pidStr := strings.TrimSpace(string(data))
pid, err := strconv.Atoi(pidStr)
if err != nil {
return -1, fmt.Errorf("invalid pid file: %v", err)
}
return pid, nil
}

func (p *Process) FindProcess() (*process.Process, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to be public or it an be a private helper? Exposing the underlying process package in this packages means we can never change the implementation, for example using another package to validate the process executable path.

pid, err := p.ReadPidFile()
if err != nil {
return nil, fmt.Errorf("cannot find process: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This underling error may be:

  • pidfile does not exist
  • invalid pid file

The caller can use errors.Is(err, os.ErrNotExists) to detect the error.

}

exists, err := process.PidExists(int32(pid))

Check failure on line 64 in pkg/process/process.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion int -> int32 (gosec)
if err != nil {
return nil, err
}
if !exists {
return nil, fmt.Errorf("process not found")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be hard to tell if the process does not exist when we return this error. We need to return which is easy to check like os.ErrNotExist for this special case.


proc, err := process.NewProcess(int32(pid))

Check failure on line 72 in pkg/process/process.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion int -> int32 (gosec)
if err != nil {
return nil, fmt.Errorf("cannot find process: %v", err)
}
if proc == nil {
return nil, fmt.Errorf("process not found")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check if the process exists with process.PidExists(int32(pid)) if we get the same information by creating a process with process.NewProcess(int32(pid))?

Check how PidExists is implemented, I guess it create a process internally.

name, err := proc.Name()
if err != nil {
return nil, fmt.Errorf("cannot find process name: %v", err)
}
if name != p.Name() {
return nil, fmt.Errorf("pid %d is stale, and is being used by %s", pid, name)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pid is stale. How the user can tell that it can delete the pidfile? We need an error that the user can check, meaning that there is no such process matching name, pid and executable path.

exe, err := proc.Exe()
if err != nil {
return nil, fmt.Errorf("cannot find process exe: %v", err)
}
if exe != p.ExecutablePath() {
return nil, fmt.Errorf("pid %d is stale, and is being used by %s", pid, exe)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue here.

}
return proc, nil
}

func (p *Process) Exists() bool {
proc, err := p.FindProcess()
return err == nil && proc != nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be wrong - maybe the process exists but we don't have permissions to get the details?

In minikube we return (bool, error) so it is easy to tell if the process exists or we cannot tell.
https://github.com/kubernetes/minikube/blob/89b97e001600434322b135de9631570fd24bad22/pkg/minikube/process/process.go#L53

}

func (p *Process) Terminate() error {
proc, err := p.FindProcess()
if err != nil {
return fmt.Errorf("cannot find process: %v", err)
}
return proc.Terminate()
}

func (p *Process) Kill() error {
proc, err := p.FindProcess()
if err != nil {
return fmt.Errorf("cannot find process: %v", err)
}
return proc.Kill()
}

func (p *Process) WritePidFile(pid int) error {
return os.WriteFile(p.pidFilePath, []byte(strconv.Itoa(pid)), 0600)
}
99 changes: 99 additions & 0 deletions pkg/process/process_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package process
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we need only public names, so we can use process_test package. This will be better tests since the test code will be the same as real code using this package.


import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
dummyProcessName = "sleep"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which platforms are supported? I don't see any build tags in this file.

dummyProcessArgs = "60"
)

var (
dummyProcess *exec.Cmd
managedProcess *Process
pidFilePath = filepath.Join(os.TempDir(), "pid")
)

func startDummyProcess() error {
dummyProcess = exec.Command(dummyProcessName, dummyProcessArgs)
err := dummyProcess.Start()
if err != nil {
return err
}
return nil
}

func TestMain(m *testing.M) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need TestMain?

Starting a tiny process is very quick, we don't need to start a dummy process before all tests.

Check minikube process package for similar tests:
https://github.com/kubernetes/minikube/blob/master/pkg/minikube/process/process_test.go

If you want to use the same process for multiple tests you can use sub tests. The parent tests can start the process, run the sub tests, and clean up.

err := startDummyProcess()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to start process:", err)
os.Exit(1)
}

managedProcess, err = New(dummyProcessName, pidFilePath, dummyProcess.Path)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to create process:", err)
os.Exit(1)
}
err = managedProcess.WritePidFile(dummyProcess.Process.Pid)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
exitCode := m.Run()
if dummyProcess.Process != nil {
_ = dummyProcess.Process.Kill()
}

os.Exit(exitCode)
}

func TestProcess_Name(t *testing.T) {
assert.Equal(t, dummyProcessName, managedProcess.Name())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test the Name() accessor which does not really need any testing.

}

func TestProcess_FindProcess(t *testing.T) {
foundProcess, err := managedProcess.FindProcess()
assert.NoError(t, err)
assert.NotNil(t, foundProcess)
assert.Equal(t, dummyProcess.Process.Pid, int(foundProcess.Pid))

assert.True(t, managedProcess.Exists())
}

func TestProcess_KillProcess(t *testing.T) {
err := managedProcess.Kill()
assert.NoError(t, err)
assert.False(t, managedProcess.Exists())

// Try to kill the non-existent process
// This should result in an error
err = managedProcess.Kill()
assert.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kill will be easier to use if it is idempotent. Trying to kill a process that does not exist is not an error from the caller point of view.

}

func TestProcess_FindProcess_InvalidPidFile(t *testing.T) {
tmpfile, err := os.CreateTemp("", "invalid_pid")
require.NoError(t, err)
defer os.Remove(tmpfile.Name())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use t.TempDir()


// Write non-numeric content into the file to mimic an invalid pid
_, err = tmpfile.WriteString("non-numeric")
require.NoError(t, err)
tmpfile.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why open a temporary file and close it manually instead os.WriteFile()?

If the issue is using a temporary file with random name, the solution is t.TempDir().


invalidProcess, err := New("invalid-process", tmpfile.Name(), "invalid-path")
assert.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking for error that we never return, remove the error. New cannot fail.


foundProcess, err := invalidProcess.FindProcess()
assert.Error(t, err)
assert.Nil(t, foundProcess)
}
Loading