Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tests/test_upgrade_release_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,9 @@ def _process_identity(pid: int) -> str | None:
# PID reuse during the parallel suite must not look like a surviving descendant.
try:
stat = Path(f"/proc/{pid}/stat").read_text()
except FileNotFoundError:
except OSError:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] 放宽到 OSError 会把读失败当成「进程已消失」,让存活后代的断言静默通过

ESRCH 这个诊断本身是对的:我在 WSL 上复现了同一条路径(open('/proc/<pid>/stat') 后 kill 该子进程再 read()),三次读取均抛 OSError/ProcessLookupError(errno 3),Path.read_text() 同样如此。所以补 ProcessLookupError 是必要的。

问题在捕获范围。_process_exists() 直接以 _process_identity(pid) == identity 作为「仍存活」判据,而本行现在把 EACCES、EMFILE、EIO、EBADF 一律变成 return None,等价于「该 pid 不是记录中的进程」。后果是本文件第 938 行那条泄漏断言(assert time.monotonic() < deadline, "Detached descendants survived gate termination")与第 951 行 assert not any(...) 在读取失败时直接放行,而这两条恰恰是用来证明 gate 终止后没有残留后代的。

仓库内同类读取的既有写法是窄口径:

  • src/powercontext/source_management/background.py:50:except (FileNotFoundError, ProcessLookupError): return False
  • src/powercontext/source_management/durable.py:82:except (FileNotFoundError, ProcessLookupError): return True

建议改为 except (FileNotFoundError, ProcessLookupError):,与生产侧保持一致;若确实想容忍其它 OSError,请在 return None 前区分「进程不存在」与「读取失败」,后者应让断言失败而不是当作已消失。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Confirmed on 3948f2e with real Linux processes, without mocking Path.read_text(): after recording a live child's start time, I lowered RLIMIT_NOFILE to 64 and exhausted the remaining descriptors. os.kill(pid, 0) still succeeded, but _process_exists(pid, identity) returned False, so the cancellation test's assert not any(...) passed for that live child. The parent commit instead raised OSError(EMFILE); restoring descriptors made the same child detectable again.

I also reproduced ProcessLookupError(ESRCH) by opening /proc/<pid>/stat, terminating and reaping the child, then reading the open file. Please catch (FileNotFoundError, ProcessLookupError) so this race is handled without turning unrelated probe failures into evidence of successful cleanup.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Update: the narrow ESRCH fix is now on master via #2396, in commit 9862ef4b. _process_identity() already catches (FileNotFoundError, ProcessLookupError) there, handling the exit race while allowing unrelated read failures to surface.

This PR is still at 3948f2ee with the broad OSError catch and the previously demonstrated live-process false negative. Since the intended fix has already landed, I suggest closing this PR as superseded; retaining its broad catch when reconciling with master would weaken the assertion again.

# FileNotFoundError covers a vanished pid at open; ProcessLookupError
# (ESRCH) is raised when the process dies between open and read.
return None
fields = stat[stat.rfind(")") + 2 :].split()
return fields[19] if len(fields) > 19 else None
Expand Down
Loading