Rust doesn't protect against deadlock

I was quite surprised how easy it is to cause deadlock in Rust. Somewhat rather disappointing, as simple scenario as mine could easily be statically analyzed and simply refuse to compile.
What am I missing?

You aren't missing anything. Rust can't prevent all bugs.

I'm very happy that Rust does not do such higher level program analysis.

If it did nothing I wrote would ever compile!


Sure, but I believe those kind of bugs are easily preventable by doing static analysis. This would I believe benefit greatly the entire Rust ecosystem.

Even more, those aren't just bugs. This is entire class of bugs, that could potentially be completely eliminated during the compilation phase...

1 Like

Surely if done properly it would help you in writing safer code?

I'm not convinced deadlocks are the kind of thing you can reliably detect outside of very simple cases.


Me neither, I just wanted to get someone's opinion...

Generally the things that the compiler prevent you from doing now are the kind of things you can encode in the type system. Rust's type system is very strong, and you can encode quite a lot of things, but you can't really encode the information needed for deadlock detection by the very nature of how mutexes interact with the type system.

To be more specific, mutexes use the feature called interior mutability to let you mutate through immutable references, and the way that the type system usually detects weird mutation pattern is by looking where the mutable references are, so mutexes have in some sense turned that part of the borrow-checker off.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.