Skip to content

Commit

Permalink
Merge pull request #155 from giuseppe/ignore-esrch
Browse files Browse the repository at this point in the history
psgo: handle ESRCH as ENOENT
  • Loading branch information
rhatdan authored May 8, 2024
2 parents 454485d + 9c5d887 commit 1ee6f99
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
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

0 comments on commit 1ee6f99

Please sign in to comment.