`_py_abc` Python implementation of `abc` is not thread-safe
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
The update to the invalidation counter is not thread-safe and can lose updates in some Python implementations:
Failures seen on:
- Python 3.14t
- Python 3.9
- pypy3.10
- pypy3.11
But not on Python 3.10-3.14 with GIL due to limited GIL switch opportunities.
For example, consider the following repro, adapted from test_abc.test_registration_basics:
import _py_abc as abc # Use Python implementation of ABCs!!
import threading
import os
import sys
sys.setswitchinterval(1e-6)
N = 5
def run(b):
b.wait()
class A(metaclass=abc.ABCMeta):
pass
A.register(int)
if not isinstance(42, A):
print("Oops!")
os._exit(1)
def main():
for _ in range(10000):
threads = []
b = threading.Barrier(N)
for _ in range(N):
t = threading.Thread(target=run, args=(b,))
threads.append(t)
t.start()
for t in threads:
t.join()
if __name__ == "__main__":
main()
Linked PRs
- gh-130131
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Lib/_py_abc.py around the invalidation-counter update and reproduce the race using the threaded example in the issue. Run Lib/test/test_abc.py, especially test_registration_basics; done means concurrent registrations no longer lose invalidation updates and the affected Python implementations pass the relevant tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100