fix: observe terminated workers and report safe_terminate outcome - #431
Open
inchang-ing wants to merge 1 commit into
Open
inchang-ing wants to merge 1 commit into
inchang-ing wants to merge 1 commit into
Conversation
After the kill attempt, wait once more (bounded by the same timeout) for the termfunc worker, so a kill that releases it lets the worker be joined instead of abandoned while still running. Threads spawned via _thread.start_new_thread are never joined by threading._shutdown, so abandoned workers could outlive safe_terminate arbitrarily. safe_terminate now also returns the WorkerPool.waitall result instead of discarding it, letting Group.terminate and other callers tell whether all workers actually finished within the bounds. Fixes pytest-dev#429
This branch has not been deployed
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.
Fixes #429
Problem
safe_terminateabandons the worker thread runningtermfunconce the kill attempt is made, and reports success regardless:termkill, whentermreply.get(timeout=timeout)times out (OSError),killfunc()runs andtermkillreturns — nothing waits for thetermfuncworker afterwards. These workers are started via_thread.start_new_thread, sothreading._shutdown()never joins them; they can outlivesafe_terminatearbitrarily (observed via pytest-xdist teardown faulthandler dumps, as described in the issue).WorkerPool.waitall()returnsbool, butsafe_terminatediscarded it — soGroup.terminate()reports success even while abandoned workers are demonstrably still running.(The unbounded
reply.get()mentioned in the issue was already bounded by commit f700163.)Change
termkill, afterkillfunc(), wait once more for thetermfuncworker with the sametimeoutbound. A kill that releases the worker lets it be joined; a kill that does not is swallowed there and reported through the pool wait instead. Worst-case bound grows by at most onetimeoutinterval, still fully bounded.safe_terminatenow returnsWorkerPool.waitall(timeout=wait_timeout)(None→boolsignature change, backward compatible since callers previously had no return value) and its docstring documents the meaning:Falsemeans sometermfuncwas still running and its thread was abandoned.Group.terminatekeeps ignoring the value; pytest-xdist-style callers can now use it.Testing
test_safe_terminate_reports_kill_that_ignores: a no-opkillfuncleaves the termfunc blocked;safe_terminatenow returnsFalse(fails on pre-fix code — verified viagit stash).test_safe_terminate_joins_when_kill_releases: akillfuncthat releases the termfunc yieldsTruewith the worker observed finished immediately on return.testing/test_multi.py:26 passed, 11 skipped, 11 xfailed, 2 xpassed(the xpass/xskip are pre-existing markers).Disclosure
This PR was prepared with AI assistance (ZCode/GLM, orchestrated via WorkBuddy).