Skip to content

Commit bfdc29f

Browse files
authored
fix: suppress benign errors when listing processes (#14660)
1 parent bf87c97 commit bfdc29f

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

agent/agent.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,16 +1676,12 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
16761676
}
16771677

16781678
score, niceErr := proc.Niceness(a.syscaller)
1679-
if niceErr != nil && !xerrors.Is(niceErr, os.ErrPermission) {
1680-
if !isNoSuchProcessErr(niceErr) {
1681-
debouncer.Warn(ctx, "unable to get proc niceness",
1682-
slog.F("cmd", proc.Cmd()),
1683-
slog.F("pid", proc.PID),
1684-
slog.Error(niceErr),
1685-
)
1686-
}
1687-
1688-
continue
1679+
if !isBenignProcessErr(niceErr) {
1680+
debouncer.Warn(ctx, "unable to get proc niceness",
1681+
slog.F("cmd", proc.Cmd()),
1682+
slog.F("pid", proc.PID),
1683+
slog.Error(niceErr),
1684+
)
16891685
}
16901686

16911687
// We only want processes that don't have a nice value set
@@ -1699,31 +1695,27 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
16991695

17001696
if niceErr == nil {
17011697
err := proc.SetNiceness(a.syscaller, niceness)
1702-
if err != nil && !xerrors.Is(err, os.ErrPermission) {
1703-
if !isNoSuchProcessErr(err) {
1704-
debouncer.Warn(ctx, "unable to set proc niceness",
1705-
slog.F("cmd", proc.Cmd()),
1706-
slog.F("pid", proc.PID),
1707-
slog.F("niceness", niceness),
1708-
slog.Error(err),
1709-
)
1710-
}
1698+
if !isBenignProcessErr(err) {
1699+
debouncer.Warn(ctx, "unable to set proc niceness",
1700+
slog.F("cmd", proc.Cmd()),
1701+
slog.F("pid", proc.PID),
1702+
slog.F("niceness", niceness),
1703+
slog.Error(err),
1704+
)
17111705
}
17121706
}
17131707

17141708
// 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.
17151709
if oomScore != unsetOOMScore && oomScore != proc.OOMScoreAdj && !isCustomOOMScore(agentScore, proc) {
17161710
oomScoreStr := strconv.Itoa(oomScore)
17171711
err := afero.WriteFile(a.filesystem, fmt.Sprintf("/proc/%d/oom_score_adj", proc.PID), []byte(oomScoreStr), 0o644)
1718-
if err != nil && !xerrors.Is(err, os.ErrPermission) {
1719-
if !isNoSuchProcessErr(err) {
1720-
debouncer.Warn(ctx, "unable to set oom_score_adj",
1721-
slog.F("cmd", proc.Cmd()),
1722-
slog.F("pid", proc.PID),
1723-
slog.F("score", oomScoreStr),
1724-
slog.Error(err),
1725-
)
1726-
}
1712+
if !isBenignProcessErr(err) {
1713+
debouncer.Warn(ctx, "unable to set oom_score_adj",
1714+
slog.F("cmd", proc.Cmd()),
1715+
slog.F("pid", proc.PID),
1716+
slog.F("score", oomScoreStr),
1717+
slog.Error(err),
1718+
)
17271719
}
17281720
}
17291721
modProcs = append(modProcs, proc)
@@ -2154,6 +2146,13 @@ func (l *logDebouncer) log(ctx context.Context, level slog.Level, msg string, fi
21542146
l.messages[msg] = time.Now()
21552147
}
21562148

2149+
func isBenignProcessErr(err error) bool {
2150+
return err != nil &&
2151+
(xerrors.Is(err, os.ErrNotExist) ||
2152+
xerrors.Is(err, os.ErrPermission) ||
2153+
isNoSuchProcessErr(err))
2154+
}
2155+
21572156
func isNoSuchProcessErr(err error) bool {
21582157
return err != nil && strings.Contains(err.Error(), "no such process")
21592158
}

agent/agentproc/proc_unix.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@ func List(fs afero.Fs, syscaller Syscaller) ([]*Process, error) {
4545

4646
cmdline, err := afero.ReadFile(fs, filepath.Join(defaultProcDir, entry, "cmdline"))
4747
if err != nil {
48-
var errNo syscall.Errno
49-
if xerrors.As(err, &errNo) && errNo == syscall.EPERM {
48+
if isBenignError(err) {
5049
continue
5150
}
5251
return nil, xerrors.Errorf("read cmdline: %w", err)
5352
}
5453

5554
oomScore, err := afero.ReadFile(fs, filepath.Join(defaultProcDir, entry, "oom_score_adj"))
5655
if err != nil {
57-
if xerrors.Is(err, os.ErrPermission) {
56+
if isBenignError(err) {
5857
continue
5958
}
6059

@@ -124,3 +123,12 @@ func (p *Process) Cmd() string {
124123
func (p *Process) cmdLine() []string {
125124
return strings.Split(p.CmdLine, "\x00")
126125
}
126+
127+
func isBenignError(err error) bool {
128+
var errno syscall.Errno
129+
if !xerrors.As(err, &errno) {
130+
return false
131+
}
132+
133+
return errno == syscall.ESRCH || errno == syscall.EPERM || xerrors.Is(err, os.ErrNotExist)
134+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy