News for hackers. Stuff that matters.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Thursday October 03, 2019 @09:41AM (#21146809)
But many race conditions are harmless. 'Fixing' those that are with extra synchronisation is not a good use of anyone's time.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by gpderetta on Thursday October 03, 2019 @12:17PM (#21148678)
by definition data races in C and C++ are UB. Even if you believe that the compiler cannot not possibly miscompile your code [1], a large quantity of these false positives will make data race detection tools (like the one used by G) less useful. Just mark your shared memory locations as atomic, you can always use relaxed ordering and non-atomic RMW operations if you worry about the cost of the extra synchronization.

[1] these sort of assumptions of course tend not to age well.

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Friday October 04, 2019 @08:08AM (#21156765)
Data races are. Race conditions are not.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by gpderetta on Friday October 04, 2019 @12:59PM (#21159590)
True.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by sanxiyn on Thursday October 03, 2019 @10:11AM (#21147136)
Note that Phoronix is being imprecise here and those hundreds are in fact data races (with no false positives), which are never okay.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Friday October 04, 2019 @05:56AM (#21156199)
> data races (with no false positives), which are never okay

This just isn’t true.

Here’s a concrete example - many JIT compilers use counters to work out when to compile a method. They allow data races in updates to the counters because performance is more important than correctness for them and the impact of a lost write is basically zero. It’s only a bug if you decide it’s a bug.

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by dekhn on Thursday October 03, 2019 @10:07AM (#21147092)
I've never heard the term "race condition" refer to something that was harmless. Can you point to some examples in the literature?
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Thursday October 03, 2019 @10:11AM (#21147127)
Even Wikipedia says it's not always a bug

> A race condition or race hazard is the condition of an electronics, software, or other system where the system's substantive behavior is dependent on the sequence or timing of other uncontrollable events. It becomes a bug when one or more of the possible behaviors is undesirable.

Or read the canonical Encyclopaedia of Parallel Computing page 1693

> [A race condition is when there is] some order among events [where the] order is not determined by the program

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by shaklee3 on Thursday October 03, 2019 @10:55AM (#21147733)
Some race conditions are intentional at the expense of knowing that the data would not be perfect. For example, if you have a structure of different metrics and one thread updates them while another thread reads them, clearly the best way to handle that is to lock while writing and reading so you always get the intended results. However, if you don't necessarily care about all of your metrics being snapshotted at the exact same point in time, you can just have the other thread share the variables knowing that this race condition will actually improve performance a little bit. Obviously you need to take care of things like words sizes that take multiple loads and stores.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by dekhn on Thursday October 03, 2019 @11:16AM (#21147999)
Hmm. I thought data races due to concurrent thread access are actually undefined behavior (up to and included termination of the program).
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by schoen on Thursday October 03, 2019 @12:29PM (#21148813)
Are you thinking of a particular programming language specification here? It seems like one programming language might define this this way, while another might simply say that the order of the events is not guaranteed (and may not be the one that the programmer expected), but not that the events themselves may do something other than the normal language semantics provide.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by jcranmer on Thursday October 03, 2019 @02:14PM (#21150072)
C++ and C both explicitly say that data races are undefined behavior, not unspecified behavior.

Data races are one of those cases where "anything may go" undefined behavior is actually quite necessary. Actual hardware memory models (particularly on "weak" architectures like ARM or PowerPC, or especially the infamously weak Alpha) result in cases where the order of memory traffic is not a consistent view between different threads. Formally specifying the hardware memory model is actually surprisingly difficult, made all the more difficult when you want to give a hardware-agnostic definition [1]. And should you want to introduce hardware transactional memory, break down in despair at the complexity [2].

The great breakthrough in memory models is the definition of the "data-race-free" model. This model says that, if memory accesses are properly protected with locks (i.e., there are no "data races" [3]), you cannot observe a violation of sequential consistency, which makes reasoning much easier. Java used this model for its corrected memory model in Java 5, and then C++11 adapted Java's memory model, and C11 took C++11's model verbatim. The upshot of this approach is that, if you make data races undefined behavior, you don't have to try to figure out how typical memory optimizations, both by the compiler and by the physical hardware, affect visible memory. Trying to work out the effects of these optimizations on the memory system is extraordinarily difficult, and the benefits of doing so are manifestly unclear, since most programmers will need to properly synchronize their code anyways.

[1] The C++ memory model attempts to do this with atomic memory references. The resulting specification (in C++11) is generally considered to be wrong, and the question of how to fix it is still up in the air as of right now.

[2] PLDI 2018 had the first formal semantics that combined transactional memory and weak memory semantics, and went on to prove that ARM's hardware lock elision was broken. This should be showing just how difficult this is to grasp even for theoreticians pushing the boundary, let alone a language trying to make it accessible to typical programmers.

[3] There are two definitions of "data race" going on here. Vernacular definitions usually define it as accesses not protected by synchronization, which permit "benign" data races for regular loads/stores. C/C++ tweaks the definition so as to use it to proscribe undefined behavior, so "benign" data race is oxymoronic in those languages. However, the use of memory_order_relaxed is intended to indicate a permitted data race in the first sense but not the second sense.

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by schoen on Thursday October 03, 2019 @02:42PM (#21150497)
Thanks for the tremendous detail on this point.

Could you or someone else please give a minimal example of an undefined access in C, just so that I can be sure of whether we're talking about the same phenomena here?

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by jcranmer on Thursday October 03, 2019 @03:15PM (#21150903)
A minimal program (using C11 threads):

   #include <threads.h>
   int x;

   int do_thread(void *) {
     for (int i = 0; i < 10000; i++)
       x = i;
     return 0;
   }
   int main() {
     thrd_t t;
     thrd_create(&t, do_thread, NULL);
     do_thread(NULL);
     thrd_join(&t, NULL);
     return 0;
   }
There is an undefined data race on x, since it is simultaneously accessed from two threads without any synchronization. Replace the declaration of x with `_Atomic int x;`, and the resulting code is well-defined.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by dekhn on Friday October 04, 2019 @08:59AM (#21157159)
thanks, this comment (point 3 in particular) did a great service in clearing up the disconnect.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by dekhn on Friday October 04, 2019 @08:58AM (#21157154)
no programming languages, just the hardware. I think the only way to reasonably think about data races is at the hardware instruction/memory level, not the programming language.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by gpderetta on Thursday October 03, 2019 @12:17PM (#21148687)
they very much are.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by shaklee3 on Thursday October 03, 2019 @02:00PM (#21149893)
Can you point to a source on that? If you have two pthreads in C accessing the same variable, they will definitely not necessarily see the same load/store order, but it's perfectly valid as a form of lazy evaluation to assume the value eventually can be seen.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by gpderetta on Thursday October 03, 2019 @02:29PM (#21150299)
Can't check chapter and verse on the standard right now. Will you take cppreference[1] as authoritative? See the section on data races. The C11 memory model only differ minimally form C++11.

[1] https://en.cppreference.com/w/cpp/language/memory_model

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by shaklee3 on Thursday October 03, 2019 @06:40PM (#21153015)
I'm aware of c++ memory models, but this is exactly what I was talking about. The actual behavior is defined by the hardware, in the programming language is an abstraction on top of that. For certain sizes, you will not get a load or a store that is only partially there. So on x64, you can safely share data between threads knowing that the observed order of that data is completely undefined. But in cases of looking into a view of statistics, that may be perfectly acceptable given the trade-offs.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by jcranmer on Thursday October 03, 2019 @08:35PM (#21153785)
> The actual behavior is defined by the hardware, in the programming language is an abstraction on top of that.

This is a very dangerous way to understand semantics of programming languages. Programming languages are usually defined via some sort of operational semantics on an abstract machine which occasionally bears some resemblance to how processor semantics are defined. But sometimes there is a massive disconnect between processor semantics and language semantics--and perhaps nowhere is that disconnect greater than memory.

In hardware, there is a firm distinction between memory and registers. Any language which approximates hardware semantics (which includes any mid-level or low-level compiler IR) is going to mirror this distinction with the notion of a potentially infinite register set (if pre-register allocation) and use of explicit instructions to move values to and from memory. But in every high-level language, there is no concept of a memory/register distinction: just values floating around in a giant bag of them, often no notion of loads or stores. You can approximate this by annotating values (as C does, with the volatile specifier), but the resulting semantics end up being muddy because you're relying on an imperfect mapping to get to the intended semantics (don't delete this load/store). Optimization that screws up the mapping of the language memory model to the hardware memory model is anticipated, even 30 years ago--that's why C has a volatile keyword.

It is possible, if you are very careful, to write C code that exposes the underlying hardware model that allows you to reason about your program using that model. But this is not typical C code, and instead requires modifying your code in such a way that you make the intended loads and stores explicit. And if you write your code in the typical way and expect it to map cleanly to hardware semantics, you're going to have a bad time.

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by shaklee3 on Thursday October 03, 2019 @11:22PM (#21154511)
Coming from a world where latency and performance is paramount, I think it's perfectly acceptable to write code targeted at a specific architecture you know won't change. I understand your points though, and it's definitely not a style you should use in 99% of programming.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by IggleSniggle on Thursday October 03, 2019 @11:27AM (#21148103)
I would call this sampling that could turn into a race condition if it stops being treated as sampling.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by petschge on Thursday October 03, 2019 @10:46AM (#21147599)
Random imagined example: Two files with different names being created in the same directory. The race decides which one gets which inode number, but both are created correctly and successfully. That would be a race condition, but as long as only the assignment of inode numbers changes between outcomes of the race nobody would care. But again, this is just a contrived example.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by Confusion on Thursday October 03, 2019 @11:11AM (#21147937)
Examples in the literature? This is not a bloody academic exercise?!

I use the term 'race condition' about once a week to point out something may happen before or after something else, because the things are happening in parallel.

Simple example:

me: I just uploaded the file to our shared dropbox

you, refreshing: I don't see it yet

me: race condition

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by dekhn on Thursday October 03, 2019 @11:17AM (#21148008)
eventual consistency is not a race condition.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by Confusion on Friday October 04, 2019 @04:10AM (#21155751)
Of course it is, that's exactly the point. 'Eventual consistency' is the name for situations where a race condition is acceptable and even a feature. It doesn't matter whether it are threads, processes or entirely different computers that are racing. Whether the data is shared via CPU cache, memory, disk or a network. We don't call it "failed eventual consistency" when a data race occurs.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by IggleSniggle on Thursday October 03, 2019 @11:31AM (#21148145)
I would never call this a race condition, but mostly because it does not cause an unrecoverable / system crashing failure state (you and your coworker have a protocol to resolve the inconsistent state).
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by hunter2_ on Thursday October 03, 2019 @09:51AM (#21146909)
The phrase "race condition" is usually not used to describe the harmless asynchronous code you're referring to.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Thursday October 03, 2019 @09:56AM (#21146972)
No you're thinking about 'data races'. Almost all software that interacts with the outside world, or clocks, like of course a kernel, is going to have many harmless 'race conditions'. Erlang for example, is a language extremely prone to race conditions, but not to data races.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by tsbinz on Thursday October 03, 2019 @10:04AM (#21147057)
The tool specifically looks for data races.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by ggggtez on Thursday October 03, 2019 @09:46AM (#21146854)
I think by definition, you are wrong. If the race condition was harmless then it wouldn't be a race condition.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Thursday October 03, 2019 @09:59AM (#21146990)
By who's definition? If you go by Padua's classic definition, it's any behaviour that depends on timing sequences. For example HN has a race condition in that the ID your comment gets allocated depends on when it's submitted. But that's not a bug, is it? It only becomes a bug when it's undesirable. I think, like a few people here, you're confusing it with a 'data race'.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by mffnbs on Thursday October 03, 2019 @10:17AM (#21147216)
You still understand, though, that the colloquial definition being used here is what you're referring to as a "data race." Your comments are disingenuous in that I believe you know that you're being pedantic, I just don't know why you've brought it up the multiple times.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Thursday October 03, 2019 @10:38AM (#21147506)
I didn't realise something as specific as 'race condition' could have a colloquial definition, but ok.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by pure-awesome on Thursday October 03, 2019 @11:17AM (#21148012)
For many terms of (programming) jargon, there's a strict academic definition, perhaps as defined in a paper in which the term was originally introduced, and the usage of the word in the broader (programming) community.

The latter is the colloquial definition.

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by ggggtez on Thursday October 03, 2019 @11:48PM (#21154616)
I feel like this comment is a classic example of reading a wikipedia entry instead of understanding the topic.

You can use it to refer to any race, but in reality the race you are referring to is only subjective to the user. The server is generally processing all requests in the order they are received.

A race condition usually is referring to a multithreaded system where the threads interfere with each other, causing undesired effects.

Relaxing "race condition" to refer to all causality... is such a loose definition as to give it no meaning at all.

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Friday October 04, 2019 @06:00AM (#21156214)
> I feel like this comment is a classic example of reading a wikipedia entry instead of understanding the topic

I did a PhD partially on irregular parallelism where understanding race conditions are essential for understanding the topic.

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by zzzcpan on Thursday October 03, 2019 @10:25AM (#21147317)
Didn't we go over this already? It's not any behavior that depends on timing sequences, it's only if your code assumes it can rely on timing sequences while it can't, i.e. a bug.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Thursday October 03, 2019 @10:31AM (#21147395)
> It's not any behavior that depends on timing sequences,

That's literally how 'race condition' is defined in the industry.

Re:Google Is Uncovering Hundreds of Race Conditions Within t... by zzzcpan on Thursday October 03, 2019 @11:03AM (#21147858)
Not true, it's pretty hard to encounter anyone in the industry using the phrase "race condition" without implying a bug.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by tanr54ok on Thursday October 03, 2019 @10:19AM (#21147240)
what? You need to describe what you’re saying better. How is an ID allocated a comment a “race condition”. A race condition desirable or not has to be related to some sequence of events.. ie its a race with a particular set of outcomes. What you’re describing sounds like basic cause and effect.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by chrisseaton on Thursday October 03, 2019 @10:22AM (#21147284)
Event A is your comment. Event B is my comment. If your comment gets to the server first if gets ID 1. If mine gets to the server first it gets ID 1. Which ID you get depends on timing outside your control. They race to get the ID. A race condition.
Re:Google Is Uncovering Hundreds of Race Conditions Within t... by tanr54ok on Thursday October 03, 2019 @01:19PM (#21149392)
So what? What’s missing is that unless something depends on the ordering of those ids, it really isn’t a race condition. Just plain happening of events is the inevitable passage of time, not a race condition. What is interesting about what happens downstream with those IDs?

You’re not wrong that not all race conditions are harmful, but i feel you’re doing a pretty bad job of explaining what a race condition is.