race-condition

Understanding goroutines

ε祈祈猫儿з 提交于 2019-11-27 18:20:18
I'm trying to understand concurrency in Go. In particular, I wrote this thread-unsafe program: package main import "fmt" var x = 1 func inc_x() { //test for { x += 1 } } func main() { go inc_x() for { fmt.Println(x) } } I recognize that I should be using channels to prevent race conditions with x , but that's not the point here. The program prints 1 and then seems to loop forever (without printing anything more). I would expect it to print an infinite list of numbers, possibly skipping some and repeating others due to the race condition (or worse -- printing the number while it is being

Atomic increment of a counter in django

喜夏-厌秋 提交于 2019-11-27 17:41:37
I'm trying to atomically increment a simple counter in Django. My code looks like this: from models import Counter from django.db import transaction @transaction.commit_on_success def increment_counter(name): counter = Counter.objects.get_or_create(name = name)[0] counter.count += 1 counter.save() If I understand Django correctly, this should wrap the function in a transaction and make the increment atomic. But it doesn't work and there is a race condition in the counter update. How can this code be made thread-safe? Oduvan New in Django 1.1 Counter.objects.get_or_create(name = name) Counter

Helgrind (Valgrind) and OpenMP (C): avoiding false positives?

我怕爱的太早我们不能终老 提交于 2019-11-27 17:06:25
问题 The documentation for the Valgrind thread error detection tool Helgrind, found here warns that, if you use GCC to compile your OpenMP code, GCC's OpenMP runtime library ( libgomp.so ) will cause a chaos of false positive reports of data races, because of its use of atomic machine instructions and Linux futex system calls instead of POSIX pthreads primitives. It tells you that you can solve this problem, however, by recompiling GCC with the --disable-linux-futex configuration option. So I

Race conditions in django

不打扰是莪最后的温柔 提交于 2019-11-27 17:05:15
Here is a simple example of a django view with a potential race condition: # myapp/views.py from django.contrib.auth.models import User from my_libs import calculate_points def add_points(request): user = request.user user.points += calculate_points(user) user.save() The race condition should be fairly obvious: A user can make this request twice, and the application could potentially execute user = request.user simultaneously, causing one of the requests to override the other. Suppose the function calculate_points is relatively complicated, and makes calculations based on all kinds of weird

Can node.js code result in race conditions?

北战南征 提交于 2019-11-27 13:12:49
From what I read, race conditions occur when different threads try to change a shared variable, which can result in a value that's not possible with any serial order of execution of those threads. But code in node.js runs in a single thread, so, does that mean code written in node.js is free of race conditions? Hector Correa Yes. Node.js can run into race conditions as soon as you start sharing resources. I mistakenly also thought you couldn't get race conditions in Node.js because it's single threaded nature, but as soon as you use a shared resource outside of node (e.g. a file from the file

How does using the try statement avoid a race condition?

▼魔方 西西 提交于 2019-11-27 12:05:39
When determining whether or not a file exists, how does using the try statement avoid a "race condition"? I'm asking because a highly upvoted answer (update: it was deleted) seems to imply that using os.path.exists() creates an opportunity that would not exist otherwise. The example given is: try: with open(filename): pass except IOError: print 'Oh dear.' But I'm not understanding how that avoids a race condition compared to: if not os.path.exists(filename): print 'Oh dear.' How does calling os.path.exists(filename) allow the attacker to do something with the file that they could not already

Can we have race conditions in a single-thread program?

余生长醉 提交于 2019-11-27 11:57:09
You can find on here a very good explanation about what is a race condition. I have seen recently many people making confusing statements about race conditions and threads. I have learned that race conditions could only occur between threads. But I saw code that looked like race conditions, in event and asynchronous based languages, even if the program was single thread, like in Node.js, in GTK+, etc. Can we have a race condition in a single thread program? jillro All examples are in a fictional language very close to Javascript. Short: A race condition can only occur between two or more

Atomic operations in Django?

人盡茶涼 提交于 2019-11-27 10:09:10
问题 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 = models.IntegerField() When someone comes through, it will look for a row that matches the visitType and visitDate; if this row doesn't exist, it will be created with counter=0. Then we increment the counter and save. My concern is that this process is totally a race. Two

How do I obtain, and synchronize, a complete list of all X11 windows?

独自空忆成欢 提交于 2019-11-27 10:02:28
问题 I want to monitor all the open windows under X11. Currently, I'm doing this as follows: Initially walking the whole tree by recursively calling XQueryTree from the root window Listening for substructure changes on the whole desktop: XSelectInput( display, root_window, SubstructureNotifyMask | PropertyChangeMask ) Handling all MapNotify, UnmapNotify and DestroyNotify events, updating my own list of windows in the process I'm mainly worried about point 1. During the recursion, XQueryTree will

Hidden threads in Javascript/Node that never execute user code: is it possible, and if so could it lead to an arcane possibility for a race condition?

℡╲_俬逩灬. 提交于 2019-11-27 09:36:35
See bottom of question for an update, based on comments/answers: This question is really about the possibility of hidden threads that do not execute callbacks. I have a question about a potential arcane scenario involving the Node Request module in which: A complete HTTP request is constructed and executed over the network (taking however many ms or even seconds) ... before a single function is executed at runtime on the local machine (typically in the nanoseconds?) - see below for details I am posting this mostly as a sanity check just to make sure I am not misunderstanding something about