Commit f963239
authored
This fixes a fairly subtle bug involving finalizers and resurrection in
debug free threaded builds: if `_PyObject_ResurrectEnd` returns `1`
(i.e., the object was resurrected by a finalizer), it's not safe to
access the object because it might still be deallocated. For example:
* The finalizer may have exposed the object to another thread. That
thread may hold the last reference and concurrently deallocate it any
time after `_PyObject_ResurrectEnd()` returns `1`.
* `_PyObject_ResurrectEnd()` may call `_Py_brc_queue_object()`, which
may internally deallocate the object immediately if the owning thread
is dead.
Therefore, it's important not to access the object after it's
resurrected. We only violate this in two cases, and only in debug
builds:
* We assert that the object is tracked appropriately. This is now moved
up betewen the finalizer and the `_PyObject_ResurrectEnd()` call.
* The `--with-trace-refs` builds may need to remember the object if
it's resurrected. This is now handled by `_PyObject_ResurrectStart()`
and `_PyObject_ResurrectEnd()`.
Note that `--with-trace-refs` is currently disabled in `--disable-gil`
builds because the refchain hash table isn't thread-safe, but this
refactoring avoids an additional thread-safety issue.
1 parent c5f925c commit f963239
File tree
3 files changed
+46
-15
lines changed- Include
- cpython
- internal
- Objects
3 files changed
+46
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
730 | 730 | | |
731 | 731 | | |
732 | 732 | | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
733 | 736 | | |
734 | 737 | | |
735 | 738 | | |
| |||
743 | 746 | | |
744 | 747 | | |
745 | 748 | | |
746 | | - | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
747 | 756 | | |
748 | 757 | | |
749 | 758 | | |
750 | 759 | | |
751 | 760 | | |
752 | 761 | | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
753 | 765 | | |
754 | 766 | | |
755 | 767 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
496 | 496 | | |
497 | 497 | | |
498 | 498 | | |
499 | | - | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
500 | 506 | | |
501 | 507 | | |
502 | | - | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
503 | 515 | | |
504 | 516 | | |
505 | 517 | | |
| |||
589 | 601 | | |
590 | 602 | | |
591 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
592 | 608 | | |
593 | 609 | | |
594 | | - | |
595 | | - | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
596 | 618 | | |
597 | 619 | | |
598 | | - | |
599 | | - | |
600 | | - | |
601 | | - | |
602 | | - | |
603 | | - | |
604 | | - | |
605 | | - | |
| 620 | + | |
| 621 | + | |
606 | 622 | | |
607 | 623 | | |
608 | 624 | | |
| |||
2601 | 2617 | | |
2602 | 2618 | | |
2603 | 2619 | | |
2604 | | - | |
2605 | | - | |
2606 | 2620 | | |
2607 | 2621 | | |
2608 | 2622 | | |
| 2623 | + | |
2609 | 2624 | | |
2610 | 2625 | | |
2611 | 2626 | | |
| |||
2621 | 2636 | | |
2622 | 2637 | | |
2623 | 2638 | | |
| 2639 | + | |
2624 | 2640 | | |
2625 | 2641 | | |
| 2642 | + | |
| 2643 | + | |
2626 | 2644 | | |
2627 | 2645 | | |
2628 | 2646 | | |
| |||
0 commit comments