2828SLOW_SUITES = frozenset (("incremental" , "store_arch" , "daemon_runtime" ))
2929POLL_SECONDS = 0.05
3030
31+ # WHY: the Windows descendant probe below is a cold `powershell.exe` + CIM
32+ # start. On a GitHub Windows runner that routinely costs seconds -- interpreter
33+ # start-up, module autoload, CIM service warm-up -- and that cost is unrelated
34+ # to the state of the tree being proven. --kill-grace bounds how long a
35+ # *process* may resist termination and CI passes 1s, so timing the probe with
36+ # it made the proof a function of interpreter latency instead of the tree: a
37+ # cold start blew the 1s budget, TimeoutExpired became "assume the worst", and
38+ # an already-clean shard exited 2. This is a stable-state budget, not a race
39+ # tune -- the answer does not change with waiting, the budget only has to cover
40+ # a cold start, and a probe that still cannot finish is reported as an
41+ # unfinished probe rather than as a leaked tree.
42+ WINDOWS_DESCENDANT_PROBE_SECONDS = 15
43+ WINDOWS_DESCENDANT_PROBE_ATTEMPTS = 2
44+
3145
3246@dataclass
3347class ActiveSuite :
@@ -123,37 +137,54 @@ def start_suite(
123137 )
124138
125139
126- def windows_descendants (pid : int , timeout : int ) -> bool :
127- """True if any live process still claims `pid` as its parent .
140+ def windows_tree_cleanup_blocker (pid : int ) -> str | None :
141+ """Why `pid`'s tree cannot be called clean, or None when it provably is .
128142
129143 Used only when the suite leader has already exited: `taskkill /T` cannot
130144 walk a tree from a dead PID, so cleanup is proven by asking whether anything
131145 is still parented to it. One level deep on purpose -- Windows does not
132146 reparent orphans, so a grandchild keeps pointing at its own (dead) parent
133147 and would not be found here. That is a weaker proof than taskkill /T, which
134148 is why it is reserved for the case where the strong proof is impossible.
149+
150+ Fail-closed: a probe that times out, cannot start, or reports failure is
151+ never read as absence. The reason names WHICH of the two happened -- a probe
152+ that did not finish, or a counted set of live descendants -- because those
153+ are different defects and used to be reported with the same sentence.
135154 """
136- try :
137- completed = subprocess .run (
138- [
139- "powershell.exe" ,
140- "-NoProfile" ,
141- "-NonInteractive" ,
142- "-Command" ,
143- "@(Get-CimInstance Win32_Process -Filter "
144- f"'ParentProcessId={ pid } ').Count" ,
145- ],
146- check = False ,
147- stdin = subprocess .DEVNULL ,
148- capture_output = True ,
149- text = True ,
150- timeout = timeout ,
151- )
152- except (OSError , subprocess .TimeoutExpired ):
153- return True # cannot prove absence -> assume the worst
154- if completed .returncode != 0 :
155- return True
156- return (completed .stdout or "" ).strip () not in ("0" , "" )
155+ unproven = "descendant probe did not run"
156+ for _ in range (WINDOWS_DESCENDANT_PROBE_ATTEMPTS ):
157+ try :
158+ completed = subprocess .run (
159+ [
160+ "powershell.exe" ,
161+ "-NoProfile" ,
162+ "-NonInteractive" ,
163+ "-Command" ,
164+ "@(Get-CimInstance Win32_Process -Filter "
165+ f"'ParentProcessId={ pid } ').Count" ,
166+ ],
167+ check = False ,
168+ stdin = subprocess .DEVNULL ,
169+ capture_output = True ,
170+ text = True ,
171+ timeout = WINDOWS_DESCENDANT_PROBE_SECONDS ,
172+ )
173+ except subprocess .TimeoutExpired :
174+ unproven = (
175+ "descendant probe could not complete in "
176+ f"{ WINDOWS_DESCENDANT_PROBE_SECONDS } s"
177+ )
178+ continue
179+ except OSError as exc :
180+ return f"descendant probe could not run: { exc } "
181+ if completed .returncode != 0 :
182+ return f"descendant probe failed (rc={ completed .returncode } )"
183+ count = (completed .stdout or "" ).strip ()
184+ if count in ("0" , "" ):
185+ return None
186+ return f"{ count } live descendant(s)"
187+ return unproven
157188
158189
159190def terminate_process_tree (active : ActiveSuite , kill_grace : int ) -> None :
@@ -167,9 +198,11 @@ def terminate_process_tree(active: ActiveSuite, kill_grace: int) -> None:
167198 # how a deliberately-hanging fixture suite reddened a release run.
168199 # taskkill /T cannot walk a tree from a dead PID, so prove cleanup
169200 # the only way still available -- nothing is parented to it.
170- if windows_descendants (process .pid , kill_grace ):
201+ blocker = windows_tree_cleanup_blocker (process .pid )
202+ if blocker is not None :
171203 raise RuntimeError (
172- f"suite { active .name !r} leader exited leaving live descendants"
204+ f"suite { active .name !r} leader exited and tree cleanup "
205+ f"could not be proven: { blocker } "
173206 )
174207 return
175208 try :
0 commit comments