-
Notifications
You must be signed in to change notification settings - Fork 259
reaper: Add a periodic reaper funcion #767
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
Merged
openshift-merge-robot
merged 1 commit into
openshift:master
from
mrunalp:add_periodic_reaper
Jun 8, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package proc | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "k8s.io/klog" | ||
| ) | ||
|
|
||
| // parseProcForZombies parses the current procfs mounted at /proc | ||
| // to find processes in the zombie state. | ||
| func parseProcForZombies() ([]int, error) { | ||
| files, err := ioutil.ReadDir("/proc") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var zombies []int | ||
| for _, file := range files { | ||
| processID, err := strconv.Atoi(file.Name()) | ||
| if err != nil { | ||
| break | ||
| } | ||
| stateFilePath := filepath.Join("/proc", file.Name(), "status") | ||
| fd, err := os.Open(stateFilePath) | ||
| if err != nil { | ||
| klog.Errorf("Failed to open %v for getting process status: %v", stateFilePath, err) | ||
| continue | ||
| } | ||
| defer fd.Close() | ||
| fs := bufio.NewScanner(fd) | ||
| for fs.Scan() { | ||
| line := fs.Text() | ||
| if strings.HasPrefix(line, "State:") { | ||
| if strings.Contains(line, "zombie") { | ||
| zombies = append(zombies, processID) | ||
| } | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return zombies, nil | ||
| } | ||
|
|
||
| // StartReaper starts a goroutine to reap processes periodically if called | ||
| // from a pid 1 process. | ||
| // If period is 0, then it is defaulted to 5 seconds. | ||
| // A caller can adjust the period depending on how many and how frequently zombie | ||
| // processes are created and need to be reaped. | ||
| func StartReaper(period time.Duration) { | ||
| if os.Getpid() == 1 { | ||
| const defaultReaperPeriodSeconds = 5 | ||
| if period == 0 { | ||
| period = defaultReaperPeriodSeconds * time.Second | ||
| } | ||
| go func() { | ||
| var zs []int | ||
| var err error | ||
| for { | ||
| zs, err = parseProcForZombies() | ||
| if err != nil { | ||
| klog.Errorf("Failed to parse proc filesystem to find processes to reap: %v", err) | ||
| continue | ||
| } | ||
| time.Sleep(period) | ||
| for _, z := range zs { | ||
| cpid, err := syscall.Wait4(z, nil, syscall.WNOHANG, nil) | ||
| if err != nil { | ||
| klog.Errorf("Unable to reap process pid %v: %v", cpid, err) | ||
| } | ||
| } | ||
| } | ||
| }() | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If period is less than or equal to zero set it to five seconds and add a comment above. Also describe why you might change the default period (if you create tens of thousands of defunct processes every five seconds).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.