Skip to content

Commit 8101abd

Browse files
committed
Studio: escalate the probe teardown in one loop so no attempt is left uncovered
An OSError from the post-kill read was raised inside the timeout branch, where the outer OSError handler was a sibling rather than an ancestor, so it escaped a function documented never to raise and left the child unreaped. Terminate and kill now escalate through one loop with a single pair of handlers, so every attempt is covered the same way, and anything not confirmed dead reaches the reaper whether it outlived SIGKILL or could not be read.
1 parent 10b8faf commit 8101abd

2 files changed

Lines changed: 44 additions & 13 deletions

File tree

‎studio/backend/tests/test_device_allocation_probe.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,37 @@ def test_a_child_that_dies_within_the_grace_needs_no_reaper(monkeypatch):
369369
assert threading.active_count() <= before
370370

371371

372+
def test_a_read_failure_during_teardown_does_not_escape(monkeypatch):
373+
# The first wait times out, so teardown starts; the post-kill read then fails. That
374+
# error is raised inside the timeout handling, so it needs the same treatment, and the
375+
# child is not confirmed dead, so it still has to reach the reaper.
376+
proc = _FakeProc(returncode = None, timeouts = 1)
377+
reaped = threading.Event()
378+
proc.wait = lambda: reaped.set() # type: ignore[method-assign]
379+
original = proc.communicate
380+
381+
def _fail_after_first(timeout = None):
382+
try:
383+
return original(timeout = timeout)
384+
except subprocess.TimeoutExpired:
385+
raise
386+
finally:
387+
proc.communicate = _boom # type: ignore[method-assign]
388+
389+
def _boom(timeout = None):
390+
proc.calls.append("communicate")
391+
raise OSError("handle is invalid")
392+
393+
proc.communicate = _fail_after_first # type: ignore[method-assign]
394+
_patch_popen(monkeypatch, proc)
395+
396+
result = probe_torch_device_allocation("cuda:0")
397+
398+
assert result.ok is False
399+
assert result.reason == "probe timed out"
400+
assert reaped.wait(timeout = 5), "unconfirmed child was never handed to the reaper"
401+
402+
372403
def test_a_read_failure_fails_closed_and_tears_the_child_down(monkeypatch):
373404
# A Windows pipe/handle failure, or a read error under resource pressure. This function
374405
# promises never to raise and to treat every unknown outcome as unsafe, and the child

‎studio/backend/utils/device_allocation_probe.py‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,25 +280,25 @@ def _terminate_and_drain(process: subprocess.Popen) -> str:
280280
"""Terminate, give it a moment, kill, and always drain and reap, so an overrunning
281281
probe cannot leave a zombie or a held pipe behind."""
282282
stderr = ""
283-
try:
284-
process.terminate()
285-
except OSError:
286-
pass
287-
try:
288-
_, stderr = process.communicate(timeout = _TERMINATE_GRACE_SECONDS)
289-
except subprocess.TimeoutExpired:
283+
# Escalate in one loop rather than nesting, so every attempt is covered by the same
284+
# handler. The nested form grew a gap: an OSError raised inside the timeout branch was
285+
# not a sibling of the outer OSError handler and escaped a function that must not raise.
286+
for signal_child in (process.terminate, process.kill):
290287
try:
291-
process.kill()
288+
signal_child()
292289
except OSError:
293290
pass
294291
try:
295292
_, stderr = process.communicate(timeout = _TERMINATE_GRACE_SECONDS)
293+
return stderr or ""
296294
except subprocess.TimeoutExpired:
297-
# Still alive after SIGKILL: stuck in the driver. Hand it to a reaper rather
298-
# than returning and letting the last reference go.
299-
_reap_later(process)
300-
except OSError:
301-
pass
295+
continue # still alive, escalate
296+
except OSError:
297+
break # pipes are unusable, so there is nothing left to drain
298+
299+
# Not confirmed dead: either it outlived SIGKILL, stuck in the driver, or we could not
300+
# read it. Either way the last reference must not simply be dropped.
301+
_reap_later(process)
302302
return stderr or ""
303303

304304

0 commit comments

Comments
 (0)