parking_lot icon indicating copy to clipboard operation
parking_lot copied to clipboard

Value inside Mutex stops being Send when updating to 0.12 from 0.11

Open vicky5124 opened this issue 3 years ago • 3 comments

I've been going through the locks of my library lavalink-rs, making them live as short as possible, not holding them across awaits, etc... It was all fine and good on 0.11, it compiles fine, and the library doesn't deadlock, even in extreme situations, but updating to 0.12, it no longer compiles. The compiler complains about a value not being Send, which makes no sense.

This is the code that fails to compile, with the error in the following image:

14 30 54_05-02-2022

Is the error due to actual poor design? Is this a regression in the library? Or, is this the compiler messing up?

I'm trying to think of ways to solve this by having an alternative design, but the only thing I can think of is refactoring the writer into being a channel instead of a lock, which would be much more work.

vicky5124 avatar Feb 05 '22 13:02 vicky5124

You need to enable the send_guard feature if you want to be able to send a MutexGuard to another thread. The reason this is a separate feature is that it is incompatible with the deadlock detector feature.

Amanieu avatar Feb 05 '22 14:02 Amanieu

Enabling the feature does fix compilation. Shouldn't the behaviour be documented? I don't see it being mentioned on Mutex nor MutexGuard

vicky5124 avatar Feb 05 '22 16:02 vicky5124

The send_guard option is documented in the README. By default, MutexGuard acts like the one in std which is not Send either.

By the way you really shouldn't be holding a MutexGuard across an await since this can cause a deadlock if tokio switches to another task while your suspended task is holding a lock.

Amanieu avatar Feb 05 '22 19:02 Amanieu