Skip to content

Fix macOS futex wake accounting with per-waiter notifications - #3637

Open
chenBright wants to merge 3 commits into
apache:masterfrom
chenBright:fix_sys_futex
Open

chenBright wants to merge 3 commits into
apache:masterfrom
chenBright:fix_sys_futex

Conversation

@chenBright

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

On macOS, repeated futex_wake_private() calls can count an already notified waiter
again before it resumes. Callers such as TaskControl::signal_task() may consequently
stop notifying other parking lots prematurely, leaving idle workers asleep while tasks
remain queued

What is changed and the side effects?

Changed:

  • Replace aggregate waiter counting with an intrusive queue of unnotified waiters, each
    owning a condition variable and notification flag.
  • Remove each waiter from the queue when notified, preventing subsequent wake calls
    from counting it again.
  • Remove timed-out waiters individually and retry spurious wakeups without extending
    the original deadline.
  • Honor notifications that win the mutex race with a timeout.

Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

@chenBright
chenBright requested a balanced review from Copilot October 21, 2026 10:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

5 open issues
What changed in this PR

This PR fixes macOS simulated futex wake accounting so repeated futex_wake_private() calls cannot “re-count” a waiter that was already notified but hasn’t resumed yet, preventing premature cessation of wakeups in higher-level schedulers.

Changes:

  • Replace aggregate waiter counting with a per-waiter intrusive queue and per-waiter condition variables.
  • Ensure notified waiters are removed from the queue at wake time; timed-out waiters remove themselves.
  • Add regression tests covering double-count prevention, timeout interactions, and non-queued mismatched waits.
File Description
test/​bthread_futex_unittest.cpp Adds regression tests validating one-time wake accounting and timeout/non-queue behaviors.
src/​bthread/​sys_futex.cpp Reworks simulated futex internals to track unnotified waiters explicitly and avoid double-counting.

Review effort: Lite

Comment thread test/bthread_futex_unittest.cpp Outdated
Comment thread test/bthread_futex_unittest.cpp Outdated
Comment thread test/bthread_futex_unittest.cpp
Comment thread test/bthread_futex_unittest.cpp Outdated
Comment thread test/bthread_futex_unittest.cpp Outdated
@chenBright
chenBright requested a balanced review from Copilot October 21, 2026 11:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

7 open issues
5 resolved since last review

Review effort: Lite

Comment thread src/bthread/sys_futex.cpp
Comment thread src/bthread/sys_futex.cpp
Comment thread test/bthread_futex_unittest.cpp Outdated
Comment thread test/bthread_futex_unittest.cpp Outdated
Comment thread test/bthread_futex_unittest.cpp
Comment thread test/bthread_futex_unittest.cpp
Comment thread test/bthread_futex_unittest.cpp
@chenBright
chenBright requested a balanced review from Copilot October 21, 2026 12:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/bthread/sys_futex.cpp
Comment thread src/bthread/sys_futex.cpp
Comment thread test/bthread_futex_unittest.cpp
@chenBright

Copy link
Copy Markdown
Contributor Author

@wwbmmm PTAL.

The remaining Copilot review comments have been evaluated and are either false positives or non-blocking suggestions that we will not adopt in this bugfix. No further code changes are needed. All 65 relevant tests passed, and this PR is ready for maintainer review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants