News for hackers. Stuff that matters.
Re:Data races in Python, despite the Global Interpreter Lock by samwillis on Monday February 21, 2022 @04:50PM (#30421093)
Interestingly if you change the line to `counter += 1` it still has the race condition. I'm not sure how the byte code is different for the two options but it doesn't make a difference, I had hoped it would.
Re:Data races in Python, despite the Global Interpreter Lock by miohtama on Monday February 21, 2022 @04:57PM (#30421162)
In the Python VM there is no atomic increment bytecode. So `counter += 1` should be exactly same as `counter = counter + 1`.

Here is an example what thread safe increment looks like in Python:

https://julien.danjou.info/atomic-lock-free-counters-in-pyth...

You need to lock it explicitly.

Note that `INC` instructor for x86 architecture needs explicit hints/locks as well, so this should suprise anyone:

https://stackoverflow.com/q/10109679

Re:Data races in Python, despite the Global Interpreter Lock by nneonneo on Monday February 21, 2022 @06:05PM (#30421928)
A bit more detail: there is an atomic addition bytecode, called INPLACE_ADD, which is why something like “a_list += [1]” is actually atomic. The problem is that integers are immutable, so “counter += 1” involves a (non-atomic) load and store of counter, and that’s where the race is.

Hypothetically, if Python had a built-in mutable integer type, the increment could be made atomic.

Re:Data races in Python, despite the Global Interpreter Lock by jasonhansel on Monday February 21, 2022 @05:57PM (#30421832)
In general, "+=" probably needs to be non-atomic to support "__add__" overloading; Python wouldn't be able to call arbitrary "__add__" methods in a way that could guarantee atomicity.