fix: suppress benign errors when listing processes (#14660)

This commit is contained in:
Jon Ayers
2024-09-12 23:00:04 +01:00
committed by GitHub
parent bf87c97ede
commit bfdc29f466
2 changed files with 38 additions and 31 deletions

View File

@ -1676,8 +1676,7 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
} }
score, niceErr := proc.Niceness(a.syscaller) score, niceErr := proc.Niceness(a.syscaller)
if niceErr != nil && !xerrors.Is(niceErr, os.ErrPermission) { if !isBenignProcessErr(niceErr) {
if !isNoSuchProcessErr(niceErr) {
debouncer.Warn(ctx, "unable to get proc niceness", debouncer.Warn(ctx, "unable to get proc niceness",
slog.F("cmd", proc.Cmd()), slog.F("cmd", proc.Cmd()),
slog.F("pid", proc.PID), slog.F("pid", proc.PID),
@ -1685,9 +1684,6 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
) )
} }
continue
}
// We only want processes that don't have a nice value set // We only want processes that don't have a nice value set
// so we don't override user nice values. // so we don't override user nice values.
// Getpriority actually returns priority for the nice value // Getpriority actually returns priority for the nice value
@ -1699,8 +1695,7 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
if niceErr == nil { if niceErr == nil {
err := proc.SetNiceness(a.syscaller, niceness) err := proc.SetNiceness(a.syscaller, niceness)
if err != nil && !xerrors.Is(err, os.ErrPermission) { if !isBenignProcessErr(err) {
if !isNoSuchProcessErr(err) {
debouncer.Warn(ctx, "unable to set proc niceness", debouncer.Warn(ctx, "unable to set proc niceness",
slog.F("cmd", proc.Cmd()), slog.F("cmd", proc.Cmd()),
slog.F("pid", proc.PID), slog.F("pid", proc.PID),
@ -1709,14 +1704,12 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
) )
} }
} }
}
// If the oom score is valid and it's not already set and isn't a custom value set by another process then it's ok to update it. // If the oom score is valid and it's not already set and isn't a custom value set by another process then it's ok to update it.
if oomScore != unsetOOMScore && oomScore != proc.OOMScoreAdj && !isCustomOOMScore(agentScore, proc) { if oomScore != unsetOOMScore && oomScore != proc.OOMScoreAdj && !isCustomOOMScore(agentScore, proc) {
oomScoreStr := strconv.Itoa(oomScore) oomScoreStr := strconv.Itoa(oomScore)
err := afero.WriteFile(a.filesystem, fmt.Sprintf("/proc/%d/oom_score_adj", proc.PID), []byte(oomScoreStr), 0o644) err := afero.WriteFile(a.filesystem, fmt.Sprintf("/proc/%d/oom_score_adj", proc.PID), []byte(oomScoreStr), 0o644)
if err != nil && !xerrors.Is(err, os.ErrPermission) { if !isBenignProcessErr(err) {
if !isNoSuchProcessErr(err) {
debouncer.Warn(ctx, "unable to set oom_score_adj", debouncer.Warn(ctx, "unable to set oom_score_adj",
slog.F("cmd", proc.Cmd()), slog.F("cmd", proc.Cmd()),
slog.F("pid", proc.PID), slog.F("pid", proc.PID),
@ -1725,7 +1718,6 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
) )
} }
} }
}
modProcs = append(modProcs, proc) modProcs = append(modProcs, proc)
} }
return modProcs, nil return modProcs, nil
@ -2154,6 +2146,13 @@ func (l *logDebouncer) log(ctx context.Context, level slog.Level, msg string, fi
l.messages[msg] = time.Now() l.messages[msg] = time.Now()
} }
func isBenignProcessErr(err error) bool {
return err != nil &&
(xerrors.Is(err, os.ErrNotExist) ||
xerrors.Is(err, os.ErrPermission) ||
isNoSuchProcessErr(err))
}
func isNoSuchProcessErr(err error) bool { func isNoSuchProcessErr(err error) bool {
return err != nil && strings.Contains(err.Error(), "no such process") return err != nil && strings.Contains(err.Error(), "no such process")
} }

View File

@ -45,8 +45,7 @@ func List(fs afero.Fs, syscaller Syscaller) ([]*Process, error) {
cmdline, err := afero.ReadFile(fs, filepath.Join(defaultProcDir, entry, "cmdline")) cmdline, err := afero.ReadFile(fs, filepath.Join(defaultProcDir, entry, "cmdline"))
if err != nil { if err != nil {
var errNo syscall.Errno if isBenignError(err) {
if xerrors.As(err, &errNo) && errNo == syscall.EPERM {
continue continue
} }
return nil, xerrors.Errorf("read cmdline: %w", err) return nil, xerrors.Errorf("read cmdline: %w", err)
@ -54,7 +53,7 @@ func List(fs afero.Fs, syscaller Syscaller) ([]*Process, error) {
oomScore, err := afero.ReadFile(fs, filepath.Join(defaultProcDir, entry, "oom_score_adj")) oomScore, err := afero.ReadFile(fs, filepath.Join(defaultProcDir, entry, "oom_score_adj"))
if err != nil { if err != nil {
if xerrors.Is(err, os.ErrPermission) { if isBenignError(err) {
continue continue
} }
@ -124,3 +123,12 @@ func (p *Process) Cmd() string {
func (p *Process) cmdLine() []string { func (p *Process) cmdLine() []string {
return strings.Split(p.CmdLine, "\x00") return strings.Split(p.CmdLine, "\x00")
} }
func isBenignError(err error) bool {
var errno syscall.Errno
if !xerrors.As(err, &errno) {
return false
}
return errno == syscall.ESRCH || errno == syscall.EPERM || xerrors.Is(err, os.ErrNotExist)
}