-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-118362: Fix thread safety around lookups from the type cache in the face of concurrent mutators #118454
gh-118362: Fix thread safety around lookups from the type cache in the face of concurrent mutators #118454
Changes from 3 commits
c688d6b
8074d0d
9367312
179dcd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import unittest | ||
|
||
from threading import Thread | ||
from multiprocessing.dummy import Pool | ||
from unittest import TestCase | ||
|
||
from test.support import is_wasi | ||
|
||
|
||
NTHREADS = 6 | ||
BOTTOM = 0 | ||
TOP = 1000 | ||
ITERS = 100 | ||
colesbury marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class A: | ||
attr = 1 | ||
|
||
@unittest.skipIf(is_wasi, "WASI has no threads.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, probably better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. iOS and Android do both have working threading, but they don't support subprocesses. In theory The simplest solution is probably to use |
||
class TestType(TestCase): | ||
def test_attr_cache(self): | ||
def read(id0): | ||
for _ in range(ITERS): | ||
for _ in range(BOTTOM, TOP): | ||
A.attr | ||
|
||
def write(id0): | ||
for _ in range(ITERS): | ||
for _ in range(BOTTOM, TOP): | ||
# Make _PyType_Lookup cache hot first | ||
A.attr | ||
A.attr | ||
x = A.attr | ||
x += 1 | ||
A.attr = x | ||
|
||
|
||
with Pool(NTHREADS) as pool: | ||
pool.apply_async(read, (1,)) | ||
pool.apply_async(write, (1,)) | ||
pool.close() | ||
pool.join() | ||
|
||
def test_attr_cache_consistency(self): | ||
class C: | ||
x = 0 | ||
|
||
DONE = False | ||
def writer_func(): | ||
for i in range(3000): | ||
C.x | ||
C.x | ||
C.x += 1 | ||
nonlocal DONE | ||
DONE = True | ||
|
||
def reader_func(): | ||
while True: | ||
# We should always see a greater value read from the type than the | ||
# dictionary | ||
a = C.__dict__['x'] | ||
b = C.x | ||
self.assertGreaterEqual(b, a) | ||
|
||
if DONE: | ||
break | ||
|
||
self.run_one(writer_func, reader_func) | ||
|
||
def test_attr_cache_consistency_subclass(self): | ||
class C: | ||
x = 0 | ||
|
||
class D(C): | ||
pass | ||
|
||
DONE = False | ||
def writer_func(): | ||
for i in range(3000): | ||
D.x | ||
D.x | ||
C.x += 1 | ||
nonlocal DONE | ||
DONE = True | ||
|
||
def reader_func(): | ||
while True: | ||
# We should always see a greater value read from the type than the | ||
# dictionary | ||
a = C.__dict__['x'] | ||
b = D.x | ||
self.assertGreaterEqual(b, a) | ||
|
||
if DONE: | ||
break | ||
|
||
self.run_one(writer_func, reader_func) | ||
|
||
def run_one(self, writer_func, reader_func): | ||
writer = Thread(target=writer_func) | ||
readers = [] | ||
for x in range(30): | ||
reader = Thread(target=reader_func) | ||
readers.append(reader) | ||
reader.start() | ||
|
||
writer.start() | ||
writer.join() | ||
for reader in readers: | ||
reader.join() | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix an issue where the type cache can expose a previously accessed attribute | ||
when a finalizer is run. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.