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.
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:
Hypothetically, if Python had a built-in mutable integer type, the increment could be made atomic.