diff --git a/changes/20250807104110.bugfix b/changes/20250807104110.bugfix new file mode 100644 index 0000000000..736ef5d603 --- /dev/null +++ b/changes/20250807104110.bugfix @@ -0,0 +1 @@ +:bug: `proc` Make sure that invalid PIDs are ignored when stopping processes diff --git a/utils/proc/find/find_linux.go b/utils/proc/find/find_linux.go index bf0e76082d..acb4453291 100644 --- a/utils/proc/find/find_linux.go +++ b/utils/proc/find/find_linux.go @@ -17,8 +17,9 @@ import ( ) const ( - procFS = "/proc" - procDataFile = "cmdline" + procFS = "/proc" + procDataFile = "cmdline" + invalidPIDErr = "could not parse PID from proc path" ) func checkProcessMatch(ctx context.Context, fs filesystem.FS, re *regexp.Regexp, procEntry string) (ok bool, err error) { @@ -51,7 +52,7 @@ func parseProcess(ctx context.Context, entry string) (p proc.IProcess, err error pid, err := strconv.Atoi(strings.Trim(strings.TrimSuffix(strings.TrimPrefix(entry, procFS), fmt.Sprintf("%v", procDataFile)), "/")) if err != nil { - err = commonerrors.WrapErrorf(commonerrors.ErrUnexpected, err, "could not parse PID from proc path '%v'", entry) + err = commonerrors.WrapErrorf(commonerrors.ErrUnexpected, err, "%v '%v'", invalidPIDErr, entry) return } @@ -75,7 +76,7 @@ func FindProcessByRegexForFS(ctx context.Context, fs filesystem.FS, re *regexp.R return } - searchGlobTerm := fmt.Sprintf("%v/*/%v", procFS, procDataFile) + searchGlobTerm := fmt.Sprintf("%v/[0-9]*/%v", procFS, procDataFile) procEntries, err := fs.Glob(searchGlobTerm) if err != nil { err = commonerrors.WrapErrorf(commonerrors.ErrUnexpected, err, "an error occurred when searching for processes using the following glob '%v'", searchGlobTerm) @@ -90,6 +91,7 @@ func FindProcessByRegexForFS(ctx context.Context, fs filesystem.FS, re *regexp.R p, err = parseProcess(ctx, entry) if err != nil { + err = commonerrors.IgnoreCorrespondTo(err, invalidPIDErr) return } diff --git a/utils/proc/find/find_linux_test.go b/utils/proc/find/find_linux_test.go index be2a9ff421..8462772e13 100644 --- a/utils/proc/find/find_linux_test.go +++ b/utils/proc/find/find_linux_test.go @@ -13,6 +13,7 @@ import ( "github.com/ARM-software/golang-utils/utils/commonerrors" "github.com/ARM-software/golang-utils/utils/commonerrors/errortest" + "github.com/ARM-software/golang-utils/utils/filesystem" "github.com/ARM-software/golang-utils/utils/logs" "github.com/ARM-software/golang-utils/utils/logs/logstest" "github.com/ARM-software/golang-utils/utils/subprocess" @@ -87,4 +88,12 @@ func TestFind(t *testing.T) { assert.Empty(t, processes) }) + t.Run("Exclude invalid", func(t *testing.T) { + ctx := context.Background() + allProcesses, err := filesystem.Glob(fmt.Sprintf("%v/[0-9]*/%v", procFS, procDataFile)) + require.NoError(t, err) + processes, err := FindProcessByName(ctx, "") // empty name will return all processes that were valid according to the logic (e.g. no invalid names or empty cmdline files) + assert.NoError(t, err) + assert.LessOrEqual(t, len(processes), len(allProcesses)) + }) }