Skip to content

alloc: clarify comment on ArcInner::weak lock sentinel#156787

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
LeSingh1:fix-arc-inner-weak-comment
Jun 13, 2026
Merged

alloc: clarify comment on ArcInner::weak lock sentinel#156787
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
LeSingh1:fix-arc-inner-weak-comment

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented May 20, 2026

Copy link
Copy Markdown
Contributor

The comment on the weak field of ArcInner currently states that the usize::MAX sentinel locks "the ability to upgrade weak pointers or downgrade strong ones", and that this is used to avoid races in make_mut and get_mut. Both halves of that description are inaccurate.

Weak::upgrade never inspects the weak count, so the lock has no effect on it. The operation only CASes on the strong count and is happy to succeed while the weak count is locked. Arc::make_mut also does not participate in the locking protocol, and the function body already contains a comment noting that observing usize::MAX for the weak count there is impossible because only a thread holding a strong reference can take the lock.

The lock is actually only taken in Arc::is_unique (which backs Arc::get_mut), and the only operation that observes and waits on it is Arc::downgrade, which spins while weak == usize::MAX before attempting its CAS to create a new Weak.

This PR rewrites the comment to describe what the lock is and what it actually prevents.

Closes #144296

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 20, 2026
@rustbot

rustbot commented May 20, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 7 candidates

Comment thread library/alloc/src/sync.rs Outdated
Comment on lines +394 to +395
// `Arc::get_mut`) to atomically confirm that only the implicit weak
// reference exists before checking the strong count for uniqueness.

@joboet joboet May 21, 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.

I think this could be phrased more precisely. The implicit-weak-reference check is trivial to achieve atomically, it's just that the entire operation (both the strong count and weak count comparison) has to be atomic, and the only way to achieve that is by locking one of the counts.

View changes since the review

@joboet

joboet commented May 28, 2026

Copy link
Copy Markdown
Member

This looks fine to me. Could you please squash your commits?

@joboet joboet assigned joboet and unassigned Mark-Simulacrum May 28, 2026
@joboet

joboet commented Jun 11, 2026

Copy link
Copy Markdown
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 11, 2026
@rustbot

rustbot commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Explain that the weak count doubles as a lock taken by get_mut and
downgrade, and that the value 1 is the implicit weak reference owned by
the strong references rather than an absence of weak handles.
@LeSingh1 LeSingh1 force-pushed the fix-arc-inner-weak-comment branch from 7d9e442 to 2be9d51 Compare June 11, 2026 20:09
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Squashed into a single commit. @rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 11, 2026
@joboet

joboet commented Jun 13, 2026

Copy link
Copy Markdown
Member

Great, thanks!
@bors r+ rollup

@rust-bors

rust-bors Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 2be9d51 has been approved by joboet

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 5. This pull request will be tested once the tree is reopened.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 13, 2026
rust-bors Bot pushed a commit that referenced this pull request Jun 13, 2026
…uwer

Rollup of 2 pull requests

Successful merges:

 - #157793 (Refactor libproc_macro: remove * imports)
 - #156787 (alloc: clarify comment on ArcInner::weak lock sentinel)
@rust-bors rust-bors Bot merged commit 1939d61 into rust-lang:main Jun 13, 2026
12 checks passed
rust-timer added a commit that referenced this pull request Jun 13, 2026
Rollup merge of #156787 - LeSingh1:fix-arc-inner-weak-comment, r=joboet

alloc: clarify comment on ArcInner::weak lock sentinel

The comment on the `weak` field of `ArcInner` currently states that the `usize::MAX` sentinel locks "the ability to upgrade weak pointers or downgrade strong ones", and that this is used to avoid races in `make_mut` and `get_mut`. Both halves of that description are inaccurate.

`Weak::upgrade` never inspects the weak count, so the lock has no effect on it. The operation only CASes on the strong count and is happy to succeed while the weak count is locked. `Arc::make_mut` also does not participate in the locking protocol, and the function body already contains a comment noting that observing `usize::MAX` for the weak count there is impossible because only a thread holding a strong reference can take the lock.

The lock is actually only taken in `Arc::is_unique` (which backs `Arc::get_mut`), and the only operation that observes and waits on it is `Arc::downgrade`, which spins while `weak == usize::MAX` before attempting its CAS to create a new `Weak`.

This PR rewrites the comment to describe what the lock is and what it actually prevents.

Closes #144296
@rustbot rustbot added this to the 1.98.0 milestone Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

The comment about the weak field in ArcArcInner is imprecise

4 participants