Skip to content
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

psgo: handle ESRCH as ENOENT #155

Merged
merged 1 commit into from
May 8, 2024
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
5 changes: 4 additions & 1 deletion internal/proc/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package proc

import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"

"golang.org/x/sys/unix"
)

// ParseAttrCurrent returns the contents of /proc/$pid/attr/current of "?" if
Expand All @@ -27,7 +30,7 @@ func ParseAttrCurrent(pid string) (string, error) {
data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%s/attr/current", pid))
if err != nil {
_, err = os.Stat(fmt.Sprintf("/proc/%s", pid))
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) || errors.Is(err, unix.ESRCH) {
// PID doesn't exist
return "", err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/containers/psgo/internal/host"
"github.com/containers/psgo/internal/proc"
"github.com/opencontainers/runc/libcontainer/user"
"golang.org/x/sys/unix"
)

// Process includes process-related from the /proc FS.
Expand Down Expand Up @@ -108,7 +109,7 @@ func FromPIDs(pids []string, joinUserNS bool) ([]*Process, error) {
for _, pid := range pids {
p, err := New(pid, joinUserNS)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if errors.Is(err, os.ErrNotExist) || errors.Is(err, unix.ESRCH) {
// proc parsing is racy
// Let's ignore "does not exist" errors
continue
Expand Down
4 changes: 2 additions & 2 deletions psgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func JoinNamespaceAndProcessInfoByPidsWithOptions(pids []string, descriptors []s
for _, pid := range pids {
ns, err := proc.ParsePIDNamespace(pid)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if errors.Is(err, os.ErrNotExist) || errors.Is(err, unix.ESRCH) {
// catch race conditions
continue
}
Expand All @@ -492,7 +492,7 @@ func JoinNamespaceAndProcessInfoByPidsWithOptions(pids []string, descriptors []s
data := [][]string{}
for i, pid := range pidList {
pidData, err := JoinNamespaceAndProcessInfoWithOptions(pid, descriptors, options)
if errors.Is(err, os.ErrNotExist) {
if errors.Is(err, os.ErrNotExist) || errors.Is(err, unix.ESRCH) {
// catch race conditions
continue
}
Expand Down
Loading