ci: use pytest-timeout's thread method instead of signal - #587
Merged
Conversation
pytest-timeout defaults to the "signal" method on POSIX, which arms SIGALRM and relies on the interpreter noticing it between bytecode instructions. If the main thread is blocked inside a native call that never returns control to Python -- a genuine OS-level deadlock, not a slow test -- the signal stays pending forever and the handler never runs. --timeout=10 was already configured; it just structurally can't interrupt that class of hang, which is exactly what happened on a recent PR's CI run: a job sat blocked for the full 30-minute job timeout instead of failing at 10s. The "thread" method runs a separate watcher thread that kills the whole process via os._exit() once the timeout elapses, regardless of what the main thread is doing -- it doesn't need the hung thread's cooperation. Under pytest-xdist (-n auto), that's one worker process; xdist detects the crash, reports the killed test as a failure, and reschedules the rest of that worker's queue onto a replacement worker, so the run doesn't hang. Trade-off: no clean teardown/reporting for whatever was running at the moment of the kill (os._exit() skips it), and pytest-timeout's own docs note the crash can show up as a generic error rather than a labeled timeout in some report formats. Worth it to turn a silent 30-minute stall into a ~10-second, clearly-attributed failure with a stack trace of every thread at the point of the hang.
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.
pytest-timeout defaults to the "signal" method on POSIX, which arms SIGALRM and relies on the interpreter noticing it between bytecode instructions. If the main thread is blocked inside a native call that never returns control to Python -- a genuine OS-level deadlock, not a slow test -- the signal stays pending forever and the handler never runs.
--timeout=10was already configured; it just structurally can't interrupt that class of hang.The "thread" method runs a separate watcher thread that kills the whole process via
os._exit()once the timeout elapses, regardless of what the main thread is doing -- it doesn't need the hung thread's cooperation. Under pytest-xdist (-n auto), that's one worker process; xdist detects the crash, reports the killed test as a failure, and reschedules the rest of that worker's queue onto a replacement worker, so the run doesn't hang.Trade-off: no clean teardown/reporting for whatever was running at the moment of the kill (
os._exit()skips it), and pytest-timeout's own docs note the crash can show up as a generic error rather than a labeled timeout in some report formats. Worth it to turn a silent 30-minute stall into a ~10-second, clearly-attributed failure with a stack trace of every thread at the point of the hang -- which is exactly the diagnostic info we're missing for the mystery hangs currently showing up on #578.