Repository navigation
Conversation
3ade8ac to
f751874
Compare
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
| gate.release(2); | ||
|
|
||
| ASSERT_TRUE(WaitUntil([&completed] { | ||
| return completed.load(std::memory_order_acquire) == 2; |
There was a problem hiding this comment.
By the time the second gate is reached, the old implementation has already fetched the wait sequence number, so posting a task here cannot detect a regression that moves the snapshot back to the end of the scan. force_empty_bucket_hint_once_for_test(0) actually skips the next scan. If the 200 ms anchor has expired by then, the timer thread will execute the anchor and then wait indefinitely on the empty heap, leaving the late task in the bucket.
| } | ||
| if (earlier) { | ||
| if (earlier || result.became_nonempty) { | ||
| futex_wake_private(&_nsignals, 1); |
There was a problem hiding this comment.
Adding the empty-bucket wakeup here invalidates the premise of periodic_wakeup_drains_buckets. The late task it submits can now wake the thread itself, so _npending == kN + 1 can hold even if the 50 ms wait limit is removed.
|
|
||
| EXPECT_EQ(kProducers * kTasksPerProducer, | ||
| scheduled.load(std::memory_order_relaxed)); | ||
| timer_thread.stop_and_join(); |
There was a problem hiding this comment.
scheduled only counts successful enqueues. The assertion will pass even if the consumer has not yet taken an entire batch of tasks, and the subsequent stop_and_join() will prevent the remaining tasks from being returned to the resource pool.

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.