Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could software written only in Rust fully avoid race conditions?

Wikipedia defines a race condition as:

A race condition or race hazard is the behavior of an electronics, software, or other system where the output is dependent on the sequence or timing of other uncontrollable events. It becomes a bug when events do not happen in the order the programmer intended.

Rust is a:

safe, concurrent, practical language

If we create software that is 100% Rust, can we avoid race conditions? Why or why not?

like image 362
Hessnov Avatar asked Dec 04 '22 20:12

Hessnov


1 Answers

No.

I've seen race conditions in:

  • filesystem accesses,
  • database accesses,
  • access to other services.

The environment in which a program evolves in full of data-races, and there's nothing a programming language can do but embrace it.


Rust focuses on memory-safety. In the context of multi-threaded programming, this means preventing data races.

A program with no data race can still contain race conditions:

  • data race: modification of a value while it is being read/written by another thread with no synchronization, the resulting behavior is unpredictable (especially when optimizers are involved),
  • race condition: a timing issue on a sequence of events, the resulting behavior is one of a small set of possible behaviors. It can be solved by synchronization, but this is not the only solution.

Race conditions are not memory errors. For Rust, this means they are considered safe, although of course they are still undesirable. They may happen at many different levels: between threads, processes, servers, ...

like image 118
Matthieu M. Avatar answered Jan 04 '23 15:01

Matthieu M.