|
5 | 5 |
|
6 | 6 | from ast import Or |
7 | 7 | from functools import partial |
8 | | -from threading import Barrier, Thread |
| 8 | +from threading import Barrier, Event, Thread |
9 | 9 | from unittest import TestCase |
10 | 10 |
|
11 | 11 | try: |
@@ -437,6 +437,45 @@ def writer(): |
437 | 437 |
|
438 | 438 | threading_helper.run_concurrently([reader, writer]) |
439 | 439 |
|
| 440 | + def test_racing_dealloc_and_split_dict_resize(self): |
| 441 | + # gh-158198: an object's inline-values __dict__ escapes to another |
| 442 | + # thread, which converts it to a combined table (by adding a non-str |
| 443 | + # key) while the owning thread drops the last reference to the object. |
| 444 | + NATTRS = 20 |
| 445 | + |
| 446 | + class C: |
| 447 | + def __init__(self): |
| 448 | + for i in range(NATTRS): |
| 449 | + setattr(self, f"a{i}", i) |
| 450 | + |
| 451 | + def make_pair(): |
| 452 | + slot = [None] |
| 453 | + done = Event() |
| 454 | + |
| 455 | + def owner(): |
| 456 | + try: |
| 457 | + for j in range(2000): |
| 458 | + o = C() |
| 459 | + for i in range(NATTRS): |
| 460 | + self.assertEqual(getattr(o, f"a{i}"), i) |
| 461 | + slot[0] = o.__dict__ |
| 462 | + for _ in range(j % 16): |
| 463 | + pass |
| 464 | + del o |
| 465 | + finally: |
| 466 | + done.set() |
| 467 | + |
| 468 | + def converter(): |
| 469 | + while not done.is_set(): |
| 470 | + d = slot[0] |
| 471 | + if d is not None: |
| 472 | + d[0] = 0 |
| 473 | + slot[0] = None |
| 474 | + |
| 475 | + return [owner, converter] |
| 476 | + |
| 477 | + threading_helper.run_concurrently(make_pair() + make_pair()) |
| 478 | + |
440 | 479 | def test_racing_dict_update_and_method_lookup_with_inline_values(self): |
441 | 480 | # gh-149816: sub-case 108 |
442 | 481 | # The race below checks that a detached dict is still valid |
|
0 commit comments