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

Consider removing mutex_atomic #4295

Open
carllerche opened this issue Jul 22, 2019 · 17 comments
Open

Consider removing mutex_atomic #4295

carllerche opened this issue Jul 22, 2019 · 17 comments

Comments

@carllerche
Copy link
Member

There are many reasons to use mutexes over atomics. Clippy is unable to correctly determine if an atomic is sufficient to replace the mutex use. Attempting to lint over could lead to pushing users to introduce bugs in their code.

I recommend removing this lint.

@Manishearth
Copy link
Member

I feel like this lint is correct most of the time; and clippy lints are supposed to be used with a liberal sprinkling of allow(). There needs to be stronger, more detailed justification for removing a lint.

@carllerche
Copy link
Member Author

My main point is the potential harm of taking action based on a false positive is high compared to other provided lints.

@carllerche
Copy link
Member Author

carllerche commented Jul 22, 2019

(I can't comment on the rate of false positive besides saying I have only experienced a 100% false positive rate 😄 ).

@Manishearth
Copy link
Member

I disagree, lints aren't as strong signals as compiler warnings, and Clippy's warnings are meant to be taken with a grain of salt. Here the signal is "there might be an issue", if you determine it isn't an issue, allow() it.

If you really think there's a problem here a stronger case needs to be made, with lots of examples.

@Manishearth
Copy link
Member

I have only experienced a 100% false positive rate

Like I said, clippy is supposed to be used with a liberal sprinkling of allow(). Some lints just don't make sense for some codebases. It's not possible for our lints to be totally globally applicable. It's up to the codebase to make this judgement.

@carllerche
Copy link
Member Author

Ok, feel free to close if you disagree.

@Manishearth
Copy link
Member

(leaving open since other maintainers, and other people, may have different opinions)

@flip1995
Copy link
Member

The lint mentions in its Known problems section that

This lint cannot detect if the mutex is actually used for waiting before a critical section.

I'm with @Manishearth here. I think in the general case this lint is correct and Atomics are sufficient. If you really need the mutex you should just allow the lint.

Removing this lint would require to show (with examples), that you need a Mutex in the general case.

@thombles
Copy link

I want to add a +1 since I ran into this reviewing a PR the other day where the submitter defaulted to using Relaxed. Replacing Mutex<bool> with AtomicBool like it shows in the example is easy, but now you need to reason correctly about Ordering. Speaking only for myself, I've read about three articles about memory ordering and I still don't know which combination of Acquire and Release to use in a given situation. I happen to know that using SeqCst everywhere is safe, maybe because I read that on stack overflow once. That's a whole lot of homework that goes unmentioned and if you don't follow through on it you might introduce heisenbugs.

I'm quite happy to use the allow feature as noted above but I feel like the lint is a footgun to begin with. Those who know how to use atomics correctly will notice this potential performance issue the moment they see it.

@taiki-e
Copy link
Member

taiki-e commented Jan 10, 2022

Filed #8260 to downgrade this lint to nursery group that allowed by default.

Seems that Carl originally proposed to remove this lint completely, but the maintainers rejected it, so I believe downgrading is a reasonable compromise for now.

bors added a commit that referenced this issue Jan 11, 2022
Downgrade mutex_atomic to nursery

See #1516 and #4295.

There are suggestions about removing this lint from the default warned lints in both issues.
Also, [`mutex_integer`](https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer) lint that has the same problems as this lint is in `nursery` group.

changelog: Moved [`mutex_atomic`] to `nursery`
paullegranddc added a commit to DataDog/libddprof that referenced this issue Apr 21, 2022
paullegranddc added a commit to DataDog/libddprof that referenced this issue Apr 21, 2022
paullegranddc added a commit to DataDog/libddprof that referenced this issue Apr 21, 2022
paullegranddc added a commit to DataDog/libddprof that referenced this issue Apr 21, 2022
paullegranddc added a commit to DataDog/libddprof that referenced this issue Apr 21, 2022
* Update 3rd party license file

* Allow mutex_atomic clippy lint
motivations: rust-lang/rust-clippy#4295
pawelchcki added a commit to DataDog/libddprof that referenced this issue Apr 21, 2022
* Update 3rd party license file

* Allow mutex_atomic clippy lint
rust-lang/rust-clippy#4295

* add more lints to correspond with existing build system

* Remove empty trailing lines

* Fix cargo home directory in LICENSE-3rdparty file

* Export new license file on check fail

Co-authored-by: paullegranddc <[email protected]>
Co-authored-by: paullegranddc <[email protected]>
r1viollet pushed a commit to DataDog/libdatadog that referenced this issue May 5, 2022
* Update 3rd party license file

* Allow mutex_atomic clippy lint
motivations: rust-lang/rust-clippy#4295
r1viollet pushed a commit to DataDog/libdatadog that referenced this issue May 5, 2022
* Update 3rd party license file

* Allow mutex_atomic clippy lint
rust-lang/rust-clippy#4295

* add more lints to correspond with existing build system

* Remove empty trailing lines

* Fix cargo home directory in LICENSE-3rdparty file

* Export new license file on check fail

Co-authored-by: paullegranddc <[email protected]>
Co-authored-by: paullegranddc <[email protected]>
@Noratrieb
Copy link
Member

I agree that this lint is bad and should be removed. Atomics are very complicated and very easy to get wrong. Mutexes on the other hand are simple and a lot harder to get wrong. People who don't think of using an atomic in the first place shouldn't be directed to using an atomic instead, the mutex will be fine.

This like feels a little like if clippy suggested replacing safe but a little more expensive type casts with transmute. Yes, it's faster and often works just as well, but it has a higher maintenance burden and more risk of getting it wrong.

We want people to write correct code, not the fastest code imaginable. And if someone knows atomics well enough to use them, they will probably not need this lint in the first place.

@chorman0773
Copy link

chorman0773 commented Nov 3, 2022

I highly recommending removing this lint. Under complicated code, a person not knowing better would be directed to (incorrectly) use an atomic rather than a mutex, and bugs caused by this may be subtle and occur rarely in practice and may go undiscovered for some time, particularily if the code is rarely or never tested off of x86.
Even if the Mutex in the code could, in theory, be replaced with an atomic, doing so requires careful consideration and careful work, especially if an ordering stricter than Relaxed is needed.

Mutex<i32> and AtomicI32 can be (and are) used in substantially different ways.

I think in the general case this lint is correct and Atomics are sufficient. If you really need the mutex you should just allow the lint.

The problem is that people who would recognize when they do and do not need a mutex would also be able to recognize when they should use an atomic vs. when they should use a mutex. The lint will hit two groups of people:

  • People who know what they are doing and have deliberately chosen to use a Mutex and thus will allow the lint, or
  • People who do not, used Mutex as a default, and would not know whether or not they can replace Mutex with an atomic, and may do so without realising that their code is now broken or unsound.
    The first group of people don't need this lint, and the second group of people may be actively harmed by the lint.

@rdrpenguin04
Copy link

It seems this is universally agreed upon that this lint is bad. Should someone make a PR to move this to restriction or something like that?

@llogiq
Copy link
Contributor

llogiq commented Dec 24, 2022

I'm actually not sure if restriction is the right group here. Maybe put it into leave it in nursery instead and also add a comment to sum up this discussion to the lint docs.

@taiki-e
Copy link
Member

taiki-e commented Dec 24, 2022

Well, this lint is already in nursery since #8260.

@rdrpenguin04
Copy link

The reason I feel nursery isn't a good fit is because, in general, the lints in nursery at least have potential to do the correct thing if the lint is finished; it's a "nursery" for future lints. In this case, the lint seems to be unhelpful in general, and the only change I see of it being useful is as a restriction. My own opinion though.

bors added a commit that referenced this issue Dec 30, 2022
Move `mutex_atomic` to `restriction`

By #4295, the general consensus seems to be that `mutex_atomic` is not a useful lint in most cases. If anything, it could be useful as a restriction on code that for whatever reason can't use atomics. Keeping it in `clippy::nursery` is harmful to people attempting to use clippy for soundness.

---

changelog: Moved [`mutex_atomic`] to `restriction`
[#10115](#10115)
<!-- chnagelog_checked -->
@ruuda
Copy link

ruuda commented May 19, 2023

This lint is also counter-productive advice when the Mutex<bool> is used with std::sync::Condvar::wait and variations, because Condvar::wait needs the mutex guard and can’t work with an AtomicBool. In fact, the example in the Condvar documentation for its canonical usage relies on Mutex<bool>. This was already raised separately in #1516, but I thought I’d highlight it here, because the lint’s docs link to this issue in particular.

ruuda added a commit to ruuda/musium that referenced this issue May 21, 2023
Clippy's advice is incorrect here, we need a Mutex for the Condvar to be
able to use Condvar::wait.

See also:

 * rust-lang/rust-clippy#1516
 * rust-lang/rust-clippy#4295 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants