Return an error on reading empty proc pid stat file#1995
Merged
shirou merged 1 commit intoshirou:masterfrom Jan 23, 2026
Merged
Return an error on reading empty proc pid stat file#1995shirou merged 1 commit intoshirou:masterfrom
shirou merged 1 commit intoshirou:masterfrom
Conversation
Contributor
Author
|
I used |
d018968 to
656d117
Compare
shirou
approved these changes
Jan 22, 2026
Owner
shirou
left a comment
There was a problem hiding this comment.
Great catch! I wasn't aware of this stat file behavior. Thank you for this effective fix.
However, as you said, could you change the error message to indicate the likely cause?
248a898 to
62a181c
Compare
Contributor
Author
|
@shirou done ! Thanks for the quick review ! |
This was referenced Apr 23, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 is a race when reading
/proc/<pid>/statwhere the file can be opened while the process still exists but then it's gone when reading the content.Depending on the underlying procfs, the file can just appear as empty in this case (ie. read just returns EOF directly), and the code happily tries to parse the file with the expected format, eventually resulting in a panic.
Testing whether the content is empty before calling
splitProcStatshould prevent most similar occurrences.Note that more subtle cases can still happen, eg. if the file is long enough we might only read some of it, but Go's
os.ReadFileuses a big enough buffer by default that it should be very rare.Ideally the code should still check that the content has the expected format and not blindly access it.
Example
Below is a stack trace from an occurence:
splitProcStatis given an empty bytes slice, soLastIndexBytereturns -1, and we index the empty slice starting at index 1 (-1 + 2), resulting in a panic.