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

Fix mpsc::SyncSender spinning behavior #106701

Merged
merged 3 commits into from
Jan 14, 2023

Conversation

ibraheemdev
Copy link
Member

Resolves #106668.

@rustbot
Copy link
Collaborator

rustbot commented Jan 11, 2023

r? @Mark-Simulacrum

(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 Jan 11, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jan 11, 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

Copy link
Member

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

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

AFAIK std::sync::mpmc does not use is_completed together with snooze, so this looks like the correct fix for the current implementation of std::sync::mpmc.

Resolves #106668.

I think beta backport is also needed to completely fix #106668 because it marked as regression-from-stable-to-beta.

@taiki-e
Copy link
Member

taiki-e commented Jan 11, 2023

The current documentation around Backoff seems to be inconsistent with how it is actually used in the mpmc implementation, so (if we keep the current behavior) I would suggest updating it.

/// Backs off in a lock-free loop.
///
/// This method should be used when we need to retry an operation because another thread made
/// progress.

/// Backs off in a blocking loop.

@m-ou-se m-ou-se assigned Amanieu and unassigned Mark-Simulacrum Jan 11, 2023
@cuviper cuviper added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jan 11, 2023
@Amanieu
Copy link
Member

Amanieu commented Jan 13, 2023

I reviewed the current usage and think it is fine.

@bors r+

Separately, I think the retry loop when sending a message should be removed (like we did with the similar loop in recv). The reasoning is the same: we don't want to waste CPU cycles spinning when trying to write to a full channel. Instead we should just go ahead and block immediately. But this can be a separate PR.

@bors
Copy link
Contributor

bors commented Jan 13, 2023

📌 Commit 8917e99 has been approved by Amanieu

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 Jan 13, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 13, 2023
…anieu

Fix `mpsc::SyncSender` spinning behavior

Resolves rust-lang#106668.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 13, 2023
…anieu

Fix `mpsc::SyncSender` spinning behavior

Resolves rust-lang#106668.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 13, 2023
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#104645 (Add log-backtrace option to show backtraces along with logging)
 - rust-lang#106465 (Bump `IMPLIED_BOUNDS_ENTAILMENT` to Deny + ReportNow)
 - rust-lang#106489 (Fix linker detection for linker (drivers) with a version postfix (e.g. clang-12 instead of clang))
 - rust-lang#106585 (When suggesting writing a fully qualified path probe for appropriate types)
 - rust-lang#106641 (Provide help on closures capturing self causing borrow checker errors)
 - rust-lang#106678 (Warn when using panic-strategy abort for proc-macro crates)
 - rust-lang#106701 (Fix `mpsc::SyncSender` spinning behavior)
 - rust-lang#106793 (Normalize test output more thoroughly)
 - rust-lang#106797 (riscv: Fix ELF header flags)
 - rust-lang#106813 (Remove redundant session field)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@ibraheemdev
Copy link
Member Author

Separately, I think the retry loop when sending a message should be removed (like we did with the similar loop in recv). The reasoning is the same: we don't want to waste CPU cycles spinning when trying to write to a full channel. Instead we should just go ahead and block immediately. But this can be a separate PR.

I agree, the original reasoning was that it is similar to acquiring a mutex, but the difference is that bounded channels are often used with heavy backpressure, so a quick unpark is often not expected.

@bors bors merged commit 720137b into rust-lang:master Jan 14, 2023
@rustbot rustbot added this to the 1.68.0 milestone Jan 14, 2023
@m-ou-se m-ou-se added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jan 18, 2023
@Mark-Simulacrum Mark-Simulacrum removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jan 19, 2023
@Mark-Simulacrum Mark-Simulacrum modified the milestones: 1.68.0, 1.67.0 Jan 19, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 20, 2023
…mulacrum

[beta] backport rollup

* Revert "Make nested RPITIT inherit the parent opaque's generics." rust-lang#106759
*  Fix mpsc::SyncSender spinning behavior rust-lang#106701
*  rustdoc: fix outdated lint section of the book rust-lang#106605
*  Do not filter substs in remap_generic_params_to_declaration_params. rust-lang#106503
*  Correct detection of elided lifetimes in impl-trait. rust-lang#106501
*  Bump rust-installer rust-lang#106196
*  Don't panic on stable since miri is not available there rust-lang#105901
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 26, 2023
…anieu

Remove optimistic spinning from `mpsc::SyncSender`

Per rust-lang#106701 (comment).
Closes rust-lang#106804

r? `@Amanieu`
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request May 31, 2023
Remove optimistic spinning from `mpsc::SyncSender`

Per rust-lang/rust#106701 (comment).
Closes #106804

r? `@Amanieu`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel. 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.

SyncSender::send enters a busy-wait loop if the buffer is full
8 participants