Use identity creation times for parent/child PID-reuse checks - #2912
Open
snowyukitty wants to merge 1 commit into
Open
snowyukitty wants to merge 1 commit into
snowyukitty wants to merge 1 commit into
Conversation
Use platform identity creation times for chronological relationship checks while retaining the Windows slow fallback for privileged processes. Preserve observed relationships only when ordering remains inconclusive, and cover null times, fallback behavior, recursion, and intervening process exits.
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.
Summary
Description
Process.parent()andProcess.children()currently compare rawcreate_time()values to reject parent-child relationships that lookchronologically impossible. Those calls bypass the platform-specific creation
time selected by
Process._get_ident().This change uses identity creation times for the ordering check, while retaining
the existing fresh
Process(self.pid)lookup so an intervening process exit canstill raise
NoSuchProcess. If an ordering time is unavailable, the check isinconclusive and the observed relationship is preserved.
The platform behavior is:
psutil's PID-reuse detection.
chronological filter is disabled. This matches the existing PID-reuse FAQ,
which documents that identity on those platforms is based on PID alone.
denied, the code retries through
create_time(), whose slower fallback canstill retrieve the timestamp for privileged processes. This preserves the
stale-PPID/PID-reuse filtering restored by
697513a.
A valid
0.0returned by the Windows fallback remains an ordering value. Onlyif the fallback also raises
AccessDeniedorZombieProcessis the checktreated as inconclusive; in that case the relationship is preserved instead
of propagating the exception.
The tests cover known inverted times, null identity times on either side,
Windows fast-to-slow fallback (including a
0.0result), denied fallback,direct and recursive
children(), and the fresh-self lookup retained from#2542.
Validation
python setup.py build_ext -i --parallel 4: passpytest -q tests/test_process.py -k "null_ctime or refreshes_self or reused_pid":6 passed, 96 deselected12 failed, 6 passed, 96 deselected(expected red)5 failed, 6 passed, 96 deselected(all five fallback-success cells)pytest -q tests/test_process.py -k "not test_open_files":75 passed, 25 skipped, 2 deselectedpytest -q tests/test_testutils.py tests/test_misc.py tests/test_contracts.py tests/test_system.py:169 passed, 13 skippedparent(),children(), andrecursive
children()for 566 live processes: zero decisions differed fromthe exact base, while a base-vs-base churn control differed in 13.
fallbacks, all successful; no process had both paths denied on this host.
Hardened or low-integrity environments may exercise that defensive branch.
services.exe.children()measured about 1.46 seconds on the exact base and1.41-1.46 seconds with this patch, with 147-148 of about 160 children taking
the slow path in both. An experimental no-fallback version took 14-17 ms, but
reproduced the bogus-child regression reverted by
697513a; the speedup camefrom skipping a necessary check.
ruff check,black --check, andgit diff --check: passThe full
tests/test_process.pyrun has onetest_open_filesWindowssubprocess race (
1 failed, 76 passed, 25 skipped). The same failure reproduceson the unmodified upstream base. Non-Windows platforms and
tests/test_memleaks.pywere not run locally.