Commit 0bbf971
committed
vm: keep the interpreter registry usable across bootstrap, drop and fork
Three gaps where the registry did not describe the interpreters that actually
exist, each of which hides an interpreter from the collector's stop-the-world.
Register before `initialize()`. Registration ran as the last step of
`initialize_vm`, so the whole bootstrap — which executes Python bytecode and
allocates GC-tracked objects — was invisible to `live_interpreter_states()`.
It still cannot run any earlier than this: the init hooks take
`PyRc::get_mut` on the state, which fails as soon as the registry holds a weak
reference to it.
Stop unregistering in `Interpreter::drop`. The handle does not decide the
interpreter's lifetime — every `ThreadedVirtualMachine` from `new_thread()`
holds its own `PyRc<PyGlobalState>` — so an interpreter with running workers
disappeared from the registry while its threads kept mutating the object graph.
The entries are weak, so lifetime is already correct without the removal; dead
entries are now reaped when registering instead. Interpreters are consequently
released rather than unregistered at a fixed point, so the two tests asserting
disappearance now wait for it: a collection in progress legitimately holds a
reference to every live interpreter.
Repair other interpreters after fork. `py_os_after_fork_child` only fixed the
forking interpreter, leaving every other one with slots for threads that did
not survive (still ATTACHED if they were running bytecode) plus locks and
stop-the-world flags held by them. Since a collection stops all interpreters,
the child's first collection would wait for threads that no longer exist. Reset
their locks, stop-the-world state and thread tables, drop this thread's cached
slots for them, and reinit the registry's own locks first, since enumerating
interpreters now takes them.
Tests: test_gc, test_threading and test_fork1 pass, as do the vm tests in both
the threading and default configurations.
Assisted-by: Claude Code:claude-opus-4-81 parent c1ba891 commit 0bbf971
4 files changed
Lines changed: 126 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
630 | 630 | | |
631 | 631 | | |
632 | 632 | | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
633 | 640 | | |
634 | 641 | | |
635 | 642 | | |
| |||
639 | 646 | | |
640 | 647 | | |
641 | 648 | | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
642 | 655 | | |
643 | 656 | | |
644 | 657 | | |
| |||
727 | 740 | | |
728 | 741 | | |
729 | 742 | | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
730 | 790 | | |
731 | 791 | | |
732 | 792 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
203 | 210 | | |
204 | 211 | | |
205 | 212 | | |
| |||
209 | 216 | | |
210 | 217 | | |
211 | 218 | | |
212 | | - | |
213 | 219 | | |
214 | 220 | | |
215 | 221 | | |
| |||
380 | 386 | | |
381 | 387 | | |
382 | 388 | | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | 389 | | |
390 | 390 | | |
391 | 391 | | |
| |||
812 | 812 | | |
813 | 813 | | |
814 | 814 | | |
815 | | - | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
816 | 836 | | |
817 | 837 | | |
818 | 838 | | |
| |||
822 | 842 | | |
823 | 843 | | |
824 | 844 | | |
825 | | - | |
| 845 | + | |
826 | 846 | | |
827 | 847 | | |
828 | 848 | | |
| |||
1203 | 1223 | | |
1204 | 1224 | | |
1205 | 1225 | | |
1206 | | - | |
| 1226 | + | |
1207 | 1227 | | |
1208 | | - | |
| 1228 | + | |
1209 | 1229 | | |
1210 | 1230 | | |
1211 | 1231 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
153 | 158 | | |
154 | 159 | | |
155 | 160 | | |
| |||
159 | 164 | | |
160 | 165 | | |
161 | 166 | | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
| |||
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
201 | 218 | | |
202 | 219 | | |
203 | 220 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
946 | 946 | | |
947 | 947 | | |
948 | 948 | | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
949 | 962 | | |
950 | 963 | | |
951 | 964 | | |
| |||
0 commit comments