Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waker::will_wake() broken on nightly #121600

Closed
jkarneges opened this issue Feb 25, 2024 · 3 comments · Fixed by #121622
Closed

Waker::will_wake() broken on nightly #121600

jkarneges opened this issue Feb 25, 2024 · 3 comments · Fixed by #121622
Labels
C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jkarneges
Copy link
Contributor

I would expect this to run to completion:

use std::sync::Arc;
use std::task::{Wake, Waker};

struct NoopWaker;

impl Wake for NoopWaker {
    fn wake(self: Arc<Self>) {}
}

fn main() {
    let waker: Waker = Arc::new(NoopWaker).into();
    let other = waker.clone();

    assert!(waker.will_wake(&other));
}

On stable, it does:

$ rustup default stable
info: using existing install for 'stable-x86_64-apple-darwin'
info: default toolchain set to 'stable-x86_64-apple-darwin'

  stable-x86_64-apple-darwin unchanged - rustc 1.76.0 (07dca489a 2024-02-04)

$ cargo run
   Compiling will_wake v0.1.0 (/Users/justin/dev/will_wake)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target/debug/will_wake`

But not on current nightly:

$ rustup default nightly
info: using existing install for 'nightly-x86_64-apple-darwin'
info: default toolchain set to 'nightly-x86_64-apple-darwin'

  nightly-x86_64-apple-darwin unchanged - rustc 1.78.0-nightly (381d69953 2024-02-24)

$ cargo run
   Compiling will_wake v0.1.0 (/Users/justin/dev/will_wake)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.29s
     Running `target/debug/will_wake`
thread 'main' panicked at src/main.rs:14:5:
assertion failed: waker.will_wake(&other)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Interestingly, it works ok without cargo:

$ rustc --edition 2021 src/main.rs
$ ./main

The cargo project isn't special though:

$ cat Cargo.toml 
[package]
name = "will_wake"
version = "0.1.0"
edition = "2021"

[dependencies]

OS: macOS 13.6.3

@jkarneges jkarneges added the C-bug Category: This is a bug. label Feb 25, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 25, 2024
@matthiaskrgr
Copy link
Member

cc @tmiasko #119863 I guess?

@jkarneges
Copy link
Contributor Author

The internal raw_waker function uses const promotion for the vtable in two places, once at initial construction and another when cloning. Maybe that results in two differently-addressed vtables.

@dtolnay
Copy link
Member

dtolnay commented Feb 26, 2024

I gave this a shot in #121622. But without a language feature for statics with type parameters, we are not seeing a reliable way to restore will_wake's functionality.

Let's consider reverting #119863. Having will_wake be a handful of CPU instructions faster but way more often wrong is not useful.

@jieyouxu jieyouxu added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 26, 2024
@bors bors closed this as completed in b0ca9b5 Mar 3, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 3, 2024
Rollup merge of rust-lang#121622 - dtolnay:wake, r=cuviper

Preserve same vtable pointer when cloning raw waker, to fix Waker::will_wake

Fixes rust-lang#121600.

As `@jkarneges` identified in rust-lang#121600 (comment), the issue is two different const promotions produce two statics at different addresses, which may or may not later be deduplicated by the linker (in this case not).

Prior to rust-lang#119863, the content of the statics was compared, and they were equal. After, the address of the statics are compared and they are not equal.

It is documented that `will_wake` _"works on a best-effort basis, and may return false even when the Wakers would awaken the same task"_ so this PR fixes a quality-of-implementation issue, not a correctness issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants