Repository navigation
Fibers: fix CondVar timeout race and simplify remote wake - #681
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Notification and timeout claims util/fibers/detail/fiber_interface.h, util/fibers/detail/fiber_interface.cc, util/fibers/detail/wait_queue.h, util/fibers/detail/wait_queue.cc |
FiberInterface adds claimable wait states for notification and timeout. Waiter::Notify reports whether notification succeeded, and NotifyOne skips waiters whose notification fails. |
Timed wait integration util/fibers/synchronization.h, util/fibers/synchronization.cc, util/fibers/detail/scheduler.cc, util/fibers/fibers_test.cc |
CondVarAny::wait_until uses a claimable waiter and resolves queue cleanup after reacquiring the lock. ProcessSleep skips wake-up when a notifier claimed the wait. Tests cover an expired waiter followed by an untimed waiter and repeated notifications during timed waits. |
Remote activation processing util/fibers/detail/fiber_interface.h, util/fibers/detail/fiber_interface.cc, util/fibers/detail/scheduler.h, util/fibers/detail/scheduler.cc, util/fibers/epoll_proactor.cc, util/fibers/uring_proactor.cc, util/fibers/synchronization.cc |
AwaitRemoteWake replaces PullMyselfFromRemoteReadyQueue. ProcessRemoteReady no longer takes an active fiber or returns a result. Both proactor main loops call the updated method. EventCount::wait_until calls AwaitRemoteWake after unlocking. |
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Suggested reviewers: kostasrim
Merge Risk: 
Minimal · previous review 1e907
-
No actionable merge-blocking defect was established in the inspected wait and notification paths. The change is mergeable after normal checks.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passedFull details: Docstring Coverage
-
Explanation
Docstring coverage is 25.93% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 27 functions across 11 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
Finishing Touches
-
Generate docstrings
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
PR Summary by QodoFix fiber condition-variable timeout race and simplify remote wakes
AI Description
Diagram
High-Level Assessment
Files changed (9)
|
Code Review by Qodo
1.
|
🤖 Augment PR SummarySummary: Fixes condition-variable timeout races with cross-thread notifications that could cause duplicate scheduling or wait-list corruption.
🤖 Was this summary useful? React with 👍 or 👎 |
There was a problem hiding this comment.
🟡 Changes recommended
Both notification methods call FiberActive() even when the queue is empty, unnecessarily initializing fiber infrastructure and retaining a 32 KiB dispatcher stack until thread exit.
1 open issue
What changed in this PR
This PR fixes a condition-variable timeout race in Helio’s fiber synchronization by handling pending remote notifications before re-locking the caller’s mutex.
Changes:
- Protect condition-variable wait queues with an internal spinlock and clean up timed-out waits before re-locking.
- Replace inline remote-queue draining with dispatcher-managed wake handling.
- Add a regression test for concurrent condition-variable timeouts and notifications.
| File | Description |
|---|---|
| util/fibers/uring_proactor.cc | Update the remote-ready processing call. |
| util/fibers/synchronization.h | Guard condition-variable queues and reorder timeout cleanup. |
| util/fibers/synchronization.cc | Update timeout cleanup to await pending remote wakes. |
| util/fibers/fibers_test.cc | Add the condition-variable timeout race regression test. |
| util/fibers/epoll_proactor.cc | Update the remote-ready processing call. |
| util/fibers/detail/scheduler.h | Simplify the remote-ready processing interface. |
| util/fibers/detail/scheduler.cc | Simplify remote scheduling and dispatcher queue processing. |
| util/fibers/detail/fiber_interface.h | Declare and document AwaitRemoteWake(). |
| util/fibers/detail/fiber_interface.cc | Implement wake awaiting and adjust the scheduler assertion. |
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
Changes to cross-thread wake consumption and intrusive waiter lifetimes require human validation of timeout, notification, and relock invariants under stress across the default dispatcher, epoll, and io_uring.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
- When the remote check fires, Preempt() switches to the dispatcher again instead of pulling remote fibers itself. It sets round_robin_run_.end_round, so the dispatcher ends the round and returns to its loop. There it pulls the remote queue and also submits and reaps I/O before it starts the next round. Pulling in Preempt() alone left I/O completions waiting until the round ended. - Only the dispatcher pulls remote fibers again, so the self-pull handling in Preempt() and the RemoteWakeBeforeSuspend test are gone. The comments on ProcessRemoteReady() and AwaitRemoteWake() are back to their #681 wording. - Config::remote_check_cycles replaces remote_check_usec, so Preempt() compares without converting units. The constructor converts the flag once. Before CycleClock::InitOnce(), the conversion gives 0, which disables the check. - round_robin_run_.yields is now uint32_t, and it shares a word with end_round. fibers_test passes with --scheduler_remote_queue_check_usec=0 and =1. ctest -L CI passes, also with the flag's default set to 1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Changes to cross-thread timeout/wakeup and queue ownership invariants require human concurrency review and runtime race/stress validation of contested interleavings unavailable in this source-only review.
No open issues 🎉
Review effort: Balanced
1e90704 to
0f33e6c
Compare
…o fibers-remote-check Conflicts were resolved by taking the squashed #681 (0f33e6c) and reapplying this branch's own changes (scheduler remote check) on top. fibers_test passes 156/156 with --scheduler_remote_queue_check_usec at 0 and 1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Roman Gershman <romange@gmail.com>
0f33e6c to
f6b1a9f
Compare
- Fix a race in fb2::CondVar (CondVarAny + fb2::Mutex). When a timed wait timed out while a notifier on another thread detached it, the waiter's relock could suspend in Mutex::lock() with the notifier's wake still queued. That either scheduled the fiber remotely twice (DFATAL) or resumed it inside Mutex::lock() and destroyed a still-linked waiter. A local fiber holding the mutex across the timeout could cause the same collision. - The timeout and the notifier now race to claim a timed wait, so only one of them wakes the fiber. CondVarAny::wait_until uses CreateClaimableWaiter(); ProcessSleep() and the notifier race on a per-fiber wait_claim_ byte, and WaitQueue::NotifyOne() skips waiters whose wait timed out. A notifier on the waiter's thread uses a relaxed load and store, so local wait and notify do no atomic read-modify-write; only remote notifiers and timeouts use a CAS. - wait_claim_ fills the padding byte next to flags_, so FiberInterface does not grow. It is separate from flags_, which joiners on other threads modify at any time. - notify_one()/notify_all() must be called under the mutex when waiters may run on other threads; Barrier::Cancel() now notifies under mtx_. CondVarAny notify skips FiberActive() when nobody waits. - Replace PullMyselfFromRemoteReadyQueue with AwaitRemoteWake(), used by EventCount::wait_until: it suspends until the dispatcher pulls the fiber from the remote queue. ProcessRemoteReady() loses its parameter, its return value and the "UB state" recovery, ScheduleFromRemote drops its unreachable revert branch, and ActivateSameThread uses DCHECK. - Add ProactorTest.CondVarTimeout, which failed in 3 of 4 runs before the fix, and FiberTest.CondVarSkipsTimedOutWaiter. fibers_test passes, mutation checks confirm the tests cover the claim, and the condvar and timeout tests passed 87 stress iterations. Signed-off-by: Roman Gershman <romange@gmail.com>
f6b1a9f to
f6cce89
Compare
dranikpg
left a comment
There was a problem hiding this comment.
Actually simplifies lots of stuff
…-remote-check * 'master' of https://github.com/romange/helio: Fibers: fix CondVar timeout race and simplify remote wake (#681) # Conflicts: # util/fibers/detail/scheduler.cc
A fiber woken from another thread waits in the remote ready queue until the dispatcher's turn comes. The dispatcher runs only after every fiber that was ready ahead of it, so under load a cross-thread hand-off can wait a whole round, and so can I/O completions. - Add Scheduler::Config::remote_check_cycles. A fiber may switch out at least that long after remote fibers were last pulled, while more are waiting. Then Preempt() takes the dispatcher out of the ready queue, sets round_robin_run_.end_round and switches to it. The dispatcher ends the round and returns to its loop, which pulls the remote queue and processes I/O completions and submissions before the next round. The dispatcher's pull resets the timer, so this happens at most once per interval. - The setting is per scheduler and is changed with UpdateConfig(), in cycles, so Preempt() compares it without converting units. New schedulers take their default from --scheduler_remote_queue_check_usec, converted once in the constructor. Before CycleClock::InitOnce(), that conversion gives 0, which disables the check. 0, the default, keeps the current behavior. - round_robin_run_.yields is now uint32_t. - Add FiberTest.RemoteWakeCheckInterval, which checks the run order with the check off and on. - Restore the ScheduleFromRemote comment that explains why kScheduleRemote can not be set there. Merging #679 and #681 brought back an older comment saying the call is made under a WaitQueue lock, which ParkNode wakes are not. fibers_test passes with --scheduler_remote_queue_check_usec=0 and =1. ctest -L CI passes, also with the flag's default set to 1. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Roman Gershman <romange@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve conflicts with the rebased base, which adds wake-to-front (#679) and the CondVar timeout fix (#681): - scheduler.{h,cc}: keep poll_min/poll_max/poll_min_events, ShouldPollEarly() and the direct handoff in place of remote_check_cycles; take the base's ScheduleFromRemote(to_front) and kWakeToFront handling. - fiber_interface.{h,cc}: take the base's versions. - fibers_test.cc: keep RemoteWakeCheckInterval on poll_min_cycles. fibers_test and fiber_socket_test pass in debug and release, also with --scheduler_poll_min_usec=0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A fiber woken from another thread waits in the remote ready queue until the dispatcher's turn comes. The dispatcher runs only after every fiber that was ready ahead of it, so under load a cross-thread hand-off can wait a whole round, and so can I/O completions. - Add Scheduler::Config::remote_check_cycles. A fiber may switch out at least that long after remote fibers were last pulled, while more are waiting. Then Preempt() calls EndRoundEarly(), which takes the dispatcher out of the ready queue, sets round_robin_run_.end_round and switches to it. The dispatcher ends the round and returns to its loop, which pulls the remote queue and processes I/O completions and submissions before the next round. The dispatcher's pull resets the timer, so this happens at most once per interval. - EndRoundEarly() is a separate function because Preempt() runs on the stack of every fiber that switches out. Inline, the branch grew Preempt()'s frame from 1216 to 1824 bytes in the unoptimized ASAN build, and FiberTest.StackSize overflowed its 8000-byte fiber stack on arm64. Now the frame is 1280 bytes. - The setting is per scheduler and is changed with UpdateConfig(), in cycles, so Preempt() compares it without converting units. New schedulers take their default from --scheduler_remote_queue_check_usec, converted once in the constructor. Before CycleClock::InitOnce(), that conversion gives 0, which disables the check. 0, the default, keeps the current behavior. - Add Scheduler::config(), a read-only getter. - round_robin_run_.yields is now uint32_t. - Add FiberTest.RemoteWakeCheckInterval, which checks the run order with the check off and on, and restores the scheduler's previous interval. - Restore the ScheduleFromRemote comment that explains why kScheduleRemote can not be set there. Merging #679 and #681 brought back an older comment saying the call is made under a WaitQueue lock, which ParkNode wakes are not. - Add a TODO in InitStackBottom: since #680, stack pages are faulted in on first use, which may cost stack locality. fibers_test passes with --scheduler_remote_queue_check_usec=0 and =1, in the debug build and in an ASAN build configured like CI. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Roman Gershman <romange@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A fiber woken from another thread waits in the remote ready queue until the dispatcher's turn comes. The dispatcher runs only after every fiber that was ready ahead of it, so under load a cross-thread hand-off can wait a whole round, and so can I/O completions. - Add Scheduler::Config::remote_check_cycles. A fiber may switch out at least that long after remote fibers were last pulled, while more are waiting. Then Preempt() calls EndRoundEarly(), which takes the dispatcher out of the ready queue, sets round_robin_run_.end_round and switches to it. The dispatcher ends the round and returns to its loop, which pulls the remote queue and processes I/O completions and submissions before the next round. The dispatcher's pull resets the timer, so this happens at most once per interval. - EndRoundEarly() is a separate function because Preempt() runs on the stack of every fiber that switches out. Inline, the branch grew Preempt()'s frame from 1216 to 1824 bytes in the unoptimized ASAN build, and FiberTest.StackSize overflowed its 8000-byte fiber stack on arm64. Now the frame is 1280 bytes. - The setting is per scheduler and is changed with UpdateConfig(), in cycles, so Preempt() compares it without converting units. New schedulers take their default from --scheduler_remote_queue_check_usec, converted once in the constructor. Before CycleClock::InitOnce(), that conversion gives 0, which disables the check. 0, the default, keeps the current behavior. - Add Scheduler::config(), a read-only getter. - round_robin_run_.yields is now uint32_t. - Add FiberTest.RemoteWakeCheckInterval, which checks the run order with the check off and on, and restores the scheduler's previous interval. - Restore the ScheduleFromRemote comment that explains why kScheduleRemote can not be set there. Merging #679 and #681 brought back an older comment saying the call is made under a WaitQueue lock, which ParkNode wakes are not. - Add a TODO in InitStackBottom: since #680, stack pages are faulted in on first use, which may cost stack locality. fibers_test passes with --scheduler_remote_queue_check_usec=0 and =1, in the debug build and in an ASAN build configured like CI. Signed-off-by: Roman Gershman <romange@gmail.com>
A fiber woken from another thread waits in the remote ready queue until the dispatcher's turn comes. The dispatcher runs only after every fiber that was ready ahead of it, so under load a cross-thread hand-off can wait a whole round, and so can I/O completions. - Add Scheduler::Config::remote_check_cycles. A fiber may switch out at least that long after remote fibers were last pulled, while more are waiting. Then Preempt() calls EndRoundEarly(), which takes the dispatcher out of the ready queue, sets round_robin_run_.end_round and switches to it. The dispatcher ends the round and returns to its loop, which pulls the remote queue and processes I/O completions and submissions before the next round. The dispatcher's pull resets the timer, so this happens at most once per interval. - EndRoundEarly() is a separate function because Preempt() runs on the stack of every fiber that switches out. Inline, the branch grew Preempt()'s frame from 1216 to 1824 bytes in the unoptimized ASAN build, and FiberTest.StackSize overflowed its 8000-byte fiber stack on arm64. Now the frame is 1280 bytes. - The setting is per scheduler and is changed with UpdateConfig(), in cycles, so Preempt() compares it without converting units. New schedulers take their default from --scheduler_remote_queue_check_usec, converted once in the constructor. Before CycleClock::InitOnce(), that conversion gives 0, which disables the check. 0, the default, keeps the current behavior. - Add Scheduler::config(), a read-only getter. - round_robin_run_.yields is now uint32_t. - Add FiberTest.RemoteWakeCheckInterval, which checks the run order with the check off and on, and restores the scheduler's previous interval. - Restore the ScheduleFromRemote comment that explains why kScheduleRemote can not be set there. Merging #679 and #681 brought back an older comment saying the call is made under a WaitQueue lock, which ParkNode wakes are not. - Add a TODO in InitStackBottom: since #680, stack pages are faulted in on first use, which may cost stack locality. - Finish DNS callback result writes before signaling a copied Done object, preventing the requesting fiber from destroying callback arguments while they are still in use. Done also handles synchronous c-ares completion. - Snapshot DNS socket descriptors before processing responses, since c-ares can insert sockets and rehash the socket-state map during callbacks. Skip sockets removed while earlier responses are processed. - Free c-ares address-info results and add an immediate-completion regression test for IPv4, IPv6 and synchronous lookup failure. fibers_test passes with --scheduler_remote_queue_check_usec=0 and =1, in the debug build and in an ASAN build configured like CI. The DNS regression and all 29 CI tests pass in the debug build. Signed-off-by: Roman Gershman <romange@gmail.com>
The dispatcher polls for remote wakes, brief tasks and I/O events between rounds, and a round lasts until every fiber that was ready when it started switches out. Under load, a cross-thread hand-off, a brief task or an I/O completion can wait a whole round. - Add Scheduler::Config::poll_min_cycles, poll_max_cycles and poll_min_events, with the flags --scheduler_poll_min_usec (0), --scheduler_poll_max_usec (200) and --scheduler_poll_min_events (16). A fiber that switches out at least poll_min_cycles after the last poll ends the round if remotely woken fibers or brief tasks wait, if at least poll_min_events I/O events wait, or if any I/O event waits and poll_max_cycles passed since the last poll. --scheduler_poll_min_usec defaults to 0, which keeps the current behavior. - To end the round, Preempt() calls EndRoundEarly(), which takes the dispatcher out of the ready queue, sets round_robin_run_.end_round and switches to it. The dispatcher returns to its loop, which pulls remote wakes and processes tasks and I/O before the next round. ProcessRemoteReady() resets the timer. Preempt() does this only while RunReadyFibersInternal() runs a normal round, where the dispatcher waits for its turn in the ready queue. - Add Scheduler::ShouldPollEarly() and DispatchPolicy::GetPendingWork(). ProactorBase reports queued tasks. The uring proactor adds unsubmitted SQEs, ready CQEs, and one event if the kernel flags pending task work or a CQ overflow; these are memory loads, without a syscall. epoll can not count ready events without epoll_wait, so it reports one; unless poll_min_events is 1, it polls early at most once per poll_max_cycles. - EndRoundEarly() and ShouldPollEarly() are separate functions because Preempt() runs on the stack of every fiber that switches out. Inline, the early-switch code grew Preempt()'s frame from 1216 to 1824 bytes in the unoptimized ASAN build, and FiberTest.StackSize overflowed its 8000-byte fiber stack on arm64. Now the frame is 1248 bytes. - The settings are per scheduler and are changed with UpdateConfig(), in cycles. New schedulers take their defaults from the flags, converted once in the constructor. Before CycleClock::InitOnce(), poll_min_cycles converts to 0, which disables early polls. - Add Scheduler::config(), a read-only getter. - round_robin_run_.yields is now uint32_t. - Add FiberTest.RemoteWakeCheckInterval, which checks the run order with early polls off and on, and restores the scheduler's previous setting. - Restore the ScheduleFromRemote comment that explains why kScheduleRemote can not be set there. Merging #679 and #681 brought back an older comment saying the call is made under a WaitQueue lock, which ParkNode wakes are not. - Add two TODOs. In InitStackBottom: since #680, stack pages are faulted in on first use, which may cost stack locality. In the uring loop: under load, sleeping fibers wake only after completions, at most once per 2.5ms. fibers_test passes with --scheduler_poll_min_usec=0 and =1, in the debug build and in an ASAN build configured like CI. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Roman Gershman <romange@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The dispatcher polls for remote wakes, brief tasks and I/O events between rounds, and a round lasts until every fiber that was ready when it started switches out. Under load, a cross-thread hand-off, a brief task or an I/O completion can wait a whole round. - Add Scheduler::Config::poll_min_cycles, poll_max_cycles and poll_min_events, with the flags --scheduler_poll_min_usec (0), --scheduler_poll_max_usec (200) and --scheduler_poll_min_events (16). A fiber that switches out at least poll_min_cycles after the last poll ends the round if remotely woken fibers or brief tasks wait, if at least poll_min_events I/O events wait, or if any I/O event waits and poll_max_cycles passed since the last poll. --scheduler_poll_min_usec defaults to 0, which keeps the current behavior. - To end the round, Preempt() calls EndRoundEarly(), which takes the dispatcher out of the ready queue, sets round_robin_run_.end_round and switches to it. The dispatcher returns to its loop, which pulls remote wakes and processes tasks and I/O before the next round. ProcessRemoteReady() resets the timer. Preempt() does this only while RunReadyFibersInternal() runs a normal round, where the dispatcher waits for its turn in the ready queue. - Add Scheduler::ShouldPollEarly() and DispatchPolicy::GetPendingWork(). ProactorBase reports queued tasks. The uring proactor adds unsubmitted SQEs, ready CQEs, and one event if the kernel flags pending task work or a CQ overflow; these are memory loads, without a syscall. epoll can not count ready events without epoll_wait, so it reports one; unless poll_min_events is 1, it polls early at most once per poll_max_cycles. - EndRoundEarly() and ShouldPollEarly() are separate functions because Preempt() runs on the stack of every fiber that switches out. Inline, the early-switch code grew Preempt()'s frame from 1216 to 1824 bytes in the unoptimized ASAN build, and FiberTest.StackSize overflowed its 8000-byte fiber stack on arm64. Now the frame is 1248 bytes. - The settings are per scheduler and are changed with UpdateConfig(), in cycles. New schedulers take their defaults from the flags, converted once in the constructor. Before CycleClock::InitOnce(), poll_min_cycles converts to 0, which disables early polls. - Add Scheduler::config(), a read-only getter. - round_robin_run_.yields is now uint32_t. - Add FiberTest.RemoteWakeCheckInterval, which checks the run order with early polls off and on, and restores the scheduler's previous setting. - Restore the ScheduleFromRemote comment that explains why kScheduleRemote can not be set there. Merging #679 and #681 brought back an older comment saying the call is made under a WaitQueue lock, which ParkNode wakes are not. - Add two TODOs. In InitStackBottom: since #680, stack pages are faulted in on first use, which may cost stack locality. In the uring loop: under load, sleeping fibers wake only after completions, at most once per 2.5ms. fibers_test passes with --scheduler_poll_min_usec=0 and =1, in the debug build and in an ASAN build configured like CI. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Roman Gershman <romange@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…682) * Fibers: optionally end the round early to poll remote wakes and I/O The dispatcher polls for remote wakes, brief tasks and I/O events between rounds, and a round lasts until every fiber that was ready when it started switches out. Under load, a cross-thread hand-off, a brief task or an I/O completion can wait a whole round. - Add Scheduler::Config::poll_min_cycles, poll_max_cycles and poll_min_events, with the flags --scheduler_poll_min_usec (0), --scheduler_poll_max_usec (200) and --scheduler_poll_min_events (16). A fiber that switches out at least poll_min_cycles after the last poll ends the round if remotely woken fibers or brief tasks wait, if at least poll_min_events I/O events wait, or if any I/O event waits and poll_max_cycles passed since the last poll. --scheduler_poll_min_usec defaults to 0, which keeps the current behavior. - To end the round, Preempt() calls EndRoundEarly(), which takes the dispatcher out of the ready queue, sets round_robin_run_.end_round and switches to it. The dispatcher returns to its loop, which pulls remote wakes and processes tasks and I/O before the next round. ProcessRemoteReady() resets the timer. Preempt() does this only while RunReadyFibersInternal() runs a normal round, where the dispatcher waits for its turn in the ready queue. - Add Scheduler::ShouldPollEarly() and DispatchPolicy::GetPendingWork(). ProactorBase reports queued tasks. The uring proactor adds unsubmitted SQEs, ready CQEs, and one event if the kernel flags pending task work or a CQ overflow; these are memory loads, without a syscall. epoll can not count ready events without epoll_wait, so it reports one; unless poll_min_events is 1, it polls early at most once per poll_max_cycles. - EndRoundEarly() and ShouldPollEarly() are separate functions because Preempt() runs on the stack of every fiber that switches out. Inline, the early-switch code grew Preempt()'s frame from 1216 to 1824 bytes in the unoptimized ASAN build, and FiberTest.StackSize overflowed its 8000-byte fiber stack on arm64. Now the frame is 1248 bytes. - The settings are per scheduler and are changed with UpdateConfig(), in cycles. New schedulers take their defaults from the flags, converted once in the constructor. Before CycleClock::InitOnce(), poll_min_cycles converts to 0, which disables early polls. - Add Scheduler::config(), a read-only getter. - round_robin_run_.yields is now uint32_t. - Add FiberTest.RemoteWakeCheckInterval, which checks the run order with early polls off and on, and restores the scheduler's previous setting. - Restore the ScheduleFromRemote comment that explains why kScheduleRemote can not be set there. Merging #679 and #681 brought back an older comment saying the call is made under a WaitQueue lock, which ParkNode wakes are not. - Add two TODOs. In InitStackBottom: since #680, stack pages are faulted in on first use, which may cost stack locality. In the uring loop: under load, sleeping fibers wake only after completions, at most once per 2.5ms. fibers_test passes with --scheduler_poll_min_usec=0 and =1, in the debug build and in an ASAN build configured like CI. - Gate early-poll work inspection in Preempt when disabled or not yet due. - Reuse the scheduling timestamp for ready-queue and switch accounting. - Refresh timestamps after suspend hooks to retain runtime attribution. - Cover non-due polling and suspend-hook accounting; all 29 CI tests and the full fiber suite with early polling enabled pass. - Separate queued submissions from completion events and flush them at the configured minimum interval without switching to the dispatcher. - Keep completion callbacks on the dispatcher and fall back to it when submissions are partial or the ring is busy. - Share submission handling across workers, full SQs, and the dispatcher, preserving deferred kernel task work and submission accounting. - Cover disabled, not-yet-due, and batched worker flushing; all 29 CI tests and enabled-flushing fiber, socket, and file suites pass. - Add opt-in yield-ring and response-handoff benchmarks, documenting that handoff timing includes responses observed without suspending. Both benchmarks run successfully with early polling disabled and enabled. Signed-off-by: Roman Gershman <roman@dragonflydb.io> --------- Signed-off-by: Roman Gershman <romange@gmail.com> Signed-off-by: Roman Gershman <roman@dragonflydb.io> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Summary
Fixes a race in
fb2::CondVar(CondVarAny+fb2::Mutex) when a timed wait races a notify from another thread. The race already exists on master:Mutex::lock()while that wake is still queued.From there, two things can happen:
ScheduleFromRemotefires the "Already scheduled remotely"DFATAL.Mutex::lock()while its waiter is still linked in the mutex's wait list, then destroys that linked waiter. In release builds this corrupts the list.The same collision can happen on a single thread when a local fiber holds the mutex across the waiter's timeout and then notifies.
Changes
CondVarAny::wait_untilcreates its waiter withCreateClaimableWaiter().ProcessSleep()and the notifier that pops the waiter race on a per-fiberwait_claim_byte.lt.lock()can suspend safely. A notifier that finds the timed-out waiter skips it, andWaitQueue::NotifyOne()moves on to the next one.ProcessSleep()only drops the fiber from the sleep queue, and the notifier's wake resumes it.ProcessSleep()runs on that thread too. Only remote notifiers and timeouts use a compare-and-swap.wait(), notifies with no waiters, and local notifies do no atomic read-modify-write.wait_claim_fills the padding byte next toflags_, soFiberInterfacedoes not grow. It is separate fromflags_because joiners on other threads modifyflags_at any time.notify_*must still be called under the caller's lock when waiters may run on other threads.Barrier::Cancel()now notifies undermtx_.PullMyselfFromRemoteReadyQueueis replaced byAwaitRemoteWake(), whichEventCount::wait_untiluses. It suspends until the dispatcher pulls the fiber from the remote queue, instead of draining that queue inline. As a result:ProcessRemoteReady()loses itsactiveparameter, its return value and the "UB state" recovery (retries, stack traces,LOG(FATAL)).EventCount::wait_untilloses itsclear_remotebookkeeping.ScheduleFromRemotedrops the unreachableIsScheduledRemotely()"revert the flags" branch.kScheduleRemotestays set from push until pull, so whoever wins the bit always finds the fiber out of the queue. The bit check itself stays as a guard against corrupting the intrusive queue.ActivateSameThreadusesDCHECKfor the same-scheduler check.Test plan
ProactorTest.CondVarTimeout(fb2::Mutex+fb2::CondVar, notifier on another thread) fails in 3 of 4 runs on master's code and passes with this change across epoll and io_uring.FiberTest.CondVarSkipsTimedOutWaiter: a local notifier holds the mutex across one waiter's timeout, thennotify_one()must skip it and wake the next waiter.CondVarSkipsTimedOutWaiter; letting the timeout wake the fiber after a remote notifier claimed the wait abortsCondVarTimeouton every run.fibers_testpasses (155 tests). The condvar, timeout, mutex and barrier tests passed 87 repeated iterations with no failures.ctest -L CIwas not rerun after the claim rework. On the earlier spinlock version it passed exceptIoTest.ProcReader, which flakes under-j4load (major page faults) and passes alone.🤖 Generated with Claude Code