Fix spurious "thread death" TOCTOU race in python_worker#8
Merged
Merged
Conversation
In Worker._process_input(), non-main tasks assigned task._thread before calling start(). The janitor thread (_cleanup_threads) flags any task where task._thread is set and the thread is not alive. A just-constructed, not-yet-started thread is not alive, so if the janitor's 0.05s poll landed in the window between Thread() construction and start(), it would wrongly fail the task with "thread death" even though the thread was about to run fine. Assign task._thread only after start() returns, closing the window. Also add test_thread_death_stress, which floods the worker with many concurrent tiny tasks (none of which can legitimately die) to surface the race. It failed reliably before the fix and passes consistently after. Fixes apposed/appose#15. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
b3a97fa to
fbc25e0
Compare
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.
Summary
Fixes the sporadic spurious "thread death" task failures reported in apposed/appose#15.
Root cause
In
Worker._process_input(), non-main tasks were wired up as:task._threadis assigned beforestart()is called. The janitor thread_cleanup_threads()flags any task wheretask._thread is not Noneandnot task._thread.is_alive(). A just-constructed, not-yet-started thread is not alive, so if the janitor's 0.05s poll lands in the window betweenThread()construction andstart(), it wrongly fails the task with "thread death" — even though the thread is about to run fine.Fix
Build the thread into a local, start it, and assign
task._threadonly afterstart()returns, so the janitor can never observe a referenced thread that isn't yet alive:Reproduction & verification
Added
test_thread_death_stress, which floods the worker with 16 threads × 200 tiny tasks (none of which can legitimately die), so any "thread death" is the bug.Full
tests/test_service.pyPython suite passes with no regressions, includingtest_python_sys_exit(a genuinesys.exitin a task is now caught and reported asSystemExit: 123, which the test allows).