The Python example has two concurrent accesses of the same memory (the global counter) where one of them is a write. That seems to meet the definition.
We're also seeing the telltale symptoms of a data race. Note that we don't need to see slicing to qualify as a data race; as plenty cache-line-aligned data races don't exhibit slicing (not implying you were claiming this, just mentioning it for anyone curious).
The Rustonomicon definition also adds "they are unsynchronized", perhaps that's the part we disagree on.
I see this code as unsynchronized; there is no mutex or atomic increment happening around this operation. One could reasonably disagree, depending on what level they're looking at: statement-level, instruction-level, opcode-level, assembly level, uop level, or a higher "logical" level.
Either way, the point of the article is to help debunk a common misperception that the GIL helps users with their concurrency in some way.
>>> import dis
>>> def increment_counter():
... counter = counter + 1
>>> dis.dis(increment_counter)
2 0 LOAD_FAST 0 (counter)
2 LOAD_CONST 1 (1)
4 BINARY_ADD
6 STORE_FAST 0 (counter)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
A data race looks like LOAD_FAST from thread 1 begins
STORE_FAST from thread 2 begins
memory is partially overwritten
LOAD_FAST from thread 1 finishes (got partially written garbage)
STORE_FAST from thread 2 finishes
This is not a data race: LOAD_FAST from thread 1
LOAD_FAST from thread 2
STORE_FAST from thread 1
STORE_FAST from thread 2
There's no LOAD_FAST + STORE_FAST "transaction" here, GIL has no such guarantee.Edit: You also need to realize that in Python, that BINARY_ADD can be anything:
import time
class SlowCounter(int):
def __add__(self, other):
time.sleep(1)
return SlowCounter(super().__add__(other))
counter = SlowCounter()
counter += 1
The GIL isn't going to wait around for the __add__.Ah, but no, there is never concurrent access to that memory address, that's what the GIL prevents. The loads/stores to that memory address happen only while the GIL is held. The "concurrency" being used here is much more narrow than the dictionary definition of concurrency and the way it is usually discussed in the Python world.
That said, reads and writes can be atomic and still subject to race conditions which is what your example demonstrates. All your article shows is that data race freedom does not imply race condition freedom and this true not only of Python but of all programming languages.
That's what the C++ standard says, linked from the wikipedia article you linked. I realize that C++ may not be completely applicable to Python, but this fits my prior understanding of the topic.
Since there's no formal semantics to consult here, no actual alternate definition of concurrency, and the definition of data race fits, I can't say that the article is incorrect.
This is going long and I need to get dinner, but thank you for the great discussion!
"The parts of this definition relating to signal handlers are idiosyncratic to C++ and are not typical of definitions of data race."
Now we can definitely go into the nature of signal handlers in C++, but suffice it to say signal handlers are not considered to be executed from within the same thread as the main program. A signal handler is executed from within a special execution context that is independent of the rest of the program and whose behavior is distinct from typical threads. Suffice to say, other than specific idiosyncrasies, a signal handler is treated as if it were a separate thread of execution (even though technically an implementation does not have to actually spawn a separate thread for a signal handler).
In Python at this level, we don't even have a notion of a memory address. In fact, the memory address of where the integer itself is stored is going to be different, since integers in Python are boxed and immutable. Even if that weren't the case and these were direct memory accesses as you'd see in a lower level language, this would still not be able to introduce a data race, as the GIL prevents these two threads from ever actually running at the same time (while executing pure-python code that does not otherwise release the GIL. A poorly written extension or buggy code outside the GIL could cause data races, but that's a bit of a rabbithole). They'll hop back and forth as they acquire/release the GIL, and so the memory accesses can never race.
The Wikipedia article quotes part of the C++ standard which defines "potentially concurrent", but even with that definition, the article's understanding of data race stands, because these two operations are indeed unsequenced w.r.t. each other.
I don't see a reason to depart from the usual understanding of "concurrent", but I'm open to being convinced!
No code that runs under the GIL is ever run concurrently. All of the code in the example holds the GIL, there's never any concurrency in your article! With no concurrency, there can be no data race.
(Edited in response to your edit)
For example it's possible on a single core CPU to have one thread operate on the higher bits of a 32-bit memory location then yield to second thread that operates on the lower 32-bits of the same memory location so that the end result is a clobbered write. At no point do the two threads run simultaneously and yet this would still be an example of a data race.
Python's GIL makes such an access impossible on any platform it runs on.
I'd be interested to see a concrete example of how it could actually happen on a real currently-deployed system. (I'm not saying people should normally write such code, but things do need to ground out in a concrete machine model at some point, even if it's just for the sake of implementing higher level language semantics.)
(Edit: replied to you with similar comment in sibling thread...)
In safe Rust data races are not possible and yet it is still possible for a Rust program to exhibit the behavior in your article, because your article is not an example of a data race but rather a race condition. Note that in the source you reference, it explicitly states that while data races are protected, race conditions are not.
The scenario I gave, where performing an operation such as "x = x + 1" can result in x decreasing in value can only be caused by an overflow, or a data race. No interpretation of a Python or safe Rust program will ever produce a decreasing value of x when evaluating "x = x + 1" (ignoring Rust's overflow behavior) since data races are not possible. In Java or C++, since data races are possible it is also possible that "x = x + 1" produces a decreasing value of x (that is not due to overflow).
Finally here is a good article that lays out the differences between data races and race conditions:
https://blog.regehr.org/archives/490
[1] https://doc.rust-lang.org/nomicon/races.html
[2] https://docs.oracle.com/cd/E19205-01/820-0619/geojs/index.ht...
[3] https://en.cppreference.com/w/cpp/language/memory_model
[4] https://en.wikipedia.org/wiki/Race_condition#Data_race