Skip to content

Commit

Permalink
Fix race condition with unix process listing
Browse files Browse the repository at this point in the history
  • Loading branch information
builder-pluralstack committed Jul 21, 2017
1 parent 4fdf99a commit 4433868
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package ps

import (
"fmt"
"io"
"os"
"strconv"
)
Expand Down Expand Up @@ -55,41 +54,36 @@ func processes() ([]Process, error) {
defer d.Close()

results := make([]Process, 0, 50)
for {
fis, err := d.Readdir(10)
if err == io.EOF {
break
fis, err := d.Readdir(-1)
if err != nil {
return nil, err
}

for _, fi := range fis {
// We only care about directories, since all pids are dirs
if !fi.IsDir() {
continue
}

// We only care if the name starts with a numeric
name := fi.Name()
if name[0] < '0' || name[0] > '9' {
continue
}

// From this point forward, any errors we just ignore, because
// it might simply be that the process doesn't exist anymore.
pid, err := strconv.ParseInt(name, 10, 0)
if err != nil {
return nil, err
continue
}

for _, fi := range fis {
// We only care about directories, since all pids are dirs
if !fi.IsDir() {
continue
}

// We only care if the name starts with a numeric
name := fi.Name()
if name[0] < '0' || name[0] > '9' {
continue
}

// From this point forward, any errors we just ignore, because
// it might simply be that the process doesn't exist anymore.
pid, err := strconv.ParseInt(name, 10, 0)
if err != nil {
continue
}

p, err := newUnixProcess(int(pid))
if err != nil {
continue
}

results = append(results, p)
p, err := newUnixProcess(int(pid))
if err != nil {
continue
}

results = append(results, p)
}

return results, nil
Expand Down

0 comments on commit 4433868

Please sign in to comment.