Skip to content

Arc: make is_unique respect promised ordering - #165962

Closed
nia-e wants to merge 1 commit into
rust-lang:mainfrom
nia-e:arc-unique-acquire
Closed

nia-e wants to merge 1 commit into
rust-lang:mainfrom
nia-e:arc-unique-acquire

Conversation

@nia-e

@nia-e nia-e commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

the docs on Arc::is_unique imply that Acquire loads are performed irrespective of success, but that's not how we handle this now and unsafe code can trigger UB because of this discrepancy. we could alternatively weaken the guarantee, but that would need to be somewhat hasty since this will be stable in 1.101. libs-nominated to decide what to do here.

thank you to @obi1kenobi & OpenAI for finding and reporting this!

AI-generated repro (that fails without acquire ordering)
use std::sync::atomic::{fence, AtomicPtr, Ordering};
use std::sync::Arc;
use std::thread;

fn main() {
    let control = std::env::args().nth(1).unwrap_or_default();
    let old = Box::into_raw(Box::new(7u8));
    let replacement = AtomicPtr::new(Box::into_raw(Box::new(42u8)));
    let slot = Arc::new(AtomicPtr::new(old));
    let weak = (control != "unique").then(|| Arc::downgrade(&slot));
    let writer = Arc::clone(&slot);
    let worker = thread::spawn(move || {
        let retired = writer.swap(replacement.into_inner(), Ordering::Relaxed);
        unsafe { drop(Box::from_raw(retired)) };
        drop(writer);
    });

    while Arc::strong_count(&slot) != 1 {
        thread::yield_now();
    }
    assert_eq!(Arc::is_unique(&slot), weak.is_none());
    if control == "fence" {
        fence(Ordering::Acquire);
    }
    let observed = slot.load(Ordering::Relaxed);
    assert_eq!(unsafe { *observed }, 42);

    worker.join().unwrap();
    unsafe { drop(Box::from_raw(slot.load(Ordering::Relaxed))) };
}

With the Miri component for nightly-2026-11-07, run the seed-zero case.

MIRIFLAGS=-Zmiri-seed=0 cargo +nightly-2026-11-07 miri run --quiet --target i686-unknown-linux-gnu

r? libs

@nia-e nia-e added beta-nominated Nominated for backporting to the compiler in the beta channel. I-libs-nominated Nominated for discussion during a libs team meeting. A-rc Area: `Rc` and `Arc` labels Nov 14, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 14, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Nov 14, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

I don't like there being an ignored load here but 🤷🏻

this.inner().weak.store(1, Release); // release the lock
unique
} else {
let _ = this.inner().strong.load(Acquire);

@clarfonthey clarfonthey Nov 14, 2026 •

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.

Is this strictly necessary? As in, does it really matter what the strong count is if the weak count says that it's not unique anyway, and you'd need to re-check the weak count to verify this?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

we need an acquire fence of some kind that touches strong. otherwise e.g. increment_strong_count won't properly synchronise as promised and that's observable (it ends up calling clone which only does a relaxed op so..)

@clarfonthey clarfonthey Nov 14, 2026 •

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.

Ugh, that's annoying, but I guess you're right. We do explicitly state this in the docs, even though I'm not sure we should.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

loosening the guarantee is also an option. i believe @SabrinaJewson originally introduced this and might be able to give more context on the desired semantics?

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.

I have a feeling that modifying the strong and weak counts was not something said with a whole lot of thought, but maybe it was. To me, the important aspect is that is_unique can explicitly be ordered before get_mut_unchecked, and so the strong count doesn't necessarily have to be involved in the false case, because in the false case you can't actually do that.

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.

My intent with the comment was just about get_mut_unchecked, so I was thinking that it would only Acquire when it returns true, but this must have slipped my mind when actually writing it. My bad 😦

If it’s not considered too much of a breaking change, I would be in favour of weakening the guarantee. If you find out an Arc is not unique, what is there to acquire anyway?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

no worries, wfm ^^ I'll check in with the team to be safe but I'll change this to a doc pr

@maxdexh

maxdexh commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

I don't understand why we can't just remove the guarantee here. AFAIK, ordering of the atomic operations on Arc has historically not been something we've considered observable behavior...

Does this have perf implications?

@nia-e

nia-e commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

it was an explicit request by the team (rust-lang/libs-team#560 (comment)). worth checking that meeting's notes and/or cc @joshtriplett in case you remember but either way it makes sense that you'd want to guarantee this ordering in most cases to ensure it's safe to do a get_mut_unchecked after

@maxdexh

maxdexh commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

If it's not safe to get_unchecked_mut the way it currently is, then that's a problem, because that's exactly how Arc::get_mut is implemented ^^

@clarfonthey

Copy link
Copy Markdown
Contributor

We can decide at the meeting what to do with this. I think what might make the most sense is to revert the stabilization of Arc::is_unique (keeping the stabilization for Rc::is_unique, which doesn't have this problem) and then make these changes so there's more time to notice issues on nightly.

this.inner().weak.store(1, Release); // release the lock
unique
} else {
let _ = this.inner().strong.load(Acquire);

@joshtriplett joshtriplett Nov 17, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This desperately needs a comment.

View changes since the review

@clarfonthey

Copy link
Copy Markdown
Contributor

Closing in favour of #166400 which was decided upon during the meeting.

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 23, 2026
@clarfonthey clarfonthey removed beta-nominated Nominated for backporting to the compiler in the beta channel. I-libs-nominated Nominated for discussion during a libs team meeting. labels Nov 23, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 24, 2026
… r=hanna-kruppe

Revert "stabilize {Arc,Rc}::is_unique"

Supercedes rust-lang#165962.
Reopens rust-lang#138938.

As mentioned last week: https://github.com/rust-lang/libs-team/blob/56dd8ca2e2aa2c097d8b8010da6fef790a5ec97c/minutes/2026-11-17.md#165962-arc-make-is_unique-respect-promised-ordering

(Note: this only reverts the stabilization commit, not the docs changes from the original PR. Those can stay.)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 24, 2026
… r=hanna-kruppe

Revert "stabilize {Arc,Rc}::is_unique"

Supercedes rust-lang#165962.
Reopens rust-lang#138938.

As mentioned last week: https://github.com/rust-lang/libs-team/blob/56dd8ca2e2aa2c097d8b8010da6fef790a5ec97c/minutes/2026-11-17.md#165962-arc-make-is_unique-respect-promised-ordering

(Note: this only reverts the stabilization commit, not the docs changes from the original PR. Those can stay.)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 24, 2026
… r=hanna-kruppe

Revert "stabilize {Arc,Rc}::is_unique"

Supercedes rust-lang#165962.
Reopens rust-lang#138938.

As mentioned last week: https://github.com/rust-lang/libs-team/blob/56dd8ca2e2aa2c097d8b8010da6fef790a5ec97c/minutes/2026-11-17.md#165962-arc-make-is_unique-respect-promised-ordering

(Note: this only reverts the stabilization commit, not the docs changes from the original PR. Those can stay.)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 24, 2026
… r=hanna-kruppe

Revert "stabilize {Arc,Rc}::is_unique"

Supercedes rust-lang#165962.
Reopens rust-lang#138938.

As mentioned last week: https://github.com/rust-lang/libs-team/blob/56dd8ca2e2aa2c097d8b8010da6fef790a5ec97c/minutes/2026-11-17.md#165962-arc-make-is_unique-respect-promised-ordering

(Note: this only reverts the stabilization commit, not the docs changes from the original PR. Those can stay.)
rust-bors Bot pushed a commit that referenced this pull request Nov 25, 2026
…uppe

Revert "stabilize {Arc,Rc}::is_unique"

Supercedes #165962.
Reopens #138938.

As mentioned last week: https://github.com/rust-lang/libs-team/blob/56dd8ca2e2aa2c097d8b8010da6fef790a5ec97c/minutes/2026-11-17.md#165962-arc-make-is_unique-respect-promised-ordering

(Note: this only reverts the stabilization commit, not the docs changes from the original PR. Those can stay.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-rc Area: `Rc` and `Arc` T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants