Skip to content

fix(procfs): drop stale /proc/<pid> entries after reap#1819

Open
jiuyue486 wants to merge 1 commit intoDragonOS-Community:masterfrom
jiuyue486:fix/procfs-pid-sync-clean
Open

fix(procfs): drop stale /proc/<pid> entries after reap#1819
jiuyue486 wants to merge 1 commit intoDragonOS-Community:masterfrom
jiuyue486:fix/procfs-pid-sync-clean

Conversation

@jiuyue486
Copy link

概述

修复进程退出并被回收后,/proc/<pid> 目录项仍可能残留的问题。

问题描述

当前 procfs 会缓存 /proc 下的 pid 目录项。
当进程已经退出并且被 waitpid() 回收后,缓存中的 /proc/<pid> 目录项可能没有及时失效,导致 /proc 视图与实际进程状态不同步。

修复内容

本次修改主要包括:

  • /proc 根目录的 populate_children() 中先清理已经失效的 pid 目录缓存,再填充当前仍存在的进程目录
  • lookup_child() 中重新检查目标 pid 是否仍然存在;若进程已不存在,则删除缓存项并返回 ENOENT
  • /proc/[pid] 目录实现中增加 validate_child(),使进程消失后对应的 pid 子树条目能够正确失效

修复效果

修复后,当进程被回收后,对应的 /proc/<pid> 目录会正确消失,避免 procfs 中出现陈旧的 pid 目录项,使 procfs 行为与实际进程生命周期保持一致。

说明

本次提交仅聚焦于修复 procfs 的 pid 目录同步问题,不包含额外测试或其他功能改动。

@github-actions github-actions bot added the Bug fix A bug is fixed in this pull request label Mar 16, 2026
@fslongjin
Copy link
Member

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8924767e1c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +90 to 92
if ProcessManager::find(pid).is_none() {
cached_children.remove(name);
return Err(SystemError::ENOENT);

Choose a reason for hiding this comment

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

P1 Badge Validate cached /proc/ entries before returning them

ProcDir::find returns a cached child immediately unless validate_child rejects it (see template/dir.rs), but RootDirOps still uses the default validator, so cached /proc/<pid> entries bypass this new existence check entirely. In practice, if a PID directory was cached before reap, later lookups can still resolve that stale inode without entering this lookup_child path, so /proc/<pid> may remain visible until a separate readdir triggers cache pruning.

Useful? React with 👍 / 👎.

Comment on lines +134 to +136
cached_children.retain(|name, _| {
if let Ok(pid) = name.parse::<RawPid>() {
pid_list.contains(&pid)

Choose a reason for hiding this comment

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

P2 Badge Avoid quadratic PID cache pruning in /proc readdir

This cache cleanup performs pid_list.contains(&pid) inside retain, making populate_children() O(number_of_cached_entries × number_of_pids). On systems with many tasks, each /proc directory listing can become disproportionately expensive while holding the cache write lock, causing avoidable latency spikes; converting pid_list to a set before retain would keep pruning linear.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug fix A bug is fixed in this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants