Repository navigation
Fix Timer.ensure_started race on concurrent first use - #11153
Open
TINGyu123644 wants to merge 3 commits into
Open
TINGyu123644 wants to merge 3 commits into
TINGyu123644 wants to merge 3 commits into
Conversation
Guard the check-then-start in Timer.ensure_started with the existing self.mutex. Two threads first touching the shared timer could both pass `if not self.running and not self.is_alive()` (running is set asynchronously by the new thread in run()) and each call Thread.start(), raising "thread already started" and potentially booting two run loops. All non-interactive contexts preserve the original behavior; on_start (default None for timer2.Timer callers) runs under the same lock. Fixes celery#11151 Signed-off-by: TINGyu123644 <[email protected]>
auvipy
self-requested a review
November 23, 2026 09:14
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11153 +/- ##
==========================================
- Coverage 91.33% 91.32% -0.01%
==========================================
Files 153 153
Lines 21841 21842 +1
Branches 2712 2712
==========================================
Hits 19948 19948
Misses 1593 1593
- Partials 300 301 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
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 #11151
Problem
Timer.ensure_started()does an unguarded check-then-start:self.runningis set asynchronously by the new thread insiderun(), so two threads that first touch the same sharedTimerconcurrently (both via_do_enter) can both pass the guard and callThread.start()twice. The loser getsRuntimeError: thread already startedout of task-message handling, and in the worst interleaving tworun()loops are bootstrapped over oneSchedule. There is a worker-wide shared timer instance, so ETA scheduling from multiple threads can hit this.Fix
Guard the check-and-start with the existing
self.mutex._do_entercallsensure_started()before acquiring the mutex, so no caller holds it at that point → no self-deadlock.on_startdefaults toNonefor alltimer2.Timercallers in the tree, so no callback re-enters the lock.start()only launches the thread (it does not run the loop inline), so the new thread simply waits on the mutex until the lock is released.Verification
ensure_started) against the unmodified source: 105/120 trials raisedRuntimeError/KeyError; against the patched source: 0/120 trials. No deadlock — every trial'sstop()completed.t/unit/utils/test_timer2.py::test_ensure_started_concurrent_first_use(pure addition, +33/-0): fails before the fix (15/20 rounds) and passes after (0/20).test_ensure_started_not_startedsemantics unchanged (guarded block still skipsstart()whenrunningis set).Diff:
celery/utils/timer2.py+8/-4,t/unit/utils/test_timer2.py+33/-0.AI assistance
This patch and its regression test were prepared with AI assistance (WorkBuddy / Tencent Hunyuan Hy). I have read and understand the change and can defend it.