Repository navigation
Conversation
songgaoye
marked this pull request as draft
November 24, 2026 13:49
songgaoye
force-pushed
the
fix/timer-thread-task-head-race
branch
from
November 24, 2026 13:56
3ade8ac to
f751874
Compare
songgaoye
marked this pull request as ready for review
November 24, 2026 13:57
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A critical concurrency bug remains in the timer fast path: a stale empty-hint read can miss a newly published bucket task and leave the timer asleep past its deadline.
1 open issue
What changed in this PR
This PR addresses a timer-bucket task-head data race while preserving the sparse-bucket fast path through an atomic task-presence hint.
Changes:
- Adds atomic task-presence publication and checking around bucket task consumption.
- Keeps task-list access protected by the bucket mutex.
- Adds regression tests for sparse buckets, delayed tasks, and concurrent scheduling.
| File | Description |
|---|---|
| test/bthread_timer_thread_unittest.cpp | Adds regression and concurrency tests covering sparse buckets, delayed tasks, and timer-bucket scheduling. |
| src/bthread/timer_thread.cpp | Adds the atomic bucket task hint and synchronizes task-list consumption and publication. |
Review effort: Lite
chenBright
reviewed
Nov 29, 2026
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.

What problem does this PR solve?
Issue Number: resolve
Problem Summary:
TimerThread::Bucket::consume_tasks()read_task_headwithout holdingBucket::_mutex, while concurrentschedule()calls update the same linked-listhead under the mutex. This is a C++ data race.
Locking every
consume_tasks()call fixes the race but regresses the sparse-bucketfast path: the timer thread scans every bucket, most of which are empty.
An atomic empty hint alone is not sufficient. A timer scan may observe a stale
empty hint after a producer has published a task. If that task is later than the
current heap minimum, the existing earlier-deadline wakeup path does not notify
the timer, allowing the task to remain in its bucket while the timer sleeps.
What is changed and the side effects?
Changed:
_has_taskshint to eachTimerThread::Bucket._task_headaccesses protected byBucket::_mutex._has_tasksonly as a sparse-bucket fast-path hint.including tasks later than the current heap minimum.
_nsignalsbefore the timer polls buckets, so a publication afterthe snapshot prevents the subsequent futex wait from sleeping on stale state.
deterministic scan-to-wait publication window, including a simulated stale
empty-hint observation.
Side effects:
Performance effects:
Preserves the sparse empty-bucket fast path without reading
_task_headoutside its mutex. Empty-to-nonempty bucket transitions perform one additional
timer-sequence publication and wake attempt; subsequent insertions into an
already nonempty bucket retain the existing low-contention path.
Breaking backward compatibility:
None.
Check List:
bthread_timer_thread_unittestpasses.