10000 gh-120198: Fix crash when two thread edit __class__ by Fidget-Spinner · Pull Request #120195 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-120198: Fix crash when two thread edit __class__ #120195

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

Merged
merged 12 commits into from
Jun 11, 2024
Prev Previous commit
Next Next commit
add test
  • Loading branch information
Fidget-Spinner committed Jun 7, 2024
commit bda9545ac413922c49b91fad870e12990258ffac
7 changes: 5 additions & 2 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,11 @@ PyAPI_FUNC(PyStatus) _PyInterpreterState_New(

#define RARE_EVENT_INTERP_INC(interp, name) \
do { \
/* saturating add */ \
if (interp->rare_events.name < UINT8_MAX) interp->rare_events.name++; \
/* saturating add */ \
int val = FT_ATOMIC_LOAD_UINT8_RELAXED(interp->rare_events.name); \
if (val < UINT8_MAX) { \
FT_ATOMIC_STORE_UINT8_RELAXED(interp->rare_events.name, val+1); \
} \
RARE_EVENT_STAT_INC(name); \
} while (0); \

Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_free_threading/test_type.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
import unittest

from concurrent.futures import ThreadPoolExecutor
Expand Down Expand Up @@ -95,6 +96,32 @@ def reader_func():

self.run_one(writer_func, reader_func)

def test___class___modification(self):
class Foo:
pass

class Bar:
pass

thing = Foo()
def work():
foo = thing
for _ in range(10000):
foo.__class__ = Bar
type(foo)
foo.__class__ = Foo
type(foo)


threads = []
for i in range(NTHREADS):
thread = threading.Thread(target=work)
thread.start()
threads.append(thread)

for thread in threads:
thread.join()

def run_one(self, writer_func, reader_func):
writer = Thread(target=writer_func)
readers = []
Expand Down
0