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

Implement Sync for mpsc::Sender #111087

Merged
merged 2 commits into from
Jun 24, 2023
Merged

Implement Sync for mpsc::Sender #111087

merged 2 commits into from
Jun 24, 2023

Conversation

ibraheemdev
Copy link
Member

@ibraheemdev ibraheemdev commented May 2, 2023

mpsc::Sender is currently !Sync because the previous implementation contained an optimization where the channel started out as single-producer and was dynamically upgraded on the first clone, which relied on a unique reference to the sender. This optimization is one of the main reasons the old implementation was so complex and was removed in #93563. mpsc::Sender can now soundly implement Sync.

Note for any potential confusion, this chance does not add MPMC behavior. This only affects the already Send + Clone sender, not receiver.

It's technically possible to rely on the !Sync behavior in the same way as a PhantomData<*mut T>, but that seems very unlikely in practice. Either way, this change is insta-stable and needs an FCP.

@rustbot label +T-libs-api -T-libs

@rustbot
Copy link
Collaborator

rustbot commented May 2, 2023

r? @joshtriplett

(rustbot has picked a reviewer for you, use r? to override)

@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 2, 2023
@rustbot
Copy link
Collaborator

rustbot commented May 2, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 2, 2023
@ibraheemdev
Copy link
Member Author

@rustbot label -T-libs

@rustbot rustbot removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 2, 2023
@rust-log-analyzer

This comment has been minimized.

@BurntSushi
Copy link
Member

This PR makes mpsc::Sender implement Sync.

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Jun 8, 2023

Team member @BurntSushi has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jun 8, 2023
@BurntSushi
Copy link
Member

Are we certain that the original reason for a !Sync impl was because of an internal optimization? Can we go back and find where the original API was sketched out and see if there were any other reasons for the !Sync impl?

@rfcbot concern why-not-sync

@BurntSushi
Copy link
Member

Right now there are some failing tests. We should check to make sure those themselves don't raise any other concerns.

@rfcbot concern failing-tests

@goffrie
Copy link
Contributor

goffrie commented Jun 8, 2023

Are we certain that the original reason for a !Sync impl was because of an internal optimization? Can we go back and find where the original API was sketched out and see if there were any other reasons for the !Sync impl?

I dug up #11111 - which made senders (and other channel types) !Freeze (because Sync hadn't been invented yet). The only comment was:

#[no_freeze] // technically this implementation is shareable, but it shouldn't
             // be required to be shareable in an arc
pub struct SharedChan<T> {

I doubt that the decision was considered in-depth - my guess is that it was done just to keep options open for future implementation changes. But maybe @alexcrichton remembers ;)

@goffrie
Copy link
Contributor

goffrie commented Jun 8, 2023

Ah, I found #42397 as well which made SyncSender Sync; there's some interesting discussion in there.

@BurntSushi
Copy link
Member

@goffrie Thanks for digging up #42397. That gives me a lot of confidence that these types weren't Sync primarily due to implementation details. So I can at least resolve that concern.

@rfcbot resolve why-not-sync

@dtolnay
Copy link
Member

dtolnay commented Jun 9, 2023

the previous implementation contained an optimization where the channel started out as single-producer and was dynamically upgraded on the first clone, which relied on a unique reference to the sender. This optimization is one of the main reasons the old implementation was so complex and was removed in #75194.

This isn't the right PR. Is it #93563?

@ibraheemdev
Copy link
Member Author

@dtolnay yeah that's the one, not sure how #75194 ended up in there.

@ibraheemdev
Copy link
Member Author

ibraheemdev commented Jun 10, 2023

I guess the big question is why implement Sync if it's already Send and Clone, and the only real reason is ergonomics. For example, it makes patterns with thread::scope possible without extra cloning clutter:

let (tx, rx) = std::sync::mpsc::channel();
 
 // std::thread::scope(|s| {
//     let tx1 = tx.clone();
//     s.spawn(move || {
//         tx1.send(1);
//     });
//
//     let tx2 = tx.clone();
//     s.spawn(move || {
//         tx2.send(2);
//     });
// })

std::thread::scope(|s| {
    s.spawn(|| {
        tx.send(1);
    });
    
    s.spawn(|| {
        tx.send(2);
    });
})

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@BurntSushi
Copy link
Member

@rfcbot resolve failing-tests

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jun 13, 2023
@rfcbot
Copy link

rfcbot commented Jun 13, 2023

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Jun 23, 2023
@rfcbot
Copy link

rfcbot commented Jun 23, 2023

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@dtolnay
Copy link
Member

dtolnay commented Jun 23, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jun 23, 2023

📌 Commit 4ceca09 has been approved by dtolnay

It is now in the queue for this repository.

@bors bors 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 23, 2023
@dtolnay dtolnay assigned dtolnay and unassigned joshtriplett Jun 23, 2023
@ibraheemdev
Copy link
Member Author

cc @thomcc because they expressed thoughts on this.

@thomcc
Copy link
Member

thomcc commented Jun 23, 2023

My thoughts (I'm assuming you're referring to this tweet) are just that it's a useful optimization sometimes to be able to do things like that (even the lower-complexity version where we just check an Arc's reference count somewhere).

If we don't want to do it in the future, then IMO there's no harm in accepting this. My tweet was definitely not about this stabilization in particular -- which I'm not opposed to (std should be willing to prioritize ergonomics over having the best possible implementation1, as the best possible implementation for a given situation will almost always depend on that situation's specifics anyway)

Footnotes

  1. Besides, nobody has come forth with an optimization here that would leverage this anyway.

bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 24, 2023
…mpiler-errors

Rollup of 8 pull requests

Successful merges:

 - rust-lang#111087 (Implement `Sync` for `mpsc::Sender`)
 - rust-lang#112763 (Bump compiler_builtins)
 - rust-lang#112963 (Stop bubbling out hidden types from the eval obligation queries)
 - rust-lang#112965 (Don't emit same goal as input during `wf::unnormalized_obligations`)
 - rust-lang#112973 (Make sure to include default en-US ftl resources for `rustc_error` crate)
 - rust-lang#112981 (Fix return type notation errors with -Zlower-impl-trait-in-trait-to-assoc-ty)
 - rust-lang#112983 (Fix return type notation associated type suggestion when -Zlower-impl-trait-in-trait-to-assoc-ty)
 - rust-lang#112986 (Update cargo)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
Copy link
Contributor

bors commented Jun 24, 2023

⌛ Testing commit 4ceca09 with merge e0ba2d0...

@bors bors merged commit 4a01a38 into rust-lang:master Jun 24, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 24, 2023
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.