Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2306,7 +2306,11 @@ def __init__(

def timeout_fn():
self.timed_out = True
self.signal(ca["timeout_signal"])
try:
self.signal(ca["timeout_signal"])
except ProcessLookupError:
# Edge case: the process probably exited
pass

self._timeout_event = None
self._timeout_timer = None
Expand Down
19 changes: 19 additions & 0 deletions tests/sh_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3012,6 +3012,25 @@ def test_custom_timeout_signal(self):
else:
self.fail("we should have handled a TimeoutException")

def test_timeout_race_condition_process_exit(self):
import signal

from sh import TimeoutException

py = create_tmp_test(
"""
import time
time.sleep(0.05)
"""
)
# Run multiple times to increase likelihood of hitting the race condition
for _ in range(50):
try:
python(py.name, _timeout=0.1, _timeout_signal=signal.SIGTERM)
except TimeoutException:
# TimeoutException is OK, but ProcessLookupError isn't
pass

def test_append_stdout(self):
py = create_tmp_test(
"""
Expand Down