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
1 answer

Is it possible to read a short-lived file reliably in /tmp due to periodic cleanup?

I'm considering making my application create a file in /tmp. All I'm doing is making a temporary file that I'll copy to a new location. On a good day this looks like: Write the temp file Move the temp file to a new location However on my system…
karobar
  • 1,250
  • 8
  • 30
  • 61
0
votes
1 answer

DDD Orchestration-based saga race condition

I have three aggregates Product, Seller, Buyer. The buyer offers to buy a product from the seller and the seller can accept or reject the buy offer. So the process for making the offer is this: in the buyer aggregate I check whether the buyer has…
manfrom
  • 91
  • 7
0
votes
1 answer

Potential race condition in openmp producer consumer example

I was trying to solve this openmp tutorial exercise in which I was required to parallelize the following serial code: /* ** PROGRAM: A simple serial producer/consumer program ** ** One function generates (i.e. produces) an array of random values. …
Setu
  • 180
  • 8
0
votes
1 answer

OpenMP Do I have race condition or false-sharing '?

I'm trying to write a code for matrix multiplication. As far as I understand OMP and pararel programming this code may suffer from race condition. #pragma omp parallel #pragma omp for for (int k = 0; k < size; k++){ for (int i = 0; i <…
0
votes
0 answers

Publishers.CombineLatest - Incorrect Value

I've recently faced an issue with CombineLatest reporting one of it's values incorrectly. This is hard to reproduce, yet it is reproducible. Setup: let onFirst = CurrentValueSubject(false) let onSecond = CurrentValueSubject
Pawel Klapuch
  • 380
  • 4
  • 15
0
votes
1 answer

Spring Boot Race Condition between PropertySource and ConditionalOnProperty

I have a custom starter that other projects depends on, and that starter applies some configurations including a PropertySource @Configuration @PropertySource(value = "classpath:application-geoip.yml", factory =…
Olgun Kaya
  • 2,519
  • 4
  • 32
  • 46
0
votes
1 answer

ASP.NET MVC2 AsyncController: Does performing multiple async operations in series cause a possible race condition?

The preamble We're implementing a MVC2 site that needs to consume an external API via https (We cannot use WCF or even old-style SOAP WebServices, I'm afraid). We're using AsyncController wherever we need to communicate with the API, and everything…
rejj
  • 1,216
  • 7
  • 13
0
votes
1 answer

Python threading: thread_object(target=func()) gives different result than target=func i.e.without parenthesis

I'm learning the concept of race_condition and lock in python multithreading and I spotted this weird behavior In the following programme from threading import Thread import time value = 0 def increase(): global value local = value …
Anmol Virk
  • 3
  • 1
  • 3
0
votes
1 answer

Returning shared_ptr in multithreading is safe?

I have just started working in multi-threaded environment with c++.I have doubt in below code snippet. std::shared_ptr getSessionLocked(const std::string& token) { std::lock_guard guard(m_mutex); if…
0
votes
1 answer

EXC_BAD_ACCESS KERN_INVALID_ADDRESS crash in addOperation of OperationQueue

I have asynchronous operation implementation like this: class AsyncOperation: Operation { private enum State: String { case ready, executing, finished fileprivate var keyPath: String { return "is\(rawValue.capitalized)" } } …
Shohin
  • 519
  • 8
  • 11
0
votes
1 answer

Preventing race conditions with transaction locking in MongoDB

I have a REST API (distributed with across multiple hosts/containers) that uses MongoDB as a database. Among the collections in my database, I want to focus on the Users and Games collections in this example. Let's say I have an endpoint (called by…
Dave
  • 65
  • 1
  • 12
0
votes
1 answer

Race condition when saving to the supabase database

Im subscribing to a webhook that is listening for Calendly session creation (invitee.created) and session cancelation (invitee.canceled). When I get the data I update the relevant record in the database. This works fine for the most part as users…
Omar
  • 49
  • 1
  • 6
0
votes
0 answers

How to prevent race condition for specific case?

I'm trying to prevent race condition and also want to don't affect system performance as could as possible. If two signal(data) arrive to server at the same time, handler starting to process these simultaneously(multi-thread), ıf signals have…
0
votes
1 answer

How can I do a read-create transaction with Npgsql?

In an ASP.NET Core 6 website which uses EF Core (Npgsql) I need to do the following scenario. There is a table which holds users' data of some type. Each row of this table (per user) has a calculated column which uses the previous row's value. This…
0
votes
1 answer

Spring JpaRepository Perform delete only if given Id exists and avoid race condition

my situtation is as follows: I have @Entity class Ingredient in my Spring JPA Project. I would like to implement a method performing delete operation on DB record by record Id public boolean deleteIngredient(String id) and if possible avoid handling…
Veidt
  • 33
  • 4
1 2 3
99
100