(active: ActiveSuite, kill_grace: int)
| 279 | |
| 280 | |
| 281 | def terminate_process_tree(active: ActiveSuite, kill_grace: int) -> None: |
| 282 | process = active.process |
| 283 | leader_exited = process.poll() is not None |
| 284 | if os.name == "nt": |
| 285 | if leader_exited: |
| 286 | # The leader can exit on its own between the timeout decision and |
| 287 | # this call. Refusing outright made the harness itself lose a race: |
| 288 | # a natural exit at the wrong moment failed the whole wave, which is |
| 289 | # how a deliberately-hanging fixture suite reddened a release run. |
| 290 | # taskkill /T cannot walk a tree from a dead PID, so prove cleanup |
| 291 | # the only way still available -- nothing is parented to it. |
| 292 | blocker = windows_tree_cleanup_blocker(process.pid) |
| 293 | if blocker is not None: |
| 294 | raise RuntimeError( |
| 295 | f"suite {active.name!r} leader exited and tree cleanup " |
| 296 | f"could not be proven: {blocker}" |
| 297 | ) |
| 298 | return |
| 299 | if not windows_taskkill_tree(process.pid): |
| 300 | if process.poll() is None: |
| 301 | process.kill() |
| 302 | try: |
| 303 | process.wait(timeout=kill_grace) |
| 304 | except subprocess.TimeoutExpired: |
| 305 | pass |
| 306 | raise RuntimeError( |
| 307 | f"suite {active.name!r} taskkill could not prove process-tree cleanup" |
| 308 | ) |
| 309 | try: |
| 310 | process.wait(timeout=kill_grace) |
| 311 | except subprocess.TimeoutExpired as exc: |
| 312 | process.kill() |
| 313 | raise RuntimeError( |
| 314 | f"suite {active.name!r} process tree resisted forced termination" |
| 315 | ) from exc |
| 316 | return |
| 317 | |
| 318 | def group_active() -> bool: |
| 319 | try: |
| 320 | os.killpg(process.pid, 0) |
| 321 | return True |
| 322 | except ProcessLookupError: |
| 323 | return False |
| 324 | except PermissionError: |
| 325 | return True |
| 326 | |
| 327 | def wait_for_group_exit(deadline: float) -> bool: |
| 328 | while time.monotonic() < deadline: |
| 329 | process.poll() |
| 330 | if not group_active(): |
| 331 | return True |
| 332 | time.sleep(POLL_SECONDS) |
| 333 | process.poll() |
| 334 | return not group_active() |
| 335 | |
| 336 | try: |
| 337 | os.killpg(process.pid, signal.SIGTERM) |
| 338 | except ProcessLookupError: |
no test coverage detected