Skip to content

fix: harden Linux procfs reads and ancestry traversal#180

Merged
HiGarfield merged 2 commits intomasterfrom
codex/improve-code-stability-and-performance
Apr 13, 2026
Merged

fix: harden Linux procfs reads and ancestry traversal#180
HiGarfield merged 2 commits intomasterfrom
codex/improve-code-stability-and-performance

Conversation

@HiGarfield
Copy link
Copy Markdown
Owner

Motivation

  • Avoid transient read failures when /proc or sysfs reads are interrupted by signals and prevent pathological infinite loops when traversing parent chains from inconsistent /proc snapshots.

Description

  • Retry getline() on EINTR in read_line_from_file() to tolerate signal-interrupted reads and return the file contents only on real failures (file: src/util.c).
  • Add a defensive next_ppid check in is_child_of() to reject invalid or self-parent links (next_ppid <= 0 || next_ppid == child_pid) and return non-descendant immediately to prevent endless loops (file: src/process_iterator_linux.c).
  • Changes are minimal, localized, and preserve existing semantics while improving robustness under racey /proc conditions.

Testing

  • Ran code formatting with cmake --build build --target format successfully.
  • Built with GCC and Clang using cmake and cmake --build ... --target all with no build errors.
  • Executed static analysis via the check target (cppcheck + clang-tidy) and found no report files with errors; the check target completed successfully.
  • Ran the test suite and dynamic analysis with Valgrind (cmake --build build --target valgrind) and all unit/integration tests and valgrind runs passed with no leaks or errors.

Codex Task

Retry getline when interrupted by signals to avoid transient

read failures while scanning procfs/sysfs files.

Also guard ancestry traversal against invalid self-parent

links, preventing pathological infinite loops in racey

process snapshots.
Copilot AI review requested due to automatic review settings April 13, 2026 16:42
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves robustness of Linux /proc/sysfs interactions by hardening single-line file reads against signal interruptions and making process ancestry traversal more defensive against inconsistent /proc snapshots.

Changes:

  • Retry getline() on EINTR in read_line_from_file() to reduce transient /proc/sysfs read failures.
  • Add a guard in Linux is_child_of() to avoid endless loops when a process reports an invalid/self parent link.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/util.c Retries getline() on EINTR while reading first-line procfs/sysfs files.
src/process_iterator_linux.c Adds defensive PPID validation during parent-chain traversal to prevent pathological loops.

Comment thread src/util.c Outdated
Comment on lines +391 to +395
* procfs/sysfs files.
*/
do {
errno = 0;
read_result = getline(&line, &line_size, input_file);
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EINTR retry loop should clear the stream error indicator before retrying. After a stdio call like getline() fails, the FILE error flag can remain set (sticky) and a subsequent getline() may immediately fail again unless clearerr(input_file) is called when errno==EINTR. Without clearing, this change may still return NULL on an interrupted read.

Suggested change
* procfs/sysfs files.
*/
do {
errno = 0;
read_result = getline(&line, &line_size, input_file);
* procfs/sysfs files. Because stdio stream error indicators are sticky,
* clear the stream state before retrying after EINTR.
*/
do {
errno = 0;
read_result = getline(&line, &line_size, input_file);
if (read_result < 0 && errno == EINTR) {
clearerr(input_file);
}

Copilot uses AI. Check for mistakes.
@HiGarfield
Copy link
Copy Markdown
Owner Author

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown

Copilot AI commented Apr 13, 2026

@copilot apply changes based on the comments in this thread

Done in b1281ac. Added clearerr(input_file) inside the loop when errno == EINTR so the sticky stdio error indicator is cleared before each retry, matching the suggestion exactly.

@HiGarfield HiGarfield merged commit eaf1b90 into master Apr 13, 2026
@HiGarfield HiGarfield deleted the codex/improve-code-stability-and-performance branch April 13, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants