Skip to content

Conversation

@frostyplanet
Copy link
Contributor

Motivation

When running with Miri, or dated arch like POWER, load(Acquire) might return old value.
But in the branch "COMPLETE || NOTIFIED" which returns None directly, might lead to a deadlock in a race condition.

Example code of deadlock report under Miri can be found in this issue: #7589

Solution

  1. In this PR, use additional CAS with the same value to ensure a happen-before relationship. It will not be necessary on x86, but it's the smallest change, chances that waking the waker concurrently or waking the waker after it's complete might be rare.

  2. An alternative solution might be changing the load to SeqCst, which will prevent Miri from reading old value,
    but I am not sure what will happen when fetch_update_action() go to the branch a second time.

@github-actions github-actions bot added R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR labels Sep 15, 2025
@frostyplanet
Copy link
Contributor Author

frostyplanet commented Sep 15, 2025

Is it ok to use #cfg[(target_arch = x86_64)] to skip additional instruction in this case?

@Darksonn
Copy link
Contributor

No. This is why I said the hardware is not relevant. Even on x86, compiler optimizations are allowed to have the same effect as what you saw on ARM. We need a release op no matter what.

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime labels Sep 15, 2025
load(Acquire) might return old value. Previously, when reading
waker state got is_notified(), it would return None directly,
which could lead to a deadlock in race condition.

To prevent this, use an additional CAS with the same value, to ensure we have a Release
store pairing with Acquire load in transition_to_running().

tokio-rs#7589
@frostyplanet
Copy link
Contributor Author

frostyplanet commented Sep 15, 2025

The comment has been fixed

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Thanks.

@Darksonn Darksonn merged commit 67869be into tokio-rs:master Sep 18, 2025
94 checks passed
@Darksonn Darksonn mentioned this pull request Oct 14, 2025
@ADD-SP ADD-SP mentioned this pull request Oct 14, 2025
ADD-SP pushed a commit to ADD-SP/tokio that referenced this pull request Oct 14, 2025
ADD-SP pushed a commit to ADD-SP/tokio that referenced this pull request Oct 14, 2025
@ADD-SP ADD-SP mentioned this pull request Oct 14, 2025
ADD-SP pushed a commit that referenced this pull request Oct 14, 2025
@ADD-SP ADD-SP mentioned this pull request Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants