Skip to content

Commit 3716afa

Browse files
authored
fix: add benign error suppression for process priority management (coder#15020)
This PR backports some benign error suppression into 2.15
1 parent 0f63510 commit 3716afa

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

agent/agent.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,13 +1669,12 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
16691669
}
16701670

16711671
score, niceErr := proc.Niceness(a.syscaller)
1672-
if niceErr != nil && !xerrors.Is(niceErr, os.ErrPermission) {
1672+
if !isBenignProcessErr(niceErr) {
16731673
debouncer.Warn(ctx, "unable to get proc niceness",
16741674
slog.F("cmd", proc.Cmd()),
16751675
slog.F("pid", proc.PID),
16761676
slog.Error(niceErr),
16771677
)
1678-
continue
16791678
}
16801679

16811680
// We only want processes that don't have a nice value set
@@ -1689,7 +1688,7 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
16891688

16901689
if niceErr == nil {
16911690
err := proc.SetNiceness(a.syscaller, niceness)
1692-
if err != nil && !xerrors.Is(err, os.ErrPermission) {
1691+
if !isBenignProcessErr(err) {
16931692
debouncer.Warn(ctx, "unable to set proc niceness",
16941693
slog.F("cmd", proc.Cmd()),
16951694
slog.F("pid", proc.PID),
@@ -1703,7 +1702,7 @@ func (a *agent) manageProcessPriority(ctx context.Context, debouncer *logDebounc
17031702
if oomScore != unsetOOMScore && oomScore != proc.OOMScoreAdj && !isCustomOOMScore(agentScore, proc) {
17041703
oomScoreStr := strconv.Itoa(oomScore)
17051704
err := afero.WriteFile(a.filesystem, fmt.Sprintf("/proc/%d/oom_score_adj", proc.PID), []byte(oomScoreStr), 0o644)
1706-
if err != nil && !xerrors.Is(err, os.ErrPermission) {
1705+
if !isBenignProcessErr(err) {
17071706
debouncer.Warn(ctx, "unable to set oom_score_adj",
17081707
slog.F("cmd", proc.Cmd()),
17091708
slog.F("pid", proc.PID),
@@ -2139,3 +2138,14 @@ func (l *logDebouncer) log(ctx context.Context, level slog.Level, msg string, fi
21392138
}
21402139
l.messages[msg] = time.Now()
21412140
}
2141+
2142+
func isBenignProcessErr(err error) bool {
2143+
return err != nil &&
2144+
(xerrors.Is(err, os.ErrNotExist) ||
2145+
xerrors.Is(err, os.ErrPermission) ||
2146+
isNoSuchProcessErr(err))
2147+
}
2148+
2149+
func isNoSuchProcessErr(err error) bool {
2150+
return err != nil && strings.Contains(err.Error(), "no such process")
2151+
}

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)