Race Conditions

Race Conditions

Have you ever heard of race conditions? I haven't... not until I decided to dive deeper into the world of cyber.

In general, race conditions stand for multiple things happening at the same time. Developers should be aware of what might happen and when. A race condition is when multiple things happen simultaneously and you were not expecting them. This could end in a very bad situation.

The best way to get a proper understanding here is to look at an example.

Let's say we have 2 bank accounts.

Bank Account 1 has $1000 and Bank Account 2 has $1000. Now, we have 2 users that do the same transaction at the same time. Let's say they want to transfer $500 from Bank Account 1 to Bank Account 2. Before they send the money, User 1 checks how much money there is on the account and he will see the number $1000. User 2 does the same thing right after user 1. Since nothing has happened yet, the $1000 will remain unchanged.

Bare with me here... User 1 decides to add $500 to Account 2. Account 1 still has $1000 but Account 2 now has $1500. Again, User 2 is on the same mission and he decides to add $500 to Bank account 2 as well. The last time he checked, this account's balance still had $1000. Now, if he adds another $500, the balance will suddenly be $2000 instead of $1500, since both users accessed the resource at the same time.  

After User 1 added the money to the other account, $500 is deducted from Account 1 which leaves it with a total balance of $500 ($1000 - $500) and Account 2 should have a balance of $1500 ($1000 + $50). This is NOT the case! Account 2 will have $2000 ($1000 + 500$ (User1) + 500$ (User2)).

Since user 2 performed a check balance, and added the money to the other account but hasn't performed another check balance when the first user added the money, he will see that he added $500 to the account but ends up with $2000 on the account instead of $1500.  

End Result:

Bank Account 1: $500

Bank Account 2: $2000

You can imagine that this is just a simple example and race conditions can become much more dangerous when we are dealing with higher numbers or we focus on different industries.

An incident that happened in the 1980s was much more harmful than just accessing a bank account at the same time. A radiation software machine used software as a security mechanism. If the operators changed the settings too quickly, the software interlocks failed and caused a deadly race condition. The race condition worked as described above and caused radiation that was 100 times the normal dose of radiation which resulted in 6 patients being injured and 3 dying.

In order to prevent race conditions from occurring, developers have to consider every possible scenario and plan for that in their software.