Questions tagged [race-condition]

A race condition is when multiple threads/processes/clients all use a resource, without proper use of locks.

A race condition is when multiple threads/processes/clients all use a resource, without the proper use of locks.

In multi-threaded programs, race conditions occur because context switches between threads are often erratic and unpredictable, often causing a shared value to change at inopportune times.

Servers are also vulnerable to race conditions, sometimes more so, because there are many independent clients which act independently, as well as delays caused by network communication.

Consider the example of two people simultaneously editing the same document:

  1. Person #1 sees an incorrect line. #1 begins to fix this line, but is a slow typist.
  2. Person #2 sees the same mistake - while #1 is typing - and fixes it.
  3. #1 and #2 commit their edited documents at the same time, without the other being notified.
  4. #1's change reaches the server first, and the server writes it to disk.
  5. #2's change reaches the server later, and the server silently overwrites #1's change.

Locks are a common way to avoid race conditions. Consider the above example with proper locking:

  1. Person #1 sees the incorrect line and starts editing it.
  2. Person #2 also sees the incorrect line, but is unable to edit until Person #1 has pushed their changes to the server.
  3. Person #1's changes are seen by both #1 and #2.
  4. Person #2 decides that #1's changes are adequate, and decides not to edit.

In this scenario, edit access to the document is restricted to one client only. This prevents Person #2 from silently overwriting Person #1's changes. See Wikipedia for further examples.

2338 questions
0
votes
2 answers

How would I create a multiprocessing.Lock() for each element in a dictionary?

I am trying to create a multiprocessing-based program that has a file cache to speed things up. This cache is empty at the start of the program but then is filled out as requests to the data are made. There is also an extra set of files which are…
0
votes
0 answers

Prevent concurrent execution of sub with same parameter

We have an application running on clinet which is used by about 30 concurrent users. In the application there is a Sub which deletes and inserts records in the underlying SQL-Server-Database. The ID is a primary key in table A. Table B is filled in…
bautista
  • 765
  • 10
  • 26
0
votes
1 answer

Race condition in multi thread

I am trying to make race condition and fix it with mutex lock. My code is working correct which is no race condition. Should not it make race condition? #define MAX_RESOURCES 10 #include #include #include #include…
ken d
  • 1
  • 1
0
votes
1 answer

Java - Why are the locks not working on my variable in a multithreaded java program?

I am trying to learn concurrency in Java and not getting the desired results, why arent my locks working? I have tried every tutorial I can find and it is getting me closer, but not 100% import java.util.concurrent.locks.*; public class Concurrent…
Constantine
  • 1
  • 1
  • 4
0
votes
0 answers

Race condition with Java executor service

Issue: Iterating through files in a directory and scanning them for findings. ExecutorService used to create a thread-pool with fixed # of threads and invoking submit method like this: final List>> futures =…
0
votes
1 answer

Atomic operations in client server applications in C

I have developed a client application and server application which can both run on any Linux computer and communicate with each other. I need to set some flag in each application to prevent a race condition when performing a particular operation on…
Engineer999
  • 3,683
  • 6
  • 33
  • 71
0
votes
0 answers

Is this petersons solution correct for N threads?

bool lock[N]; int turn=0; int offset=0; int M = N-1; int pidToN(int pid); //returns a unique number between (0,N-1) for a given pid; mapping pids void critical() { int pidn = pidToN(getpid()); lock[pidn] = true; turn = M-pidn; …
Tony
  • 81
  • 9
0
votes
0 answers

EBS volume lock

I have a pool of EBS volumes that are being taken and assigned to machines during the deployment process, conducted by cloudformation. This deployment process has the following steps: Select untagged volumes from the pool Assign tags to them Give…
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
0
votes
1 answer

race condition in KVM with hypercall KVM_HC_KICK_CPU

To implement efficient spinlocks in the VM enviroment, KVM documentation says that a vcpu waiting for spinlock can execute halt instruction and let the spinlock holder vcpu get chance for execution, this spinlock holder vcpu can then execute…
zephyr0110
  • 223
  • 1
  • 11
0
votes
0 answers

Is there a way to kill and clean up a c++ program if it deadlocks/waits forever

So I built a program that should be released to production soon, but I'm worried if I run into a situation where all threads lock/wait, that the pipeline will be compromised. I am pretty sure I designed it so this won't happen, but if it were to,…
0
votes
1 answer

Is this Redis Race Condition Scenario Possible?

I'm debugging an issue in an application and I'm running into a scneario where I'm out of ideas, but I suspect a race condition might be in play. Essentially, I have two API routes - let's call them A and B. Route A generates some data and Route B…
Bassinator
  • 1,682
  • 3
  • 23
  • 50
0
votes
1 answer

Postgres update where null

I wonder whether this scenario is safe race condition wise. I mean is there is any risk that 2 or more queries run in the same time will override themself: UPDATE table_name SET process_id = 'foobar' WHERE process_id IS NULL AND (...); Desired…
Robert Trzebiński
  • 1,327
  • 8
  • 16
0
votes
1 answer

Can race condition happen between processes?

I know that between the threads in the same process, race condition can happen because there are many information explaining about it. But, I cannot find any information explaining that between processes, race condition can happen. So, can race…
0
votes
2 answers

How to solve race condition when installing ruby gems and running scripts that use these gems?

We found interesting problem. Our environment is configured by using ansible, which in turn installs gems. Some of the gems, we want version that is newer than something. For example, aws-sdk-core version >= 3.104. This ansible tasks runs: gem…
chooboo
  • 51
  • 5
0
votes
1 answer

What happens when a variable is read and AT THE SAME TIME another thread MODIFIES IT?

If the reading happens BEFORE or AFTER the update the behavior is obvious, it is not difficult to see, but what if it happens at the same time? Assuming there is nothing else (no synchronization, just a simple variable read and assignment). from…
user19551894