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
25
votes
2 answers

How to explain the reentrant RuntimeError caused by printing in signal handlers?

Code: # callee.py import signal import sys import time def int_handler(*args): for i in range(10): print('INTERRUPT', args) sys.exit() if __name__ == '__main__': signal.signal(signal.SIGINT, int_handler) …
hsfzxjy
  • 1,242
  • 4
  • 14
  • 22
25
votes
1 answer

My attributes are way too racy, what should I do?

In a linux device driver, creating sysfs attributes in probe is way too racy--specifically, it experiences a race condition with userspace. The recommended workaround is to add your attributes to various default attribute groups so they can be…
Gavin S. Yancey
  • 1,216
  • 1
  • 13
  • 34
25
votes
7 answers

Atomic operations in Django?

I'm trying to implement (what I think is) a pretty simple data model for a counter: class VisitorDayTypeCounter(models.Model): visitType = models.CharField(max_length=60) visitDate = models.DateField('Visit Date') counter =…
Tony Arkles
  • 1,946
  • 13
  • 14
25
votes
4 answers

Why would try/finally rather than a "using" statement help avoid a race condition?

This question relates to a comment in another posting here: Cancelling an Entity Framework Query I will reproduce the code example from there for clarity: var thread = new Thread((param) => { var currentString = param as string; …
Mike Nunan
  • 429
  • 4
  • 8
25
votes
2 answers

Working with a global singleton in Flask (WSGI), do I have to worry about race conditions?

The hello world demo for Flask is: from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run() What if I modified this like so: from flask import Flask app =…
Buttons840
  • 9,239
  • 15
  • 58
  • 85
24
votes
3 answers

Race condition on x86

Could someone explain this statement: shared variables x = 0, y = 0 Core 1 Core 2 x = 1; y = 1; r1 = y; r2 = x; How is it possible to have r1 == 0 and r2 == 0 on x86 processors? Source "The Language of Concurrency" by Bartosz…
Ation
  • 731
  • 7
  • 16
24
votes
5 answers

How does the JVM internally handle race conditions?

If multiple threads try to update the same member variable, it is called a race condition. But I was more interested in knowing how the JVM handles it internally if we don't handle it in our code by making it synchronised or something else? Will it…
krmanish007
  • 6,749
  • 16
  • 58
  • 100
23
votes
2 answers

Why does printf() in the parent almost always win the race condition after fork()?

There is a somewhat famous Unix brain-teaser: Write an if expression to make the following program print Hello, world! on the screen. The expr in if must be a legal C expression and should not contain other program structures. if (expr) …
比尔盖子
  • 2,693
  • 5
  • 37
  • 53
23
votes
1 answer

How do browsers handle HTTP keepalive race condition?

There exists a known race condition in the HTTP keepalive mechanism: HTTP KeepAlive connection closed by server but client had sent a request in the mean time https://github.com/mikem23/keepalive-race As I understand, I need my HTTP client either…
speller
  • 1,641
  • 2
  • 20
  • 27
23
votes
8 answers

Ways to Find a Race Condition

I have a bit of code with a race condition in it... I know that it is a race condition because it does not happen consistently, and it seems to happen more often on dual core machines. It never happens when I'm tracing. Although, there is a…
23
votes
2 answers

Chrome Extension error: "Unchecked runtime.lastError while running browserAction.setIcon: No tab with id"

I'm coding my Google Chrome Extension where I set the app's icon from the background script as such: try { objIcon = { "19": "images/icon19.png", "38": "images/icon38.png" }; chrome.browserAction.setIcon({ path:…
c00000fd
  • 20,994
  • 29
  • 177
  • 400
21
votes
3 answers

Handling race condition in model.save()

How should one handle a possible race condition in a model's save() method? For example, the following example implements a model with an ordered list of related items. When creating a new Item the current list size is used as its position. From…
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
19
votes
2 answers

Atomic UPDATE to increment integer in Postgresql

I'm trying to figure out if the query below is safe to use for the following scenario: I need to generate sequential numbers, without gaps. As I need to track many of them, I have a table holding sequence records, with a sequence integer column. To…
19
votes
3 answers

Django: IntegrityError during Many To Many add()

We run into a known issue in django: IntegrityError during Many To Many add() There is a race condition if several processes/requests try to add the same row to a ManyToManyRelation. How to work around this? Envionment: Django 1.9 Linux…
guettli
  • 25,042
  • 81
  • 346
  • 663
19
votes
2 answers

ReactJS concurrent SetState race condition

I have a component structure like this Idea is that actions on components in block E are processed by a function of component A to state of A and than…
akaprog
  • 191
  • 1
  • 1
  • 3