Yet Another Performance Footgun in Rust - Multithreading Lock Edition

It turns out that RAII can lead to performance "footguns", not just regarding memory, but also with multithreading locks.

The "holding a mutex longer than necessary" footgun.

It’s easy to fall into this trap :

{
    let data = data.lock()
    data.update(10) 
    // ...lots of other code...
}

Oops, the mutex remains locked for the entire duration of the scope, even though it only needed to be held until the final write to the data variable. This degrades performance because other threads are forced to wait longer than they should. The performance hit, latency and throughput, scales linearly with the amount of code executed after the last write, the longer that code takes to reach the end of the scope, the more severe the latency increase and throughput drop become.

This footgun appears frequently in real world code, as many developers write "vanilla" Rust without manually managing the lifetime of these locks. One might suggest calling drop explicitly or wrapping the logic in an inner scope, but neither is a robust or reliable solution. Why? Because both require manual awareness, in other words, they rely on the developer's skill to do so. By definition, a "footgun" is something that catches users off guard, often without them even realizing it.

So, what is the solution? As it turns out, "ASAP drop" (dropping as soon as possible) can resolve not only memory related footguns but also performance issues stemming from multithreading locks.

Visualization :

let data = data.lock().unwrap() // `data` becomes a `MutexGuard<T>` 

data.update(10) 

data.update(20) 

// The compiler analyzes when the `MutexGuard<data>` is last used. 
// If the code above represents the final access, the compiler automatically inserts an unlock operation immediately afterward.
// This allows other threads to take their turn without any unnecessary, prolonged waiting. 

data.unlock() // Inserted by the compiler

Edit : removed the last reader section to prevent further unnecerery confusion.

This ensures that other threads get their turn as soon as possible, eliminating wasted time, resulting in low latency and high throughput right out of the box.

1 Like

Is this just clippy::significant_drop_tightening?

But enforced instead of linted? Would be better to get that lint out of nursery, rather than adding a new language feature.

9 Likes

Mutex is not a RwLock. This would very much be unsound. If the lifetime of the MutexGuard is not extended by reads, then there is no way for it to protect those reads against concurrent writes from a following attempt to claim the lock.

Not everyone uses Clippy, so it can't enforce this high performance coding, consequently, a great deal of "vanilla" Rust code still contains these performance "footguns," meaning there is no guarantee that Rust multithreaded code out there is low latency and high throughput.

Because when that last reads happen, no write operations are occurring nor will any occur, so it is safe, keeping the lock engaged would actually be a mistake.

The moment you drop the lock, another thread can acquire it for writing, and start writing, which will conflict with the remaining reads.

6 Likes

The thread attempting to perform a write operation is told to wait until the atomic counter for readers reaches 0, the lock itself is already unlocked, so operations that only perform reads do not make other threads' read operations wait.

I realized that this wouldn't result in an even distribution of operations if the number of reads and writes are high, because writes are forced to wait for all reads to finish, they aren't allowed to jump ahead. This leads to low latency for reads but high latency for writes, which is a drawback for scenarios that also require low latency for writes. Therefore, the "last reader" aspect can be disregarded.

This has already been proposed and rejected long ago at Static drop semantics by pnkfelix · Pull Request #210 · rust-lang/rfcs · GitHub and Allow Eager Drops by rkjnsn · Pull Request #239 · rust-lang/rfcs · GitHub. Unless you have some new way to prevent this behavior from being confusing, it's unlikely that re-attempting it would succeed.

6 Likes

Note, that the discussion mentioned above took place a long time ago, before Rust had "non lexical lifetimes." At that time, it was argued that tracking a variable after it had been moved was impossible, it is no longer true after "non lexical lifetimes". The principle behind "ASAP drop" is essentially the same as that of non lexical lifetimes, the only difference is that while non lexical lifetimes uses the last usage information to end the borrow of T, ASAP drop uses the last usage info to inserts a drop(variable_name).

In my previous thread regarding ASAP drop, the specific issue raised concerned locks, now I demonstrated that, in fact, locks work significantly better with ASAP drop than they do with scope based dropping. The only remaining issue involves raw pointers, which are inherently unsafe anyway. This is not a strong enough argument to claim that ASAP drop can't be implemented, as a solution exists: if a conversion to a raw pointer (such as as *const T, &raw const T, &raw mut T, etc) occurs, the drop mode for that specific T can simply revert to scope based dropping. It offers the best of both worlds.

You cannot detect non-obviou uses through raw pointers.

There is no readers counter in a Mutex.

I mentioned making a Mutex behave like an RwLock by read access is non blocking, but I also stated that this idea was discarded for the reasons I typed above.

You can take a look at the other sections.

How do you detect such a conversion, if it occurs inside a function call?

By looking at the signature, if the return type is a raw pointer, it is clear that this is not an "ASAP drop" but rather a "scope based drop". Subsequently, when the compiler processes the function body, it will identify which variable is converted into the raw pointer returned by the function.

The raw pointer may be hidden inside another type. Or it may be stashed into one of the arguments.

1 Like

I don't see how that works. Take this contrived example on the Rust playground; no function signatures contain raw pointers as parameters or return types..

How is the compiler supposed to detect that calling DangerZone::pr (which takes only &mut self) will cause UB if s is dropped ASAP, but not if s is dropped at the end of the scope, without looking inside the definition of DangerZone::pr?

Note, too, that DangerZone::clear cannot cause UB, and has the same type signature as DangerZone::pr.

And yes, this is not the greatest code out there for a number of reasons - but today, if you remove the if true { … } block (which is there to simulate what I could do if there was in fact, ASAP drop of a MutexGuard), it's safe. Bad code, but safe. Removing drop(s) is sufficient to ensure that it remains safe - it'll deadlock when it tries to lock the mutex for a second time, but that's still safe, just undesired. But with the drop(s) in place, I can trigger UB that the lock is "supposed" to protect me from - and your heuristic of "is there a raw pointer in a function signature" doesn't fire, so with ASAP drop, I'd have the equivalent of the drop(s).

So, does the Rust compiler only look at a function's return type as a "plain" entity, or does it evaluate it recursively, down to its constituent parts? If it only considers the top level type, there are plenty of other ways to achieve this. It’s the same mechanism the compiler uses to detect !Send and !Sync in raw pointers, even when they are nested within other types. Essentially, there’s a trait, let’s call it ASAP_Drop, that is automatically implemented for all types, with the exception of raw pointers. If any component type is !ASAP_Drop, then the entire type is considered !ASAP_Drop. This approach can be leveraged to track down raw pointers.

As I mentioned above, "ASAP drop" is intended for optimizing Rust code that relies on references. Pointers, however, are "unsafe" and do not carry lifetime information, so the compiler will not apply ASAP drop to code involving raw pointers. Furthermore, because ASAP drop for MutexGuard significantly mitigates the "accidental extension of a lock's scope" pitfall, it also makes the code much more resilient against deadlocks.

Your code will utilize scope based dropping, which the compiler will handle automatically. Like in this my design explanation :

A constructor for a null std::ptr will include a marker, something like T: !ASAP_drop, indicating that the type does not support ASAP drop. If you attempt to use a type that impl ASAP_drop, the compiler will catch this error internally (preventing it from reaching the user) and instead designate that specific section to use scoped drop.

An example of vanilla Rust code that falls into a "deadlock" footgun in scope based drop and impossible to happen in ASAP drop out of the box.

Scope based drop :

use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

fn main() {
    let raw_data = Arc::new(Mutex::new(0));
    let resource_b = Arc::new(Mutex::new(false));

    let t1_data = Arc::clone(&raw_data);
    let t1_res = Arc::clone(&resource_b);
    
    let handle = thread::spawn(move || {
        let mut data = t1_data.lock().unwrap();
        *data += 1;
        
        thread::sleep(Duration::from_millis(50));
        
        let ready = t1_res.lock().unwrap();
        if *ready {
            println!("Thread 1 finish");
        }
    });

    thread::sleep(Duration::from_millis(10));

    let mut ready = resource_b.lock().unwrap();
    *ready = true;

    let data = raw_data.lock().unwrap();
    println!("Main thread read: {}", *data);

    handle.join().unwrap();
}

ASAP drop (using a manual drop call to simulate an ASAP drop, as the ASAP drop feature is not yet available) :

use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

fn main() {
    let raw_data = Arc::new(Mutex::new(0));
    let resource_b = Arc::new(Mutex::new(false));

    let t1_data = Arc::clone(&raw_data);
    let t1_res = Arc::clone(&resource_b);
    
    let handle = thread::spawn(move || {
        let mut data = t1_data.lock().unwrap();
        *data += 1;
        drop(data);
        
        thread::sleep(Duration::from_millis(50));
        
        let ready = t1_res.lock().unwrap();
        if *ready {
            println!("Thread 1 finish");
        }
        drop(ready);
    });

    thread::sleep(Duration::from_millis(10));

    let mut ready = resource_b.lock().unwrap();
    *ready = true;
    drop(ready);

    let data = raw_data.lock().unwrap();
    println!("Main thread read: {}", *data);
    drop(data);

    handle.join().unwrap();
}

I see that in your example, ASAP drop would indeed prevent a deadlock. However, to prevent deadlocks, you are generally advised to only ever lock resources in the same order and have a hierarchy of locks (Deadlock (computer science) - Wikipedia). This would have prevented the deadlock in your example as well.

Coming back to this

in the context of this example

the compiler would need to somehow track that anywhere in the struct DangerZone there is a pointer to the locked data. This may even be in another crate. Let's assume this would work.

Getting back to the deadlock situation, think of the following example

Lock resource A
Lock resource B
holds_pointer = do_something_with_B() // Here we are calling an external crate that somewhere deep in the chain
                                      // converts B to a pointer and therefore causes holds_pointer to be !ASAP_drop.
do_last_thing_with_A()
-> implicit A drop
do_other_things(holds_pointer)
-> B drops due to scope end

The problem I see here is that you would somehow need to know that holds_pointer has a pointer to B, therefore is implicitly !ASAP_Drop, and you would have to manually prevent A from getting dropped before B to ensure your lock hierarchy is kept intact. In my mind this a bigger potential footgun than the performance impact of keeping locks too long.