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
0 answers

Reproducing race conditions: continue all threads except one in gdb

I have found a race condition in my application code and now I am wondering how I could create a test case for it, that can be run as a test script that is determined to trigger a specific effect of the race condition and doesn't require a…
Yunus King
  • 1,141
  • 1
  • 11
  • 23
0
votes
1 answer

Obtaining number of processed messages in RabbitMQ queue

I would like to obtain a number of acknowledged and unacknowledged messages in a RabbitMQ queue, programmatically. I'm using the Typescript SDK. I've searched around but people typically propose using the HTTP API, which is no use for me. I'm using…
raf
  • 42
  • 11
0
votes
2 answers

c++11 two critical sections can use nested lock_guard?

If I have two critical sections, and I make two corresponding mutex to protect each of them.(I think it is necessary to precisely control when to lock because they use in different times and scenario, Q1:is it really so?) For example: bool…
f1msch
  • 509
  • 2
  • 12
0
votes
2 answers

lateinit var for LiveData

In my ViewModel I have a lateinit var to hold some LiveData. The way this variable is initialized depends on the data and the current date. Can't do it in SQL. This is the ViewModel: class MainViewModel { lateinit var timeStamps:…
0
votes
1 answer

Is a "race condition" the only reason a parallelized code would give a different output than sequential?

I have a function that, when I run it in parallel w/ diff inputs, gives a different output than when I run it sequentially with those same inputs. Is a race condition the only reason this would happen? EDIT: I have tried the following - I run 3…
Vladimir Belik
  • 280
  • 1
  • 12
0
votes
0 answers

MySQL Data inserted out of order (race condition?)

We have a table with the following DDL: create table test ( a VARCHAR(36) CHARSET utf8 NOT NULL, b TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) NOT NULL, number BIGINT UNSIGNED AUTO_INCREMENT, …
Dan Nemes
  • 300
  • 3
  • 16
0
votes
0 answers

Can I use MethodImplAttribute(MethodImplOptions.Synchronized) in ASP.NET Core?

In an application a user can create an order. When the order is created it is on pending for payment status. Since a pending order locks the product, a user can only have one order in pending for payment. This order can either be paid or be…
0
votes
1 answer

Thread safe LiveData updates

I have the following code which has a race condition. I try to find an item in a list and set its loading property. But if onLoaded("A") and onLoaded("B") are called multiple times from different threads. I always lose the data of the first call if…
0
votes
0 answers

C - Synchronization with thread and conditional variables

I'm trying to do a program in which there are two threads which talk like "pin-pong" way. The current result is the following: the player 2 starts the game 1-I'm 139735290140224 and my ID is 2 2-I'm 139735290140224 and my ID is 2 and is turn of…
0
votes
0 answers

How to insert a column to make it auto increment according to another column

There is the table: id type serial_no 1 apple 1 2 banana 1 3 banana 2 4 apple 2 5 water 1 I want the serial_no to be auto increment for every type I tried INSERT INTO MY_TBALE (type, serial_no) VALUES ( apple, (SELECT…
0
votes
2 answers

Prevent race condition in Postgres

let's suppose that I have an endpoint /seen/{bigint_number}, which is about 10K concurrently visits that with a random bigint number. the logic is simple. if the number is already stored in the database, it returns true, if the number has not been…
Yuseferi
  • 7,931
  • 11
  • 67
  • 103
0
votes
2 answers

Handling events in interactive Vega legends - race condtion

The linked chart contains only a legend and the legend works as follows: clicking on a fruit name toggles it on and off shift-clicking on a fruit name switches it ON and switches OFF all other fruit names Legend display is controlled by two…
az5112
  • 590
  • 2
  • 11
0
votes
0 answers

Is there a race condition when using IHttpClientFactory and different BaseUrl? ASP.NET Core

A named HttpClient is registered in Startup.cs as services.AddHttpClient("MainClient"). The HttpClient is used like this in different places: var httpClient = _httpClientFactory.CreateClient("MainClient"); httpClient.BaseAddress = new…
FireShock
  • 1,082
  • 1
  • 15
  • 25
0
votes
0 answers

Preventing race condition and probably incorrect access token expiration time check

Anytime I call SendAsync, it should automatically authenticate the API if the access token has expired. The issue is that _accessTokenExpirationTime may lead to a race condition. How do I protect against it, SemaphoreSlim? I also don't think the way…
nop
  • 4,711
  • 6
  • 32
  • 93
0
votes
0 answers

improper printf in semaphore

i have a log function which is called just after sem_post(&sem_ptr): sem_post(&sem_ptr); write(STD_OUT, "blah", 5); when i use these sequence of code, i mean calling write after sem_post, sometimes the "blah" string is printed twice, but when i…
CaptainHb
  • 13
  • 4
1 2 3
99
100