Per-interpreter runtime state and a process-wide GC stop-the-world (#8517)

* vm: add per-interpreter runtime state and interpreter registry

- vm/runtime.rs: process-global interpreter registry (monotonic ids, weak
  entries), InterpreterWhence/InterpreterInfo, process main id recording via
  main_interpreter_id(), a threading-gated owner map (store_owned_interpreter/
  take_owned_interpreter/is_owned_interpreter/owned_interpreter_count), and the
  SUPPORTS_ISOLATED_INTERPRETERS constant.
- PyGlobalState gains interpreter_id/whence/is_main and is_main_interpreter();
  PyConfig/Settings derive Clone so a subinterpreter can clone parent config.
- Interpreter: id()/whence()/is_main()/is_process_main(),
  create_subinterpreter() and create_owned_subinterpreter(); unregister on Drop.
- thread.rs: per-interpreter thread slots (INTERP_THREAD_SLOTS), slot swap when
  switching interpreters on one OS thread, cleanup keyed by interpreter id.
- Install signal handlers and init the main-thread ident only on the main
  interpreter; _thread._is_main_interpreter reflects the current interpreter.
- sys.implementation.supports_isolated_interpreters reads the constant.
- Guard the registry static for non-threading builds where rc::Weak is !Send.

Assisted-by: Claude Code:claude-opus-4-8

* gc: stop every interpreter while collecting

The generation lists are process-global, so a collection reads and frees
objects owned by every interpreter. CollectStopTheWorld stopped only the
collecting interpreter, leaving other interpreters' threads free to mutate
the same object graph during the reference-subtraction, reachability and
snapshot phases.

Stop all live interpreters instead, in runtime id order, and restart them in
reverse. The global `collecting` mutex serializes collectors process-wide, so
no second collector takes these exclusions in another order; fork acquires a
single interpreter's exclusion, so the orders cannot cycle.

- runtime: add live_interpreter_states(), ordered by interpreter id.
- StopTheWorldState methods take &PyGlobalState instead of &VirtualMachine,
  so an interpreter's world can be stopped without a VM for it; update the
  call sites in frame, _thread, posix, faulthandler and capi.
- Document in gc_state() that the collector is process-wide: gc.disable(),
  thresholds, gc.garbage and gc.get_objects() observe process-wide state, and
  a per-interpreter collector additionally needs untrack_object (called from
  default_dealloc with no VM in scope) routed to the owning interpreter.

Tests: stop_the_world_parks_threads_of_another_interpreter asserts a thread
entered in one interpreter parks another interpreter's threads (it fails
without this change), plus a collect-while-another-interpreter-churns test.

Assisted-by: Claude Code:claude-opus-4-8

* vm: scope the interpreter registry per thread without threading

The registry held `rc::Weak` in a process-global `OnceLock` and covered the
resulting `!Send`/`!Sync` with an `unsafe impl` justifying it as "non-threading
builds are single-threaded". That is not this codebase's model: `static_cell!`
is thread-local without the `threading` feature precisely so each OS thread can
own its own `Context::genesis()` and `GcState`, so two threads could reach the
same `Rc` counts through the registry.

Use `static_cell!` for the registry as well, matching `gc_state()`, and drop the
`unsafe impl`. Ids are consequently unique per registry rather than per process
in non-threading builds, which is documented on `alloc_interpreter_id`.

Move the recorded main interpreter id into the registry so it follows the same
scoping instead of living in a separate global `OnceLock`.

Also make `CollectStopTheWorld::new` accumulate into a live guard: it built a
bare `Vec` and only moved it into the restarting `Drop` type after stopping
every interpreter, so an unwind partway through the loop left the already
stopped interpreters parked forever with their exclusion held.

Assisted-by: Claude Code:claude-opus-4-8

* 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-8

* vm: attach and detach thread slots when switching interpreters

`enter_vm` decided whether to attach from `was_outermost` (an empty VM_STACK),
which held while a thread could only ever be in one interpreter. With a slot per
(thread, interpreter) pair, entering interpreter B from a thread already inside
interpreter A's section switched CURRENT_THREAD_SLOT to B's slot but attached
nothing: the thread then ran B's bytecode with B's slot DETACHED while A's slot
stayed ATTACHED. A collector stopping B force-parks the DETACHED slot and
concludes B is stopped, and then walks the object graph this thread is still
mutating.

Pair the attach/detach with the slot switch instead (≈ `_PyThreadState_Swap`):
`begin_interpreter_section` detaches the enclosing interpreter's slot, makes the
target slot current and attaches it, and `end_interpreter_section` undoes that
and re-attaches the enclosing interpreter. Both live in `set_current_vm`, which
every path making a VM current already goes through, so `enter_vm` and
`VmBootstrapGuard` no longer track outermost-ness themselves.

`nested_enter_of_subinterpreter_is_stoppable` covers this: it runs a
subinterpreter nested inside the parent's section and asserts the sub's threads
park when the sub's world is stopped. It fails with the previous
attach-at-outermost-only behavior.

Assisted-by: Claude Code:claude-opus-4-8

* vm: list only the current interpreter's subclasses

Interpreters share the context, so `class Foo(int)` in one of them pushes
onto the same `int.subclasses` every other one reads, and
`int.__subclasses__()` returned types no other interpreter can reach.

Record the creating interpreter on `HeapTypeExt` and filter
`__subclasses__` by it, the way `lookup_tp_subclasses` reads
`tp_subclasses` out of per-interpreter state for static builtin types.
Types built before any interpreter exists — the ones the shared context
creates, including the exception hierarchy — carry no id and stay visible
to every interpreter, which is what `_PyStaticType_InitBuiltin` produces
by registering the builtin subclass links once per interpreter.

The other walks over `subclasses` (version-tag invalidation, abc flag
propagation, mro updates, slot propagation) are left as they are: each
starts from a type being mutated, so from a heap type, whose subclasses
all live in the interpreter that created it.

`subinterpreter_subclasses_are_scoped_to_their_interpreter` covers this
and fails without the filter.

Assisted-by: Claude Code:claude-opus-5

* gc: give each interpreter its own collector state

The generation lists stay process-wide, because an object is untracked from
`default_dealloc`, where no interpreter is in scope to route to. What changes
is that a collection no longer acts on every interpreter's objects, and the gc
module no longer reports one interpreter's state to another.

`track_object` stamps the running interpreter into a new `gc_owner` word on the
object header, and a collection takes as candidates only the objects carrying
its own tag plus the ones carrying none. `gc_owner` fits in the padding the
header's alignment already forces, so objects do not grow; an assertion on the
header size keeps it that way.

Objects allocated with no interpreter running — everything the shared context
builds — carry no owner and stay candidates for every interpreter, which is
where they were before. Objects that outlive the interpreter that tracked them
are adopted the same way by the next full collection, rather than being left to
a collector that will never come.

`enabled`, the thresholds, the debug flags, the statistics, `gc.garbage` and
`gc.callbacks` move onto `PyGlobalState` — the last two off the shared
`Context` — so `gc.disable()`, `gc.set_threshold()`, `gc.get_stats()`,
`gc.get_objects()` and `gc.garbage` describe the interpreter that asks. The
occupancy counts behind `gc.get_count()` and `gc.get_freeze_count()` stay
process-wide: they measure how full the shared lists are. Their decrements are
saturating now, since a collection zeroes the generations it emptied while
another interpreter's objects are still sitting in them.

Stop-the-world still stops every interpreter. Unowned objects are candidates
and any interpreter can incref one, so the refcounts a collection reads are
only stable while all of them are parked.

This also fixes a deadlock it exposed: `CollectStopTheWorld` dropped its
references to the stopped interpreters while the collection still held the
generation read locks, so releasing the last reference to one — which frees its
objects, and so untracks them — waited for a write lock behind that read lock.
The references are now held until the guard itself drops.

`collections_only_reach_the_collecting_interpreter` and
`get_objects_only_reports_the_calling_interpreter` cover this and both fail
without the owner check.

Assisted-by: Claude Code:claude-opus-5

* _queue, _thread: detach before taking locks held across waits

`_queue.Semaphore` holds its mutex across the `allow_threads` condvar wait,
and `join_internal` holds a thread handle's completion mutex the same way.
Stop-the-world can stop a thread while it holds either one. The remaining
acquisitions ran attached, so a thread blocking on such a mutex had no
safepoint left to reach: the stop never completed, and the holder was never
resumed to release it.

Route those acquisitions through helpers that detach first. The fork-child
reinit paths keep their direct locks.

Assisted-by: Claude

* _io, _winapi: detach on the remaining stopped-holdable lock takes

`TextIOWrapper.__repr__` took `data` directly while every other method takes it
through `lock_opt`, which detaches. `Overlapped` holds `inner` across the
`allow_threads` in `GetOverlappedResult`, and all four of its takes were direct.

A thread stopped by stop-the-world can be holding either mutex, so taking one
while attached left the blocked thread with no safepoint to reach.

Assisted-by: Claude

* gc: size the interpreter owner tag to the header padding

`gc_owner` was a u32. With 4-byte pointers its alignment pushed it out of the
padding that follows the gc bits and generation, growing every object by a
word and tripping the `SIZEOF_PYOBJECT_HEAD` assertion on 32-bit targets.

Introduce `GcOwner = u16`, which the `repr(C)` layout places at offset 10 on
32-bit and 18 on 64-bit, leaving the header at 6 words on both. Tags now run
out after 65535 interpreters; `alloc_owner` already falls back to
`GC_NO_OWNER`, so an interpreter past that collects as it did before tagging.

Also widen three test deadlines that measure liveness, not speed.

Assisted-by: Claude

* vm: reuse cleared frame blocks and shorten interpreter hot paths

- `datastack` remembers the most recently popped frame block. `push_frame`
  reports an exact LIFO reuse, and `setup_datastack_frame` then skips
  zero-filling localsplus.
- Small-int loads push the context's cached int as a borrowed stack reference
  instead of taking a new one.
- Calls to jitted functions go straight to `execute_call_vectorcall`.
- Binary-op specialization reads ints through the new `PyInt::try_to_i64_fast`
  instead of the generic primitive conversion, and `try_to_bool` moves its
  non-bool path into a `#[cold]` helper.
- Dict caches read an entry through a keys-version stamp
  (`get_index_if_keys_version`) rather than an entry-index hint.
- Frame publishing caches a pointer to `ThreadSlot::top_iframe` in a
  thread-local `Cell` instead of borrowing `CURRENT_THREAD_SLOT`.

* gc, vm: address review notes on owner tags and interpreter docs

Look up retired owner tags with a sorted binary search instead of a linear
scan, which every scanned object paid for once per dropped interpreter.

Drop the claim that clearing a tag frees it for reuse; `alloc_owner` only
ever hands out new tags. Also correct the tag space it mentions, which is
16-bit since the tag was sized to the header padding.

Document `is_main` as "top-level interpreter" rather than "the process
main": every top-level interpreter sets it, and only the first registered
one becomes the main `main_interpreter_id` reports.

Assert membership rather than an exact owned-interpreter count delta; the
owned table is process-global and other tests store into it in parallel.

Assisted-by: Claude

* vm: gate interpreter registration on stop-the-world admission

A collection snapshots the registry, stops the interpreters it found, and
then reads tracked objects with their threads parked. An interpreter that
registered after the snapshot was taken was absent from it, so nothing
stopped it and its bootstrap ran Python — allocating and mutating the
shared generation lists — underneath that scan.

Registration and the stop now share a process-global gate: the collection
holds it from before the snapshot until the restart, and registration takes
it around the registry insert. The insert runs detached, since a thread
that waited for the gate, or re-attached while holding it, would leave the
stop it waits for no safepoint to complete at.

Assisted-by: Claude

* gc: keep the tracking counters off the barrier path

Every tracked allocation and every free went through sequentially consistent
counter updates, and each free through a `fetch_update` CAS loop. The counters
drive only the gen0 threshold and `gc.get_count()`, and the generation locks —
not the counters — order the list changes they describe, so they are relaxed
now and the decrement is a load plus a conditional `fetch_sub`.

`is_enabled` and `threshold`, both read once per allocation, are relaxed for
the same reason: an allocation racing `gc.disable()` may use either value.

Drop `alloc_count`, which nothing has ever read.

Assisted-by: Claude

* dict: settle lookups and iteration steps under one read guard

A lookup probed under a read guard, dropped it with the matched entry in
hand, and then took the lock again to re-find the entry. `lookup_extract`
reads the entry while the probe still holds the guard when key identity
settles the match, which is the case that cannot run Python.

Dict and set iterators took one lock to compare the size and another to read
the entry, then cloned both the key and the value even though a keys or
values view keeps only one of them. `next_entry_checked` does the size check
and the read under one guard, and clones through a per-view projection.

A store that missed its inline-cache hint re-probed the dict afterwards only
to recover the entry index the store had just computed; `unchecked_push`
reports that index instead.

Assisted-by: Claude

* vm: build a call's argument vector once

The specialized CALL handlers collected the positional arguments into one
vector and then copied them into a second one to put `self` in front, so
every specialized builtin, method-descriptor, class and non-Python call paid
two allocations, a copy and two frees. The arguments are already laid out on
the value stack in vectorcall order, so `take_call_args` fills one vector by
index — the shape `execute_call_vectorcall` already used.

`vectorcall_native_function` and the keyword path of `vectorcall_function`
then cloned that vector again to build `FuncArgs`; both now move it in
through `from_vectorcall_owned`, as the other vectorcall slots do.

Assisted-by: Claude

* vm: reach an instance dict without cloning it

The specialized attribute instructions cloned the instance dict — a rwlock
round-trip plus a refcount round-trip — for two things that only look at it:
`LoadAttrMethodLazyDict` asking whether the dict exists, and the keys-version
stamp check that is the whole of `shadowing_instance_attr`'s fast path. Both
now read it borrowed, through `has_instance_dict` / `with_instance_dict`.

`generic_getattr_opt` probed the dict with the name's `&Wtf8`, which hashes
the name on every lookup and can never match a key by pointer. Passing the
`Py<PyStr>` uses the string's cached hash and the interned-key identity
check, and drops an allocation when a stored key is not an exact `str`.

Assisted-by: Claude

* vm: shorten the per-instruction safepoint and the call preamble

The dispatch loop asks once per instruction whether stop-the-world wants this
thread, and that read went through `CURRENT_THREAD_SLOT` — a `RefCell` borrow,
so two stores to thread-local memory around an `Option` test. Cache the
`stop_requested` pointer in a plain `Cell` at the same three places the frame
pointers are cached, and the safepoint becomes one relaxed load.

`lasti` is advanced from the index the loop just read, rather than reloaded
to increment it, and `Resume` reads `quickened` before swapping it, so a call
to an already-quickened code object costs a load instead of an atomic
read-modify-write.

An exact-args vectorcall to a Python function built a heap `FrameObject`
where the equivalent `invoke` path uses a data stack frame; it now does the
same when tracing is off.

Assisted-by: Claude

* vm: give KwArgs a zero-sized hasher

`KwArgs::default()` is built for every call, keyword-less ones included, and
it seeded a `RandomState` each time — a thread-local read and 16 bytes in
every `FuncArgs`. Keyword names come from the program text, so the map now
uses `BuildHasherDefault`, whose `Default` is a zero-init.

Assisted-by: Claude

* vm: promote borrowed stack refs before a yield

`yield <small int>` tripped the "borrowed refs on stack at yield point"
assertion: `LoadSmallInt` pushes a borrowed ref, and a yield saves the stack
with the frame, which is exactly what that assertion forbids. Promote the
stack first, so a suspended frame owns everything it holds.

Assisted-by: Claude

* vm: run the exact-args vectorcall on a heap frame again

`vectorcall_function` took the data stack path for an exact-args call whenever
tracing was off. A data stack frame that materializes — one traceback entry is
enough — copies its localsplus into the `FrameObject`, so every value it holds
gains a reference for as long as the frame runs, and a tail call holds the
callee's function object until the caller's next call or return. Both are
reachable without this hunk, through the `CallPyExactArgs` specialization, but
routing the vectorcall entry point through the data stack made them apply from
a call site's first execution: test_enumerate, test_memoryio, test_sys,
test_tempfile and test_traceback all read a refcount or a finalization that
moved.

Assisted-by: Claude

* vm: release a tail call's function at the callee's return

The trampoline moved the callee's function object into the caller's
`SuspendedFrame` and dropped it only once the caller had run again, so a
function entered through a tail call kept one reference past its return:
`sys.getrefcount` on it read one high for the caller's next stretch of
bytecode, and anything the function reached stayed alive that much longer.

The callee's frame is released before the `ReturnValue` or `Unwind` action is
formed, and a materialized frame object holds its own references, so the owner
is dropped where the suspended caller is popped instead.

Assisted-by: Claude

* vm: stop copying a running frame's locals when it materializes

`materialize_slow` cloned the source frame's fast locals into the new
`FrameObject`, so every value in a data stack frame gained a reference lasting
as long as that frame object: one traceback entry through a frame kept all of
its locals alive, and `sys.getrefcount` read one high for each of them.

Nothing read that copy while the frame ran. `find_live_source_iframe` already
routes reads to the live frame, and `exit_iframe` fills the slots from it at
return, so the copy now starts empty as `materialize_slow_chain`'s does, and
`snapshot_to_heap` goes with its last caller.

Two readers had to be brought to the live frame the same way.
`has_active_hidden_locals` read the frame object's own slots, which let
`locals()` in a class body write an inlined comprehension's hidden variable
into the class namespace; it now shares `live_fastlocals` with the other two.
Another thread's frame has no live frame to reach from here, so the six
cross-thread sites fill the copy under stop-the-world through
`materialize_with_locals`.

Assisted-by: Claude

* vm: key data stack frame reuse on the exact frame size

push_frame and pop_frame compared 16-byte-aligned sizes, so two frames
whose sizes differ by less than the alignment matched as a reuse. The
caller reads that as "every slot was cleared by the previous frame", but
the larger frame's tail slots were never touched by it.

Assisted-by: Claude

* vm: store the frame object payload address in the cross-thread slot

set_current_frame and reinit_frame_slot_after_fork wrote the address of
the Py<FrameObject>, while the cross-thread readers hand that pointer to
Py::from_payload_ptr, which subtracts the object header from it. Both
writers now store the payload address.

Assisted-by: Claude

* vm: copy a foreign thread's frames instead of linking them

sys._current_frames(), _thread._current_exceptions() and the cross-thread
f_back path handed back frame objects still linked to the running frames.
The owning thread wrote its locals into those same buffers at exit_iframe
while the reader was cloning out of them. materialize_detached and
materialize_detached_chain copy the values under stop-the-world and leave
nothing for the owner to write into; materialize_with_locals is gone.

FrameColdData gains attached_tid, the thread still running the frame a
frame object was materialized from, which exit_iframe clears once it has
written the values in. check_locals_access now rejects a read from any
other thread while it is set; the FrameOwner test could not, because
materialize_slow forces FrameObject ownership.

Also removes with_iframe, which had no callers.

Assisted-by: Claude

* vm: resolve the two rustdoc links this branch added

MAIN_INTERPRETER_ID is not in scope in interpreter.rs, and new_thread only
exists in a threading build, so neither link resolved.

Assisted-by: Claude
This commit is contained in:
Jeong, YunWon authored and GitHub committed 2026-08-16 21:21:31 +09:00
1 parent c3bfefaeb0
commit 11de045ef8
40 files changed
+3357 -988

No files matched your search

+16 -12
View File
@@ -9,7 +9,7 @@ pub unsafe extern "C" fn PyObject_GC_Track(op: *mut PyObject) {
with_vm(|_vm| {
let obj = unsafe { &*op };
if !obj.is_gc_tracked() {
unsafe { gc_state::gc_state().track_object(obj.into()) };
unsafe { gc_state::gc_state().track_object(obj.into(), gc_state::current_owner()) };
}
})
}
@@ -36,29 +36,33 @@ pub unsafe extern "C" fn PyObject_GC_IsFinalized(op: *mut PyObject) -> c_int {
#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Collect() -> isize {
let result = gc_state::gc_state().collect(2);
(result.collected + result.uncollectable) as isize
with_vm(|vm| {
let result = vm.state.gc.collect(2);
(result.collected + result.uncollectable) as isize
})
}
#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Enable() -> c_int {
let gc = gc_state::gc_state();
let was_enabled = gc.is_enabled();
gc.enable();
was_enabled.into()
with_vm(|vm| {
let was_enabled: c_int = vm.state.gc.is_enabled().into();
vm.state.gc.enable();
was_enabled
})
}
#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Disable() -> c_int {
let gc = gc_state::gc_state();
let was_enabled = gc.is_enabled();
gc.disable();
was_enabled.into()
with_vm(|vm| {
let was_enabled: c_int = vm.state.gc.is_enabled().into();
vm.state.gc.disable();
was_enabled
})
}
#[unsafe(no_mangle)]
pub extern "C" fn PyGC_IsEnabled() -> c_int {
gc_state::gc_state().is_enabled().into()
with_vm(|vm| -> c_int { vm.state.gc.is_enabled().into() })
}
#[unsafe(no_mangle)]
+2 -2
View File
@@ -109,8 +109,8 @@ mod tests {
current_vm_is_set(),
"This thread did not have a vm attached"
);
vm.state.stop_the_world.stop_the_world(vm);
vm.state.stop_the_world.start_the_world(vm);
vm.state.stop_the_world.stop_the_world(&vm.state);
vm.state.stop_the_world.start_the_world(&vm.state);
});
});
});
+24 -9
View File
@@ -74,9 +74,20 @@ mod _queue {
}
}
fn release(&self) {
/// Take `mutex`, detaching first so that blocking on it cannot stall a
/// stop-the-world request.
///
/// A waiter holds this mutex across its `allow_threads` wait, so it can
/// still hold it when it is stopped. An attached thread blocking on it
/// would then never reach a safepoint, the stop would never complete,
/// and the holder would never be resumed to release it.
fn lock_count(&self, vm: &VirtualMachine) -> parking_lot::MutexGuard<'_, usize> {
vm.allow_threads(|| self.mutex.lock())
}
fn release(&self, vm: &VirtualMachine) {
{
let mut count = self.mutex.lock();
let mut count = self.lock_count(vm);
*count += 1;
} // lock dropped. now we can notify a waiting thread
@@ -95,7 +106,7 @@ mod _queue {
// Guard must be dropped before check_signals() below, since a
// signal handler may call back into this same queue.
{
let mut count = self.mutex.lock();
let mut count = self.lock_count(vm);
if *count > 0 {
*count -= 1;
@@ -151,11 +162,15 @@ mod _queue {
}
impl PySimpleQueue {
fn push(&self, item: PyObjectRef) {
#[cfg_attr(
not(feature = "threading"),
expect(unused_variables, reason = "only the semaphore needs the vm")
)]
fn push(&self, item: PyObjectRef, vm: &VirtualMachine) {
self.buf.lock().push_back(item);
#[cfg(feature = "threading")]
self.sem.release();
self.sem.release(vm);
}
/// Returns a strong reference from the head of the buffer.
@@ -221,14 +236,14 @@ mod _queue {
}
#[pymethod]
fn put(&self, args: PutArgs) {
fn put(&self, args: PutArgs, vm: &VirtualMachine) {
let PutArgs { item, .. } = args;
self.push(item);
self.push(item, vm);
}
#[pymethod]
fn put_nowait(&self, item: PyObjectRef) {
self.push(item);
fn put_nowait(&self, item: PyObjectRef, vm: &VirtualMachine) {
self.push(item, vm);
}
#[pymethod]
+2 -2
View File
@@ -260,8 +260,8 @@ mod decl {
use core::sync::atomic::Ordering;
let current_tid = rustpython_vm::stdlib::_thread::get_ident();
{
vm.state.stop_the_world.stop_the_world(vm);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(vm); }
vm.state.stop_the_world.stop_the_world(&vm.state);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(&vm.state); }
let registry = vm.state.thread_frames.lock();
#[expect(
clippy::iter_over_hash_type,
+7
View File
@@ -34,6 +34,7 @@ impl<'a> TryFromBorrowedObject<'a> for bool {
impl PyObjectRef {
/// Convert Python bool into Rust bool.
#[inline(always)]
pub fn try_to_bool(self, vm: &VirtualMachine) -> PyResult<bool> {
if self.is(&vm.ctx.true_value) {
return Ok(true);
@@ -41,6 +42,12 @@ impl PyObjectRef {
return Ok(false);
}
self.try_to_bool_slow(vm)
}
#[cold]
#[inline(never)]
fn try_to_bool_slow(self, vm: &VirtualMachine) -> PyResult<bool> {
let slots = &self.class().slots;
// 1. Try nb_bool slot first
+2 -2
View File
@@ -247,9 +247,9 @@ fn vectorcall_native_function(
let mut all_args = Vec::with_capacity(args.len() + 1);
all_args.push(self_obj);
all_args.extend(args);
FuncArgs::from_vectorcall(&all_args, nargs + 1, kwnames)
FuncArgs::from_vectorcall_owned(all_args, nargs + 1, kwnames)
} else {
FuncArgs::from_vectorcall(&args, nargs, kwnames)
FuncArgs::from_vectorcall_owned(args, nargs, kwnames)
};
(zelf.value.func)(vm, func_args)
+48 -43
View File
@@ -114,11 +114,6 @@ impl PyDict {
&self.entries
}
/// Monotonically increasing version for mutation tracking.
pub(crate) fn version(&self) -> u64 {
self.entries.version()
}
/// Returns all keys as a Vec, atomically under a single read lock.
/// Thread-safe: prevents "dictionary changed size during iteration" errors.
pub fn keys_vec(&self) -> Vec<PyObjectRef> {
@@ -821,18 +816,15 @@ impl Py<PyDict> {
}
}
/// Fast lookup using a cached entry index hint.
pub(crate) fn get_item_opt_hint<K: DictKey + ?Sized>(
/// Read a cached exact-dict entry after validating its key-layout stamp.
#[inline]
pub(crate) fn get_item_by_index_and_keys_version(
&self,
key: &K,
hint: u16,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
if self.exact_dict(vm) {
self.entries.get_hint(vm, key, usize::from(hint))
} else {
self.get_item_opt(key, vm)
}
version: u16,
index: u16,
) -> Option<PyObjectRef> {
self.entries
.get_index_if_keys_version(u32::from(version), usize::from(index))
}
/// Lookup trying a cached entry index hint first.
@@ -1098,6 +1090,7 @@ macro_rules! dict_view {
$class_name: literal,
$iter_class_name: literal,
$reverse_iter_class_name: literal,
$project_fn: expr,
$result_fn: expr
) => {
#[pyclass(module = false, name = $class_name)]
@@ -1120,7 +1113,7 @@ macro_rules! dict_view {
}
fn item(vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef) -> PyObjectRef {
$result_fn(vm, key, value)
$result_fn(vm, $project_fn(&key, &value))
}
fn __reversed__(&self) -> Self::ReverseIter {
@@ -1206,7 +1199,7 @@ macro_rules! dict_view {
while let Some((next_position, key, value)) =
dict.entries.next_entry(position)
{
entries.push(($result_fn)(vm, key, value));
entries.push(($result_fn)(vm, ($project_fn)(&key, &value)));
position = next_position;
}
entries
@@ -1223,18 +1216,22 @@ macro_rules! dict_view {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let mut internal = zelf.internal.lock();
let next = if let IterStatus::Active(dict) = &internal.status {
if dict.entries.has_changed_size(&zelf.size) {
internal.status = IterStatus::Exhausted;
return Err(
vm.new_runtime_error("dictionary changed size during iteration")
);
}
match dict.entries.next_entry(internal.position) {
Some((position, key, value)) => {
internal.position = position;
PyIterReturn::Return(($result_fn)(vm, key, value))
match dict.entries.next_entry_checked(
internal.position,
&zelf.size,
$project_fn,
) {
Err(dict_inner::DictChanged) => {
internal.status = IterStatus::Exhausted;
return Err(
vm.new_runtime_error("dictionary changed size during iteration")
);
}
None => {
Ok(Some((position, item))) => {
internal.position = position;
PyIterReturn::Return(($result_fn)(vm, item))
}
Ok(None) => {
internal.status = IterStatus::Exhausted;
PyIterReturn::StopIteration(None)
}
@@ -1282,7 +1279,7 @@ macro_rules! dict_view {
while let Some((found_index, key, value)) =
dict.entries.prev_entry(position)
{
entries.push(($result_fn)(vm, key, value));
entries.push(($result_fn)(vm, ($project_fn)(&key, &value)));
if found_index == 0 {
break;
}
@@ -1309,22 +1306,26 @@ macro_rules! dict_view {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let mut internal = zelf.internal.lock();
let next = if let IterStatus::Active(dict) = &internal.status {
if dict.entries.has_changed_size(&zelf.size) {
internal.status = IterStatus::Exhausted;
return Err(
vm.new_runtime_error("dictionary changed size during iteration")
);
}
match dict.entries.prev_entry(internal.position) {
Some((found_index, key, value)) => {
match dict.entries.prev_entry_checked(
internal.position,
&zelf.size,
$project_fn,
) {
Err(dict_inner::DictChanged) => {
internal.status = IterStatus::Exhausted;
return Err(
vm.new_runtime_error("dictionary changed size during iteration")
);
}
Ok(Some((found_index, item))) => {
if found_index == 0 {
internal.status = IterStatus::Exhausted;
} else {
internal.position = found_index - 1;
}
PyIterReturn::Return(($result_fn)(vm, key, value))
PyIterReturn::Return(($result_fn)(vm, item))
}
None => {
Ok(None) => {
internal.status = IterStatus::Exhausted;
PyIterReturn::StopIteration(None)
}
@@ -1348,7 +1349,8 @@ dict_view! {
"dict_keys",
"dict_keyiterator",
"dict_reversekeyiterator",
|_vm: &VirtualMachine, key: PyObjectRef, _value: PyObjectRef| key
|key: &PyObjectRef, _value: &PyObjectRef| key.clone(),
|_vm: &VirtualMachine, key: PyObjectRef| key
}
dict_view! {
@@ -1361,7 +1363,8 @@ dict_view! {
"dict_values",
"dict_valueiterator",
"dict_reversevalueiterator",
|_vm: &VirtualMachine, _key: PyObjectRef, value: PyObjectRef| value
|_key: &PyObjectRef, value: &PyObjectRef| value.clone(),
|_vm: &VirtualMachine, value: PyObjectRef| value
}
dict_view! {
@@ -1374,7 +1377,9 @@ dict_view! {
"dict_items",
"dict_itemiterator",
"dict_reverseitemiterator",
|vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef|
|key: &PyObjectRef, value: &PyObjectRef| (key.clone(), value.clone()),
// Builds a tuple, so it runs after the dict's read guard is released.
|vm: &VirtualMachine, (key, value): (PyObjectRef, PyObjectRef)|
vm.new_tuple((key, value)).into()
}
+7 -20
View File
@@ -512,7 +512,7 @@ impl FrameObject {
let live = self.find_live_source_iframe();
if !live.is_null() {
// Read live prev_line. Use read_volatile to bypass LLVM noalias
// on the &mut InterpreterFrame borrow in with_iframe.
// on the &mut InterpreterFrame borrow held by the running frame.
let prev = unsafe {
let field_ptr = core::ptr::addr_of!((*live).prev_line);
core::ptr::read_volatile(field_ptr as *const u32)
@@ -897,32 +897,19 @@ impl Py<FrameObject> {
{
// Enter STW before dereferencing `prev` — the owning thread may
// return and free the stack-allocated iframe at any time.
vm.state.stop_the_world.stop_the_world(vm);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(vm); }
vm.state.stop_the_world.stop_the_world(&vm.state);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(&vm.state); }
let prev_ref = unsafe { &*prev };
// Fast path: already materialized.
if let Some(fo) = prev_ref.frame_obj() {
fo.mark_escaped();
return Some(fo.to_owned());
}
// Slow path: materialize the entire chain and link retained_back.
let mut cur = prev;
let mut child_fo: Option<crate::PyRef<crate::frame::FrameObject>> = None;
while !cur.is_null() {
let iframe = unsafe { &*cur };
let fo = iframe.materialize(vm).to_owned();
if let Some(child) = child_fo.take() {
let mut guard = child.iframe().cold().retained_back.lock();
if guard.is_none() {
*guard = Some(fo.clone());
}
}
child_fo = Some(fo);
cur = iframe.previous();
}
let fo = prev_ref.materialize(vm);
// Slow path: copy the whole chain, linked through retained_back.
// SAFETY: the world is stopped, so the owning thread is parked.
let fo = unsafe { prev_ref.materialize_detached_chain(vm) };
fo.mark_escaped();
return Some(fo.to_owned());
return Some(fo);
}
#[allow(unreachable_code)]
+33 -6
View File
@@ -550,6 +550,20 @@ impl Py<PyFunction> {
self.code.flags.contains(bytecode::CodeFlags::OPTIMIZED)
}
/// Whether this function currently has native JIT code. Adaptive Python
/// call specializations must yield to that entry point.
#[inline]
pub(crate) fn is_jitted(&self) -> bool {
#[cfg(feature = "jit")]
{
self.jitted_code.lock().is_some()
}
#[cfg(not(feature = "jit"))]
{
false
}
}
pub fn invoke_with_locals(
&self,
func_args: FuncArgs,
@@ -643,8 +657,8 @@ impl Py<PyFunction> {
.and_then(|()| vm.run_frame_fast(iframe));
// Release data stack memory — must happen on both success and error.
unsafe {
if let Some(base) = iframe.release_datastack_frame() {
vm.datastack_pop(base);
if let Some((base, size)) = iframe.release_datastack_frame() {
vm.datastack_pop_frame(base, size);
}
}
result
@@ -669,7 +683,10 @@ impl Py<PyFunction> {
);
// SAFETY: the frame is alive (held by `frame`) and untracked.
unsafe {
crate::gc_state::gc_state().track_object(core::ptr::NonNull::from(frame.as_object()));
crate::gc_state::gc_state().track_object(
core::ptr::NonNull::from(frame.as_object()),
crate::gc_state::current_owner(),
);
}
frame.set_generator(&obj);
obj
@@ -820,8 +837,8 @@ impl Py<PyFunction> {
let result = vm.run_frame_fast(iframe);
unsafe {
if let Some(base) = iframe.release_datastack_frame() {
vm.datastack_pop(base);
if let Some((base, size)) = iframe.release_datastack_frame() {
vm.datastack_pop_frame(base, size);
}
}
result
@@ -1616,6 +1633,16 @@ pub(crate) fn vectorcall_function(
let code: &Py<PyCode> = &zelf.code;
let has_kwargs = kwnames.is_some_and(|kw| !kw.is_empty());
if zelf.is_jitted() {
let func_args = if has_kwargs {
FuncArgs::from_vectorcall_owned(args, nargs, kwnames)
} else {
args.truncate(nargs);
FuncArgs::from(args)
};
return zelf.invoke(func_args, vm);
}
let is_simple = !has_kwargs
&& code.flags.contains(bytecode::CodeFlags::OPTIMIZED)
&& !code.flags.contains(bytecode::CodeFlags::VARARGS)
@@ -1640,7 +1667,7 @@ pub(crate) fn vectorcall_function(
// SLOW PATH: construct FuncArgs from owned Vec and delegate to invoke()
let func_args = if has_kwargs {
FuncArgs::from_vectorcall(&args, nargs, kwnames)
FuncArgs::from_vectorcall_owned(args, nargs, kwnames)
} else {
args.truncate(nargs);
FuncArgs::from(args)
+16
View File
@@ -305,6 +305,22 @@ impl PyInt {
&self.value
}
/// Extract the inline magnitude without the generic primitive-conversion path.
#[inline(always)]
pub(crate) fn try_to_i64_fast(&self) -> Option<i64> {
let bits = self.value.bits();
if bits > i64::BITS as u64 {
return None;
}
let magnitude = self.value.iter_u64_digits().next().unwrap_or(0);
let signed_magnitude = i64::try_from(magnitude).ok();
match self.value.sign() {
Sign::Minus if magnitude == 1u64 << 63 => Some(i64::MIN),
Sign::Minus => signed_magnitude.map(|value| -value),
Sign::NoSign | Sign::Plus => signed_magnitude,
}
}
/// Fast decimal string conversion, using i64 path when possible.
#[inline]
#[must_use]
+7 -7
View File
@@ -1533,16 +1533,16 @@ impl IterNext for PySetIterator {
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let mut internal = zelf.internal.lock();
let next = if let IterStatus::Active(dict) = &internal.status {
if dict.has_changed_size(&zelf.size) {
internal.status = IterStatus::Exhausted;
return Err(vm.new_runtime_error("set changed size during iteration"));
}
match dict.next_entry(internal.position) {
Some((position, key, _)) => {
match dict.next_entry_checked(internal.position, &zelf.size, |key, ()| key.clone()) {
Err(crate::dict_inner::DictChanged) => {
internal.status = IterStatus::Exhausted;
return Err(vm.new_runtime_error("set changed size during iteration"));
}
Ok(Some((position, key))) => {
internal.position = position;
PyIterReturn::Return(key)
}
None => {
Ok(None) => {
internal.status = IterStatus::Exhausted;
PyIterReturn::StopIteration(None)
}
+35 -2
View File
@@ -294,6 +294,16 @@ pub struct HeapTypeExt {
pub slots: Option<PyRef<PyTuple<PyStrRef>>>,
pub type_data: PyRwLock<Option<TypeDataSlot>>,
pub specialization_cache: TypeSpecializationCache,
/// The interpreter this type was created in, or `None` for the types the
/// shared context builds before any interpreter exists.
pub interpreter_id: Option<i64>,
}
impl HeapTypeExt {
/// The interpreter a type created right now belongs to.
fn creating_interpreter_id() -> Option<i64> {
crate::vm::thread::try_with_current_vm(|vm| vm.state.interpreter_id)
}
}
pub struct TypeSpecializationCache {
@@ -553,6 +563,22 @@ impl PyType {
self.modified_inner();
}
/// Whether the interpreter with `interpreter_id` can see this type.
///
/// Interpreters share the context, so a subclass of a shared type is
/// recorded on an object every interpreter reaches. Only the interpreter
/// that created it can name it, so only that one lists it.
pub fn is_visible_to_interpreter(&self, interpreter_id: i64) -> bool {
match self
.heaptype_ext
.as_ref()
.and_then(|ext| ext.interpreter_id)
{
Some(owner) => owner == interpreter_id,
None => true,
}
}
pub fn new_simple_heap(
name: &str,
base: &Py<Self>,
@@ -589,6 +615,7 @@ impl PyType {
slots: None,
type_data: PyRwLock::new(None),
specialization_cache: TypeSpecializationCache::new(),
interpreter_id: HeapTypeExt::creating_interpreter_id(),
};
let base = bases[0].clone();
@@ -1948,13 +1975,18 @@ impl PyType {
}
#[pymethod]
fn __subclasses__(&self) -> PyList {
fn __subclasses__(&self, vm: &VirtualMachine) -> PyList {
let mut subclasses = self.subclasses.write();
subclasses.retain(|x| x.upgrade().is_some());
let interpreter_id = vm.state.interpreter_id;
PyList::from(
subclasses
.iter()
.map(|x| x.upgrade().unwrap())
.filter_map(|x| x.upgrade())
.filter(|obj| {
obj.downcast_ref::<Self>()
.is_none_or(|typ| typ.is_visible_to_interpreter(interpreter_id))
})
.collect::<Vec<_>>(),
)
}
@@ -2368,6 +2400,7 @@ impl Constructor for PyType {
slots: heaptype_slots.clone(),
type_data: PyRwLock::new(None),
specialization_cache: TypeSpecializationCache::new(),
interpreter_id: HeapTypeExt::creating_interpreter_id(),
};
(slots, heaptype_ext)
};
+47 -1
View File
@@ -61,6 +61,9 @@ pub struct DataStack {
top: *mut u8,
/// End of usable space in the current chunk.
limit: *mut u8,
/// Most recently popped full-frame allocation whose localsplus slots were
/// cleared before the pop. An exact LIFO reuse can skip zero-filling them.
reusable_frame: Option<(*mut u8, usize)>,
}
impl DataStack {
@@ -73,7 +76,12 @@ impl DataStack {
// Skip one ALIGN-sized slot in the root chunk so that `pop()` never
// frees it (`push_chunk` convention).
let top = unsafe { top.add(ALIGN) };
Self { chunk, top, limit }
Self {
chunk,
top,
limit,
reusable_frame: None,
}
}
/// Check if the current chunk has at least `size` bytes available.
@@ -91,6 +99,26 @@ impl DataStack {
/// (LIFO order).
#[inline(always)]
pub fn push(&mut self, size: usize) -> *mut u8 {
self.reusable_frame = None;
self.push_inner(size)
}
/// Allocate a full interpreter frame and report whether it exactly reuses
/// a just-cleared frame block.
#[inline(always)]
pub fn push_frame(&mut self, size: usize) -> (*mut u8, bool) {
let reusable_frame = self.reusable_frame.take();
let ptr = self.push_inner(size);
// Exact sizes, not aligned ones: the caller reads "reused" as "every
// slot of this frame was cleared by the last one", and two frames whose
// sizes differ by less than ALIGN share an aligned size while the
// larger one's tail slots were never touched, let alone cleared.
let reused = reusable_frame.is_some_and(|(base, old_size)| base == ptr && old_size == size);
(ptr, reused)
}
#[inline(always)]
fn push_inner(&mut self, size: usize) -> *mut u8 {
let aligned_size = (size + ALIGN - 1) & !(ALIGN - 1);
unsafe {
if self.top.add(aligned_size) <= self.limit {
@@ -138,6 +166,24 @@ impl DataStack {
/// and all allocations made after it must already have been popped.
#[inline(always)]
pub unsafe fn pop(&mut self, base: *mut u8) {
self.reusable_frame = None;
unsafe { self.pop_inner(base) };
}
/// Pop a full frame whose localsplus slots have already been cleared.
///
/// # Safety
/// `base` and `size` must describe the most recent allocation returned by
/// `push_frame`, every later allocation must already be popped, and all
/// localsplus slots in the frame must have been cleared.
#[inline(always)]
pub unsafe fn pop_frame(&mut self, base: *mut u8, size: usize) {
unsafe { self.pop_inner(base) };
self.reusable_frame = Some((base, size));
}
#[inline(always)]
unsafe fn pop_inner(&mut self, base: *mut u8) {
debug_assert!(!base.is_null());
if self.is_in_current_chunk(base) {
// Common case: base is within the current chunk.
+142 -36
View File
@@ -20,7 +20,7 @@ use alloc::fmt;
use core::mem::size_of;
use core::ops::ControlFlow;
use core::sync::atomic::{
AtomicU32, AtomicU64,
AtomicU32,
Ordering::{AcqRel, Acquire, Relaxed, Release},
};
use num_traits::ToPrimitive;
@@ -39,7 +39,6 @@ type EntryIndex = usize;
pub(crate) struct Dict<T = PyObjectRef> {
inner: PyRwLock<DictInner<T>>,
version: AtomicU64,
/// Keys-version stamp, assigned lazily by `assign_keys_version` and
/// reset to 0 whenever the key set changes. Value-only updates keep it.
///
@@ -202,7 +201,6 @@ impl<T: Clone> Clone for Dict<T> {
fn clone(&self) -> Self {
Self {
inner: PyRwLock::new(self.inner.read().clone()),
version: AtomicU64::new(0),
keys_version: AtomicU32::new(0),
}
}
@@ -217,7 +215,6 @@ impl<T> Default for Dict<T> {
indices: vec![IndexEntry::FREE; 8],
entries: Vec::new(),
}),
version: AtomicU64::new(0),
keys_version: AtomicU32::new(0),
}
}
@@ -240,6 +237,10 @@ pub struct DictSize {
filled: usize,
}
/// The dict was resized under an iterator holding an older [`DictSize`].
#[derive(Debug)]
pub(crate) struct DictChanged;
struct GenIndexes {
idx: HashIndex,
perturb: HashValue,
@@ -309,7 +310,7 @@ impl<T> DictInner<T> {
key: PyObjectRef,
value: T,
index_entry: IndexEntry,
) {
) -> usize {
let entry = DictEntry {
hash: hash_value,
key,
@@ -330,6 +331,9 @@ impl<T> DictInner<T> {
self.resize(new_size)
}
}
// A resize keeps entry positions and rewrites only the index-index, so
// this stays the entry's index afterwards.
entry_index
}
const fn size(&self) -> DictSize {
@@ -362,16 +366,6 @@ impl<T> DictInner<T> {
type PopInnerResult<T> = ControlFlow<Option<DictEntry<T>>>;
impl<T: Clone> Dict<T> {
/// Monotonically increasing version counter for mutation tracking.
pub(crate) fn version(&self) -> u64 {
self.version.load(Acquire)
}
/// Bump the version counter after any mutation.
fn bump_version(&self) {
self.version.fetch_add(1, Release);
}
/// Current keys-version stamp, or 0 if none has been assigned since the
/// last key-set change. Equal nonzero stamps guarantee an unchanged key
/// set (values may differ).
@@ -481,7 +475,26 @@ impl<T: Clone> Dict<T> {
where
K: DictKey + ?Sized,
{
let _removed = loop {
self.insert_known_hash_indexed(vm, key, hash, value)?;
Ok(())
}
/// [`Self::insert_known_hash`], also reporting the entry index it stored to.
///
/// The index doubles as a `hint` for [`Self::get_hint`] /
/// [`Self::insert_with_hint`], so a caller that wants one gets it from the
/// store itself instead of probing the dict a second time.
fn insert_known_hash_indexed<K>(
&self,
vm: &VirtualMachine,
key: &K,
hash: HashValue,
value: T,
) -> PyResult<usize>
where
K: DictKey + ?Sized,
{
let (stored_index, _removed) = loop {
let (entry_index, index_index) = self.lookup(vm, key, hash, None)?;
let mut inner = self.write();
if let Some(index) = entry_index.index() {
@@ -500,9 +513,8 @@ impl<T: Clone> Dict<T> {
)]
if entry.index == index_index {
let removed = core::mem::replace(&mut entry.value, value);
self.bump_version();
// defer dec RC
break Some(removed);
break (index, Some(removed));
} else {
// stuff shifted around, let's try again
}
@@ -516,12 +528,17 @@ impl<T: Clone> Dict<T> {
continue;
}
self.invalidate_keys_version();
inner.unchecked_push(index_index, hash, key.to_pyobject(vm), value, entry_index);
self.bump_version();
break None;
let stored = inner.unchecked_push(
index_index,
hash,
key.to_pyobject(vm),
value,
entry_index,
);
break (stored, None);
}
};
Ok(())
Ok(stored_index)
}
pub(crate) fn contains<K: DictKey + ?Sized>(
@@ -616,7 +633,6 @@ impl<T: Clone> Dict<T> {
match inner.entries.get_mut(hint) {
Some(Some(entry)) if key.key_is(&entry.key) => {
let removed = core::mem::replace(&mut entry.value, value);
self.bump_version();
drop(inner);
// defer dec RC until after the lock is released
drop(removed);
@@ -625,8 +641,9 @@ impl<T: Clone> Dict<T> {
_ => value,
}
};
self.insert(vm, key, value)?;
self.hint_for_key(vm, key)
let hash = key.key_hash(vm)?;
let stored = self.insert_known_hash_indexed(vm, key, hash, value)?;
Ok(u16::try_from(stored).ok())
}
/// Fast path lookup using a cached entry index (`hint`).
@@ -656,6 +673,22 @@ impl<T: Clone> Dict<T> {
}
}
/// Read an entry directly when a cached keys-version still describes the
/// dictionary layout. The version is rechecked while holding the read lock
/// so the entry index and value are observed from the same key-set state.
#[inline]
pub(crate) fn get_index_if_keys_version(&self, version: u32, index: usize) -> Option<T> {
let inner = self.read();
if self.keys_version.load(Acquire) != version {
return None;
}
inner
.entries
.get(index)
.and_then(Option::as_ref)
.map(|entry| entry.value.clone())
}
fn _get_inner<K: DictKey + ?Sized>(
&self,
vm: &VirtualMachine,
@@ -663,7 +696,12 @@ impl<T: Clone> Dict<T> {
hash: HashValue,
) -> PyResult<Option<T>> {
let ret = loop {
let (entry, index_index) = self.lookup(vm, key, hash, None)?;
let (entry, index_index) =
match self.lookup_extract(vm, key, hash, None, |entry| entry.value.clone())? {
// Read under the probe's own guard: nothing to re-check.
(_, Some(value)) => break Some(value),
(lookup, None) => lookup,
};
if let Some(index) = entry.index() {
let inner = self.read();
if let Some(entry) = inner.get_entry_checked(index, index_index) {
@@ -701,7 +739,6 @@ impl<T: Clone> Dict<T> {
inner.indices.resize(8, IndexEntry::FREE);
inner.used = 0;
inner.filled = 0;
self.bump_version();
// defer dec rc
core::mem::take(&mut inner.entries)
};
@@ -830,7 +867,6 @@ impl<T: Clone> Dict<T> {
}
self.invalidate_keys_version();
inner.unchecked_push(index_index, hash, key.to_owned(), value, entry);
self.bump_version();
break None;
};
Ok(())
@@ -867,7 +903,6 @@ impl<T: Clone> Dict<T> {
value.clone(),
index_entry,
);
self.bump_version();
return Ok(value);
}
}
@@ -905,7 +940,6 @@ impl<T: Clone> Dict<T> {
let ret = (key_obj.clone(), value.clone());
self.invalidate_keys_version();
inner.unchecked_push(index_index, hash, key_obj, value, index_entry);
self.bump_version();
return Ok(ret);
}
}
@@ -918,6 +952,58 @@ impl<T: Clone> Dict<T> {
self.read().size()
}
/// Step to the first live entry at or after `position`, verifying the size
/// against `old` under the same read guard.
///
/// `project` runs under that guard, so it must not run Python or take
/// another dict lock; it is there so an iterator clones only the field it
/// keeps rather than both the key and the value.
pub(crate) fn next_entry_checked<R>(
&self,
mut position: EntryIndex,
old: &DictSize,
project: impl FnOnce(&PyObjectRef, &T) -> R,
) -> Result<Option<(usize, R)>, DictChanged> {
let inner = self.read();
if inner.size() != *old {
return Err(DictChanged);
}
loop {
let Some(entry) = inner.entries.get(position) else {
return Ok(None);
};
position += 1;
if let Some(entry) = entry {
return Ok(Some((position, project(&entry.key, &entry.value))));
}
}
}
/// [`Self::next_entry_checked`] in reverse.
pub(crate) fn prev_entry_checked<R>(
&self,
mut position: EntryIndex,
old: &DictSize,
project: impl FnOnce(&PyObjectRef, &T) -> R,
) -> Result<Option<(usize, R)>, DictChanged> {
let inner = self.read();
if inner.size() != *old {
return Err(DictChanged);
}
loop {
let Some(entry) = inner.entries.get(position) else {
return Ok(None);
};
if let Some(entry) = entry {
return Ok(Some((position, project(&entry.key, &entry.value))));
}
if position == 0 {
return Ok(None);
}
position -= 1;
}
}
pub(crate) fn next_entry(&self, mut position: EntryIndex) -> Option<(usize, PyObjectRef, T)> {
let inner = self.read();
loop {
@@ -1004,8 +1090,30 @@ impl<T: Clone> Dict<T> {
vm: &VirtualMachine,
key: &K,
hash_value: HashValue,
mut lock: Option<PyRwLockReadGuard<'_, DictInner<T>>>,
lock: Option<PyRwLockReadGuard<'_, DictInner<T>>>,
) -> PyResult<LookupResult> {
let (ret, _) = self.lookup_extract(vm, key, hash_value, lock, |_| ())?;
Ok(ret)
}
/// [`Self::lookup`], additionally reading the matched entry when the probe
/// settles it by key identity.
///
/// That is the common case, and it is decided while the read guard is still
/// held — so a caller that only wants the entry's value gets it here instead
/// of taking the lock a second time to re-find what the probe already had.
/// `extract` therefore runs under the guard and must not run Python. It is
/// not called when the key had to be compared with `key_eq`, which does run
/// Python and so releases the guard first.
#[cfg_attr(feature = "flame-it", flame("Dict"))]
fn lookup_extract<K: DictKey + ?Sized, R>(
&self,
vm: &VirtualMachine,
key: &K,
hash_value: HashValue,
mut lock: Option<PyRwLockReadGuard<'_, DictInner<T>>>,
extract: impl Fn(&DictEntry<T>) -> R,
) -> PyResult<(LookupResult, Option<R>)> {
let mut idxs = None;
let mut free_slot = None;
let ret = 'outer: loop {
@@ -1035,7 +1143,7 @@ impl<T: Clone> Dict<T> {
Some(free) => (IndexEntry::DUMMY, free),
None => (IndexEntry::FREE, index_index),
};
return Ok(idxs);
return Ok((idxs, None));
}
idx => {
let entry = unsafe {
@@ -1051,7 +1159,7 @@ impl<T: Clone> Dict<T> {
reason = "Keeping the empty `else` block here for documentation"
)]
if key.key_is(&entry.key) {
break 'outer ret;
return Ok((ret, Some(extract(entry))));
} else if entry.hash == hash_value {
break (entry.key.clone(), ret);
} else {
@@ -1076,7 +1184,7 @@ impl<T: Clone> Dict<T> {
// warn!("Perturb value: {}", i);
};
Ok(ret)
Ok((ret, None))
}
// returns Err(()) if changed since lookup
@@ -1117,7 +1225,6 @@ impl<T: Clone> Dict<T> {
} = IndexEntry::DUMMY;
inner.used -= 1;
let removed = slot.take();
self.bump_version();
Ok(ControlFlow::Break(removed))
}
@@ -1152,7 +1259,6 @@ impl<T: Clone> Dict<T> {
// entry.index always refers valid index
inner.indices.get_unchecked_mut(entry.index)
} = IndexEntry::DUMMY;
self.bump_version();
Some((entry.key, entry.value))
}
+385 -243
View File
File diff suppressed because it is too large. Load diff
+14 -5
View File
@@ -8,6 +8,7 @@ use crate::{
use core::ops::{Deref, DerefMut, RangeInclusive};
use indexmap::IndexMap;
use itertools::Itertools;
use std::hash::DefaultHasher;
pub trait IntoFuncArgs: Sized {
fn into_args(self, vm: &VirtualMachine) -> FuncArgs;
@@ -414,16 +415,24 @@ impl<T: TryFromObject> FromArgOptional for T {
// issue #8228). `PyStr` is WTF-8 backed, and CPython only requires that a
// keyword key be a `str`, not that it be valid UTF-8.
#[derive(Clone, Debug)]
pub struct KwArgs<T = PyObjectRef>(IndexMap<Wtf8Buf, T>);
pub struct KwArgs<T = PyObjectRef>(KwArgsMap<T>);
/// The map behind [`KwArgs`].
///
/// The hasher is zero-sized rather than the randomly seeded default: a
/// `KwArgs` is built for every call, including the far more common
/// keyword-less one, and seeding reads a thread-local. Keyword names come
/// from the program text, so per-process hash randomization buys nothing.
pub type KwArgsMap<T> = IndexMap<Wtf8Buf, T, core::hash::BuildHasherDefault<DefaultHasher>>;
impl<T> Default for KwArgs<T> {
fn default() -> Self {
Self(IndexMap::new())
Self(KwArgsMap::default())
}
}
impl<T> Deref for KwArgs<T> {
type Target = IndexMap<Wtf8Buf, T>;
type Target = KwArgsMap<T>;
fn deref(&self) -> &Self::Target {
&self.0
@@ -447,7 +456,7 @@ where
impl<T> KwArgs<T> {
#[must_use]
pub const fn new(map: IndexMap<Wtf8Buf, T>) -> Self {
pub const fn new(map: KwArgsMap<T>) -> Self {
Self(map)
}
@@ -508,7 +517,7 @@ where
T: TryFromObject,
{
fn from_args(vm: &VirtualMachine, args: &mut FuncArgs) -> Result<Self, ArgumentError> {
let mut kwargs = IndexMap::new();
let mut kwargs = KwArgsMap::default();
for (name, value) in args.remaining_keywords() {
kwargs.insert(name, value.try_into_value(vm)?);
}
+2 -2
View File
@@ -11,8 +11,8 @@ mod protocol;
mod time;
pub use argument::{
ArgumentError, FromArgOptional, FromArgs, FuncArgs, IntoFuncArgs, KwArgs, OptionalArg,
OptionalOption, PosArgs,
ArgumentError, FromArgOptional, FromArgs, FuncArgs, IntoFuncArgs, KwArgs, KwArgsMap,
OptionalArg, OptionalOption, PosArgs,
};
pub use arithmetic::{PyArithmeticValue, PyComparisonValue};
pub use buffer::{ArgAsciiBuffer, ArgBytesLike, ArgMemoryBuffer, ArgStrOrBytesLike};
+463 -187
View File
@@ -4,10 +4,10 @@
use crate::common::linked_list::LinkedList;
use crate::common::lock::{PyMutex, PyRwLock};
use crate::object::{GC_PERMANENT, GC_UNTRACKED, GcLink};
use crate::object::{GC_NO_OWNER, GC_PERMANENT, GC_UNTRACKED, GcLink, GcOwner};
use crate::{AsObject, PyObject, PyObjectRef};
use core::ptr::NonNull;
use core::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering};
use core::sync::atomic::{AtomicBool, AtomicU16, AtomicU32, AtomicUsize, Ordering};
use std::collections::HashSet;
fn elapsed_secs(
@@ -56,10 +56,12 @@ pub struct GcStats {
pub duration: f64,
}
/// A single GC generation with intrusive linked list
/// One generation's collection policy and statistics, per interpreter.
///
/// The objects themselves live in the process-wide lists on [`GcState`], so the
/// occupancy count sits there; what an interpreter owns is when to collect and
/// what its own collections have done.
pub struct GcGeneration {
/// Number of objects in this generation
count: AtomicUsize,
/// Threshold for triggering collection
threshold: AtomicU32,
/// Collection statistics
@@ -70,7 +72,6 @@ impl GcGeneration {
#[must_use]
pub const fn new(threshold: u32) -> Self {
Self {
count: AtomicUsize::new(0),
threshold: AtomicU32::new(threshold),
stats: PyMutex::new(GcStats {
collections: 0,
@@ -82,16 +83,14 @@ impl GcGeneration {
}
}
pub fn count(&self) -> usize {
self.count.load(Ordering::SeqCst)
}
/// Relaxed: this is policy read once per allocation, and a collection
/// racing `gc.set_threshold()` may use either value.
pub fn threshold(&self) -> u32 {
self.threshold.load(Ordering::SeqCst)
self.threshold.load(Ordering::Relaxed)
}
pub fn set_threshold(&self, value: u32) {
self.threshold.store(value, Ordering::SeqCst);
self.threshold.store(value, Ordering::Relaxed);
}
pub fn stats(&self) -> GcStats {
@@ -131,6 +130,26 @@ impl GcGeneration {
}
}
/// Drop one from a generation's occupancy.
///
/// A collection resets the counts of the generations it emptied, but it only
/// empties its own interpreter's objects; another interpreter's stay behind with
/// the count already zeroed, and untracking one of those must not wrap.
fn release_count(count: &AtomicUsize) {
if count.load(Ordering::Relaxed) > 0 {
count.fetch_sub(1, Ordering::Relaxed);
}
}
/// Whether `owner`'s collections act on `obj`.
///
/// Objects with no owner — everything the shared context allocates, and anything
/// allocated with no interpreter current — belong to all of them.
fn is_owned_by(obj: &PyObject, owner: GcOwner) -> bool {
let obj_owner = obj.gc_owner();
obj_owner == owner || obj_owner == GC_NO_OWNER
}
/// Wrapper for NonNull<PyObject> to impl Hash/Eq for use in temporary collection sets.
/// Only used within collect_inner, never shared across threads.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
@@ -146,41 +165,83 @@ struct GcPtr(NonNull<PyObject>);
/// well-defined while all other threads are parked at a safepoint. Restarting
/// happens explicitly once the snapshot has pinned every object; `Drop` is a
/// backstop that also restarts on the early-return paths.
///
/// A collection acts on one interpreter's objects, but its candidates include
/// the ones no interpreter owns, which every interpreter can reference and so
/// incref. Reading a refcount that another interpreter is changing is what
/// makes an object look unreachable when it is not, so every live interpreter
/// is stopped, not just the collecting one. Stopping in `runtime` id order
/// keeps exclusion acquisition ordered; the `collecting` mutex additionally
/// serializes collections process-wide, so no second collector can take these
/// exclusions in another order.
#[cfg(feature = "threading")]
struct CollectStopTheWorld {
vm: *const crate::VirtualMachine,
stopped: bool,
/// Stopped interpreter states, in stop order. Held as strong references so
/// an interpreter cannot be dropped between stop and restart, and kept past
/// the restart so that releasing the last one — which frees that
/// interpreter's objects, and so removes them from these lists — happens
/// after the collection has let go of the generation locks.
stopped: Vec<crate::common::rc::PyRc<crate::vm::PyGlobalState>>,
/// Keeps interpreters from registering between the snapshot below and the
/// restart. One registered in that window would be missing from `stopped`,
/// so its bootstrap would keep running — and mutating the shared generation
/// lists — while this collection reads them.
admission: Option<parking_lot::MutexGuard<'static, ()>>,
restarted: bool,
}
#[cfg(feature = "threading")]
impl CollectStopTheWorld {
/// Request stop-the-world when the current thread has an attached VM.
/// Falls back to no barrier when no VM is attached (the tracked-object
/// reads then run without other threads only if the caller guarantees it).
/// Request stop-the-world on every live interpreter when the current thread
/// has an attached VM. Falls back to no barrier when no VM is attached (the
/// tracked-object reads then run without other threads only if the caller
/// guarantees it).
fn new() -> Self {
let vm = crate::vm::thread::try_with_current_vm(|vm| {
vm.state.stop_the_world.stop_the_world(vm);
vm as *const crate::VirtualMachine
});
match vm {
Some(vm) => Self { vm, stopped: true },
None => Self {
vm: core::ptr::null(),
stopped: false,
},
// No attached VM means no interpreter is running Python on this thread;
// keep the historical no-barrier fallback.
if !crate::vm::thread::current_vm_is_set() {
return Self {
stopped: Vec::new(),
admission: None,
restarted: true,
};
}
// Accumulate into a live `Self` rather than a bare Vec: if a later
// `stop_the_world` unwinds, dropping this guard restarts the
// interpreters already stopped, instead of leaving their threads parked
// and their exclusion held forever.
let mut guard = Self {
stopped: Vec::new(),
admission: Some(crate::vm::runtime::lock_admission_for_stop()),
restarted: false,
};
for state in crate::vm::runtime::live_interpreter_states() {
state.stop_the_world.stop_the_world(&state);
guard.stopped.push(state);
}
guard
}
/// Restart the world. Idempotent.
fn restart(&mut self) {
if self.stopped {
// SAFETY: the current thread stays attached to this VM for the
// whole collection — the VM is never popped from the thread's VM
// stack while collecting — so the pointer is valid here.
let vm = unsafe { &*self.vm };
vm.state.stop_the_world.start_the_world(vm);
self.stopped = false;
if self.restarted {
return;
}
self.restarted = true;
// Reverse of the stop order. The references stay until this guard is
// dropped; see the field comment.
for state in self.stopped.iter().rev() {
state.stop_the_world.start_the_world(state);
}
// Nothing is parked any more, so registration may resume.
self.admission = None;
}
/// Whether this collection actually stopped the world.
#[cfg(all(unix, debug_assertions))]
fn is_stopped(&self) -> bool {
!self.stopped.is_empty()
}
}
@@ -191,29 +252,35 @@ impl Drop for CollectStopTheWorld {
}
}
/// Global GC state
/// The process-wide object lists every interpreter's collections walk.
///
/// Interpreter-owned policy and results live in [`GcInterpreterState`]; what is
/// here is shared because the lists are: an object is untracked from
/// `default_dealloc`, where no interpreter is in scope, so it has to be findable
/// without one.
pub struct GcState {
/// 3 generations (0 = youngest, 2 = oldest)
pub generations: [GcGeneration; 3],
/// Permanent generation (frozen objects)
pub permanent: GcGeneration,
/// GC enabled flag
pub enabled: AtomicBool,
/// Per-generation intrusive linked lists for object tracking.
/// Objects start in gen0, survivors are promoted to gen1, then gen2.
generation_lists: [PyRwLock<LinkedList<GcLink, PyObject>>; 3],
/// Frozen/permanent objects (excluded from normal GC)
permanent_list: PyRwLock<LinkedList<GcLink, PyObject>>,
/// Debug flags
pub debug: AtomicU32,
/// gc.garbage list (uncollectable objects with __del__)
pub garbage: PyMutex<Vec<PyObjectRef>>,
/// gc.callbacks list
pub callbacks: PyMutex<Vec<PyObjectRef>>,
/// Number of tracked objects per generation, across all interpreters.
///
/// Advisory: they drive the collection threshold and `gc.get_count()`, and
/// the generation locks — not these counters — order the list changes they
/// describe. Every access is therefore relaxed, which keeps the tracking and
/// untracking of every object off the barrier path.
counts: [AtomicUsize; 3],
/// Number of frozen objects. Advisory, like `counts`.
permanent_count: AtomicUsize,
/// Mutex for collection (prevents concurrent collections)
collecting: PyMutex<()>,
/// Allocation counter for gen0
alloc_count: AtomicUsize,
/// Next `gc_owner` tag to hand to an interpreter.
next_owner: AtomicU16,
/// Tags of interpreters that are gone. Their objects outlived them, so a
/// collection adopts them — tags them `GC_NO_OWNER` again — as it walks,
/// rather than leaving them for a collector that will never come.
retired: PyMutex<Vec<GcOwner>>,
}
// SAFETY: All fields are either inherently Send/Sync (atomics, RwLock, Mutex) or protected by PyMutex.
@@ -233,103 +300,70 @@ impl GcState {
#[must_use]
pub const fn new() -> Self {
Self {
generations: [
GcGeneration::new(2000), // young
GcGeneration::new(10), // old[0]
GcGeneration::new(0), // old[1]
],
permanent: GcGeneration::new(0),
enabled: AtomicBool::new(true),
generation_lists: [
PyRwLock::new(LinkedList::new()),
PyRwLock::new(LinkedList::new()),
PyRwLock::new(LinkedList::new()),
],
permanent_list: PyRwLock::new(LinkedList::new()),
debug: AtomicU32::new(0),
garbage: PyMutex::new(Vec::new()),
callbacks: PyMutex::new(Vec::new()),
counts: [
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
],
permanent_count: AtomicUsize::new(0),
collecting: PyMutex::new(()),
alloc_count: AtomicUsize::new(0),
next_owner: AtomicU16::new(GC_NO_OWNER + 1),
retired: PyMutex::new(Vec::new()),
}
}
/// Check if GC is enabled
pub fn is_enabled(&self) -> bool {
self.enabled.load(Ordering::SeqCst)
/// Reserve a tag for a new interpreter. Tags are never reused; exhausting
/// the tag space falls back to `GC_NO_OWNER`, which costs isolation but
/// stays correct, rather than aliasing a live interpreter.
fn alloc_owner(&self) -> GcOwner {
self.next_owner
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |next| {
next.checked_add(1)
})
.unwrap_or(GC_NO_OWNER)
}
/// Enable GC
pub fn enable(&self) {
self.enabled.store(true, Ordering::SeqCst);
}
/// Disable GC
pub fn disable(&self) {
self.enabled.store(false, Ordering::SeqCst);
}
/// Get debug flags
pub fn get_debug(&self) -> GcDebugFlags {
GcDebugFlags::from_bits_truncate(self.debug.load(Ordering::SeqCst))
}
/// Set debug flags
pub fn set_debug(&self, flags: GcDebugFlags) {
self.debug.store(flags.bits(), Ordering::SeqCst);
}
/// Get thresholds for all generations
pub fn get_threshold(&self) -> (u32, u32, u32) {
(
self.generations[0].threshold(),
self.generations[1].threshold(),
self.generations[2].threshold(),
)
}
/// Set thresholds
pub fn set_threshold(&self, t0: u32, t1: Option<u32>, t2: Option<u32>) {
self.generations[0].set_threshold(t0);
if let Some(t1) = t1 {
self.generations[1].set_threshold(t1);
}
if let Some(t2) = t2 {
self.generations[2].set_threshold(t2);
/// Record that `owner`'s interpreter is gone, so the next collection adopts
/// whatever it left behind. Retagging the objects here would mean walking
/// every list under an interpreter drop, which happens while a collection
/// holds the collecting lock.
fn retire_owner(&self, owner: GcOwner) {
if owner == GC_NO_OWNER {
return;
}
self.retired.lock().push(owner);
}
/// Get counts for all generations
/// Get counts for all generations. Tracked objects are shared, so these are
/// process-wide even though the thresholds they are compared against are
/// per interpreter.
pub fn get_count(&self) -> (usize, usize, usize) {
(
self.generations[0].count(),
self.generations[1].count(),
self.generations[2].count(),
self.counts[0].load(Ordering::Relaxed),
self.counts[1].load(Ordering::Relaxed),
self.counts[2].load(Ordering::Relaxed),
)
}
/// Get statistics for all generations
pub fn get_stats(&self) -> [GcStats; 3] {
[
self.generations[0].stats(),
self.generations[1].stats(),
self.generations[2].stats(),
]
}
/// Track a new object (add to gen0).
/// Track a new object (add to gen0) as owned by `owner`.
/// O(1) — intrusive linked list push_front, no hashing.
///
/// # Safety
/// obj must be a valid pointer to a PyObject
pub unsafe fn track_object(&self, obj: NonNull<PyObject>) {
pub unsafe fn track_object(&self, obj: NonNull<PyObject>, owner: GcOwner) {
let obj_ref = unsafe { obj.as_ref() };
obj_ref.set_gc_tracked();
obj_ref.set_gc_generation(0);
obj_ref.set_gc_owner(owner);
self.generation_lists[0].write().push_front(obj);
self.generations[0].count.fetch_add(1, Ordering::SeqCst);
self.alloc_count.fetch_add(1, Ordering::SeqCst);
self.counts[0].fetch_add(1, Ordering::Relaxed);
}
/// Untrack an object (remove from GC lists).
@@ -348,10 +382,10 @@ impl GcState {
(
&self.generation_lists[obj_gen as usize]
as &PyRwLock<LinkedList<GcLink, PyObject>>,
&self.generations[obj_gen as usize].count,
&self.counts[obj_gen as usize],
)
} else if obj_gen == GC_PERMANENT {
(&self.permanent_list, &self.permanent.count)
(&self.permanent_list, &self.permanent_count)
} else {
return; // GC_UNTRACKED or unknown — already untracked
};
@@ -363,7 +397,7 @@ impl GcState {
continue; // Retry with the updated generation
}
if unsafe { list.remove(obj) }.is_some() {
count.fetch_sub(1, Ordering::SeqCst);
release_count(count);
obj_ref.clear_gc_tracked();
obj_ref.set_gc_generation(GC_UNTRACKED);
} else {
@@ -381,14 +415,18 @@ impl GcState {
}
}
/// Get tracked objects (for gc.get_objects)
/// If generation is None, returns all tracked objects.
/// If generation is Some(n), returns objects in generation n only.
pub fn get_objects(&self, generation: Option<i32>) -> Vec<PyObjectRef> {
/// Get the objects `owner` tracks (for gc.get_objects), plus the ones no
/// interpreter owns.
/// If generation is None, returns all such objects.
/// If generation is Some(n), returns those in generation n only.
pub fn get_objects(&self, generation: Option<i32>, owner: GcOwner) -> Vec<PyObjectRef> {
fn collect_from_list(
list: &LinkedList<GcLink, PyObject>,
owner: GcOwner,
) -> impl Iterator<Item = PyObjectRef> + '_ {
list.iter().filter_map(|obj| obj.try_to_owned())
list.iter()
.filter(move |obj| is_owned_by(obj, owner))
.filter_map(|obj| obj.try_to_owned())
}
match generation {
@@ -396,14 +434,14 @@ impl GcState {
// Return all tracked objects from all generations + permanent
let mut result = Vec::new();
for gen_list in &self.generation_lists {
result.extend(collect_from_list(&gen_list.read()));
result.extend(collect_from_list(&gen_list.read(), owner));
}
result.extend(collect_from_list(&self.permanent_list.read()));
result.extend(collect_from_list(&self.permanent_list.read(), owner));
result
}
Some(g) if (0..=2).contains(&g) => {
let guard = self.generation_lists[g as usize].read();
collect_from_list(&guard).collect()
collect_from_list(&guard, owner).collect()
}
_ => Vec::new(),
}
@@ -412,14 +450,14 @@ impl GcState {
/// Check if automatic GC should run and run it if needed.
/// Called after object allocation.
/// Returns true if GC was run, false otherwise.
pub fn maybe_collect(&self) -> bool {
if !self.is_enabled() {
fn maybe_collect(&self, gc: &GcInterpreterState) -> bool {
if !gc.is_enabled() {
return false;
}
// Check gen0 threshold
let count0 = self.generations[0].count.load(Ordering::SeqCst) as u32;
let threshold0 = self.generations[0].threshold();
let count0 = self.counts[0].load(Ordering::Relaxed) as u32;
let threshold0 = gc.generations[0].threshold();
if threshold0 > 0 && count0 >= threshold0 {
#[cfg(feature = "threading")]
{
@@ -435,7 +473,7 @@ impl GcState {
// thread whose frames could be read mid-mutation, so collect inline.
#[cfg(not(feature = "threading"))]
{
self.collect(0);
self.collect_inner(gc, 0, false);
return true;
}
}
@@ -443,18 +481,13 @@ impl GcState {
false
}
/// Perform garbage collection on the given generation
pub fn collect(&self, generation: usize) -> CollectResult {
self.collect_inner(generation, false)
}
/// Force collection even if GC is disabled (for manual gc.collect() calls)
pub fn collect_force(&self, generation: usize) -> CollectResult {
self.collect_inner(generation, true)
}
fn collect_inner(&self, generation: usize, force: bool) -> CollectResult {
if !force && !self.is_enabled() {
fn collect_inner(
&self,
gc: &GcInterpreterState,
generation: usize,
force: bool,
) -> CollectResult {
if !force && !gc.is_enabled() {
return CollectResult::default();
}
@@ -473,7 +506,7 @@ impl GcState {
core::sync::atomic::fence(Ordering::SeqCst);
let generation = generation.min(2);
let debug = self.get_debug();
let debug = gc.get_debug();
// Clear the method cache to release strong references that
// might prevent cycle collection (_PyType_ClearCache).
@@ -511,26 +544,56 @@ impl GcState {
.map(|i| self.generation_lists[i].read())
.collect();
// Only this interpreter's objects, plus the ones no interpreter owns.
// Another interpreter's objects stay out of the candidate set, so they
// act as external roots: anything they reference survives this pass.
let owner = gc.owner;
// Sorted so that the test below, which every scanned object pays for,
// stays logarithmic in the number of interpreters that have been
// dropped instead of linear.
let retired = {
let mut retired = self.retired.lock().clone();
retired.sort_unstable();
retired
};
let mut collecting: HashSet<GcPtr> = HashSet::new();
for gen_list in &gen_locks {
for obj in gen_list.iter() {
if obj.strong_count() > 0 {
if retired.binary_search(&obj.gc_owner()).is_ok() {
obj.set_gc_owner(GC_NO_OWNER);
}
if obj.strong_count() > 0 && is_owned_by(obj, owner) {
collecting.insert(GcPtr(NonNull::from(obj)));
}
}
}
// A full collection is the only one that sees every generation, so it
// is where adoption finishes and the tags stop being tracked.
if generation == 2 && !retired.is_empty() {
for obj in self.permanent_list.read().iter() {
if retired.binary_search(&obj.gc_owner()).is_ok() {
obj.set_gc_owner(GC_NO_OWNER);
}
}
// Only the tags this scan saw: one retired while it ran still has
// objects nobody has adopted.
self.retired
.lock()
.retain(|tag| retired.binary_search(tag).is_err());
}
if collecting.is_empty() {
// Reset counts for generations whose objects were promoted away.
// For gen2 (oldest), survivors stay in-place so don't reset gen2 count.
let reset_end = if generation >= 2 { 2 } else { generation + 1 };
for i in 0..reset_end {
self.generations[i].count.store(0, Ordering::SeqCst);
self.counts[i].store(0, Ordering::Relaxed);
}
let duration = elapsed_secs(start_time);
self.generations[generation].update_stats(0, 0, 0, duration);
gc.generations[generation].update_stats(0, 0, 0, duration);
return CollectResult {
collected: 0,
uncollectable: 0,
@@ -638,7 +701,7 @@ impl GcState {
// because stack-allocated frames update only CURRENT_FRAME (via
// set_current_frame_nosave), not top_frame.
#[cfg(all(unix, feature = "threading", debug_assertions))]
if stw.stopped {
if stw.is_stopped() {
let unreachable_set: HashSet<GcPtr> = unreachable.iter().copied().collect();
let mut cur = crate::vm::thread::get_current_frame();
while !cur.is_null() {
@@ -701,12 +764,12 @@ impl GcState {
self.promote_survivors(generation, &survivor_refs);
let reset_end = if generation >= 2 { 2 } else { generation + 1 };
for i in 0..reset_end {
self.generations[i].count.store(0, Ordering::SeqCst);
self.counts[i].store(0, Ordering::Relaxed);
}
let duration = elapsed_secs(start_time);
self.generations[generation].update_stats(0, 0, candidates, duration);
gc.generations[generation].update_stats(0, 0, candidates, duration);
return CollectResult {
collected: 0,
uncollectable: 0,
@@ -724,12 +787,12 @@ impl GcState {
self.promote_survivors(generation, &survivor_refs);
let reset_end = if generation >= 2 { 2 } else { generation + 1 };
for i in 0..reset_end {
self.generations[i].count.store(0, Ordering::SeqCst);
self.counts[i].store(0, Ordering::Relaxed);
}
let duration = elapsed_secs(start_time);
self.generations[generation].update_stats(0, 0, candidates, duration);
gc.generations[generation].update_stats(0, 0, candidates, duration);
return CollectResult {
collected: 0,
uncollectable: 0,
@@ -849,7 +912,7 @@ impl GcState {
}
if debug.contains(GcDebugFlags::SAVEALL) {
let mut garbage_guard = self.garbage.lock();
let mut garbage_guard = gc.garbage.lock();
for obj_ref in &truly_dead {
garbage_guard.push(obj_ref.clone());
}
@@ -926,7 +989,10 @@ impl GcState {
reason = "Iteration order doesn't matter here"
)]
for &ptr in &late_resurrected {
unsafe { self.track_object(ptr.0) };
// Re-tracking a resurrected object: it keeps the owner it
// was allocated under.
let owner = unsafe { ptr.0.as_ref() }.gc_owner();
unsafe { self.track_object(ptr.0, owner) };
}
}
rustpython_common::refcount::with_deferred_drops(|| {
@@ -950,12 +1016,12 @@ impl GcState {
// For gen2 (oldest), survivors stay in-place so don't reset gen2 count.
let reset_end = if generation >= 2 { 2 } else { generation + 1 };
for i in 0..reset_end {
self.generations[i].count.store(0, Ordering::SeqCst);
self.counts[i].store(0, Ordering::Relaxed);
}
let duration = elapsed_secs(start_time);
self.generations[generation].update_stats(collected, 0, candidates, duration);
gc.generations[generation].update_stats(collected, 0, candidates, duration);
CollectResult {
collected,
@@ -998,14 +1064,10 @@ impl GcState {
}
if unsafe { src.remove(ptr) }.is_some() {
self.generations[src_gen]
.count
.fetch_sub(1, Ordering::SeqCst);
release_count(&self.counts[src_gen]);
dst.push_front(ptr);
self.generations[next_gen]
.count
.fetch_add(1, Ordering::SeqCst);
self.counts[next_gen].fetch_add(1, Ordering::Relaxed);
obj.set_gc_generation(next_gen as u8);
}
@@ -1015,45 +1077,66 @@ impl GcState {
/// Get count of frozen objects
pub fn get_freeze_count(&self) -> usize {
self.permanent.count()
self.permanent_count.load(Ordering::Relaxed)
}
/// Freeze all tracked objects (move to permanent generation).
/// Freeze the objects `owner` could collect (move them to the permanent
/// generation).
/// Lock order: generation_lists[i] → permanent_list (consistent with unfreeze).
pub fn freeze(&self) {
fn freeze(&self, owner: GcOwner) {
let mut count = 0usize;
for (gen_idx, gen_list) in self.generation_lists.iter().enumerate() {
let mut list = gen_list.write();
let mut perm = self.permanent_list.write();
while let Some(ptr) = list.pop_front() {
let moving: Vec<_> = list
.iter()
.filter(|obj| is_owned_by(obj, owner))
.map(NonNull::from)
.collect();
for ptr in moving {
if unsafe { list.remove(ptr) }.is_none() {
continue;
}
perm.push_front(ptr);
unsafe { ptr.as_ref().set_gc_generation(GC_PERMANENT) };
count += 1;
release_count(&self.counts[gen_idx]);
}
self.generations[gen_idx].count.store(0, Ordering::SeqCst);
}
self.permanent.count.fetch_add(count, Ordering::SeqCst);
self.permanent_count.fetch_add(count, Ordering::Relaxed);
}
/// Unfreeze all objects (move from permanent to gen2).
/// Unfreeze the objects `owner` froze (move them from permanent to gen2).
/// Lock order: generation_lists[2] → permanent_list (consistent with freeze).
pub fn unfreeze(&self) {
fn unfreeze(&self, owner: GcOwner) {
let mut count = 0usize;
{
let mut gen2 = self.generation_lists[2].write();
let mut perm_list = self.permanent_list.write();
while let Some(ptr) = perm_list.pop_front() {
let moving: Vec<_> = perm_list
.iter()
.filter(|obj| is_owned_by(obj, owner))
.map(NonNull::from)
.collect();
for ptr in moving {
if unsafe { perm_list.remove(ptr) }.is_none() {
continue;
}
gen2.push_front(ptr);
unsafe { ptr.as_ref().set_gc_generation(2) };
count += 1;
}
self.permanent.count.store(0, Ordering::SeqCst);
let _ = self.permanent_count.fetch_update(
Ordering::Relaxed,
Ordering::Relaxed,
|permanent| Some(permanent.saturating_sub(count)),
);
}
self.generations[2].count.fetch_add(count, Ordering::SeqCst);
self.counts[2].fetch_add(count, Ordering::Relaxed);
}
/// Reset all locks to unlocked state after fork().
@@ -1070,13 +1153,7 @@ impl GcState {
unsafe {
reinit_mutex_after_fork(&self.collecting);
reinit_mutex_after_fork(&self.garbage);
reinit_mutex_after_fork(&self.callbacks);
for generation in &self.generations {
generation.reinit_stats_after_fork();
}
self.permanent.reinit_stats_after_fork();
reinit_mutex_after_fork(&self.retired);
for rw in &self.generation_lists {
reinit_rwlock_after_fork(rw);
@@ -1086,11 +1163,196 @@ impl GcState {
}
}
/// Per-interpreter garbage collector state (≈ `PyInterpreterState.gc`).
///
/// The generation lists are process-wide (see [`GcState`]); what an interpreter
/// owns is the policy applied to them and the results — which objects its
/// collections consider, whether they run automatically, and where uncollectable
/// objects end up.
pub struct GcInterpreterState {
/// Tag written into every object this interpreter tracks.
owner: GcOwner,
/// Per-generation thresholds and statistics.
pub generations: [GcGeneration; 3],
/// GC enabled flag
enabled: AtomicBool,
/// Debug flags
debug: AtomicU32,
/// Uncollectable objects saved by this interpreter's collections, drained
/// into `py_garbage` by `gc.collect()`.
pub garbage: PyMutex<Vec<PyObjectRef>>,
/// `gc.garbage`
pub py_garbage: crate::builtins::PyListRef,
/// `gc.callbacks`
pub py_callbacks: crate::builtins::PyListRef,
}
impl GcInterpreterState {
pub fn new(ctx: &crate::vm::Context) -> Self {
Self {
owner: gc_state().alloc_owner(),
generations: [
GcGeneration::new(2000), // young
GcGeneration::new(10), // old[0]
GcGeneration::new(0), // old[1]
],
enabled: AtomicBool::new(true),
debug: AtomicU32::new(0),
garbage: PyMutex::new(Vec::new()),
py_garbage: ctx.new_list(Vec::new()),
py_callbacks: ctx.new_list(Vec::new()),
}
}
/// Check if GC is enabled.
///
/// Relaxed, like [`GcGeneration::threshold`]: it is read once per
/// allocation, and an allocation racing `gc.disable()` may use either value.
pub fn is_enabled(&self) -> bool {
self.enabled.load(Ordering::Relaxed)
}
/// Enable GC
pub fn enable(&self) {
self.enabled.store(true, Ordering::Relaxed);
}
/// Disable GC
pub fn disable(&self) {
self.enabled.store(false, Ordering::Relaxed);
}
/// Get debug flags
pub fn get_debug(&self) -> GcDebugFlags {
GcDebugFlags::from_bits_truncate(self.debug.load(Ordering::SeqCst))
}
/// Set debug flags
pub fn set_debug(&self, flags: GcDebugFlags) {
self.debug.store(flags.bits(), Ordering::SeqCst);
}
/// Get thresholds for all generations
pub fn get_threshold(&self) -> (u32, u32, u32) {
(
self.generations[0].threshold(),
self.generations[1].threshold(),
self.generations[2].threshold(),
)
}
/// Set thresholds
pub fn set_threshold(&self, t0: u32, t1: Option<u32>, t2: Option<u32>) {
self.generations[0].set_threshold(t0);
if let Some(t1) = t1 {
self.generations[1].set_threshold(t1);
}
if let Some(t2) = t2 {
self.generations[2].set_threshold(t2);
}
}
/// Get statistics for all generations
pub fn get_stats(&self) -> [GcStats; 3] {
[
self.generations[0].stats(),
self.generations[1].stats(),
self.generations[2].stats(),
]
}
/// Perform garbage collection on the given generation
pub fn collect(&self, generation: usize) -> CollectResult {
gc_state().collect_inner(self, generation, false)
}
/// Force collection even if GC is disabled (for manual gc.collect() calls)
pub fn collect_force(&self, generation: usize) -> CollectResult {
gc_state().collect_inner(self, generation, true)
}
/// The tracked objects this interpreter can reach (for gc.get_objects).
pub fn get_objects(&self, generation: Option<i32>) -> Vec<PyObjectRef> {
gc_state().get_objects(generation, self.owner)
}
/// Move the objects this interpreter could collect into the permanent
/// generation.
pub fn freeze(&self) {
gc_state().freeze(self.owner);
}
/// Move them back out of it.
pub fn unfreeze(&self) {
gc_state().unfreeze(self.owner);
}
/// Reset this interpreter's GC locks to unlocked state after fork().
///
/// # Safety
/// Must only be called after fork() in the child process when no other
/// threads exist. The calling thread must NOT hold any of these locks.
#[cfg(all(unix, feature = "threading"))]
pub unsafe fn reinit_after_fork(&self) {
unsafe {
crate::common::lock::reinit_mutex_after_fork(&self.garbage);
for generation in &self.generations {
generation.reinit_stats_after_fork();
}
}
}
}
impl Drop for GcInterpreterState {
fn drop(&mut self) {
// Objects this interpreter tracked can outlive it (another interpreter
// may still hold one). Clearing the tag hands them to every collection
// instead of stranding them. The tag itself is not handed back: it stays
// retired so that a later interpreter cannot inherit these objects.
gc_state().retire_owner(self.owner);
}
}
/// The tag `track_object` should write for the interpreter running now.
#[must_use]
pub fn current_owner() -> GcOwner {
// SAFETY: the pointee is owned by the `PyGlobalState` of the VM on top of
// this thread's VM stack, which outlives the section this call runs in.
crate::vm::thread::current_gc_state().map_or(GC_NO_OWNER, |gc| unsafe { gc.as_ref() }.owner)
}
/// Track a freshly allocated object under the interpreter running now, and let
/// it collect if the allocation pushed gen0 past its threshold.
///
/// # Safety
/// obj must be a valid pointer to a PyObject that is not already tracked.
pub(crate) unsafe fn track_new_object(obj: NonNull<PyObject>) {
let state = gc_state();
let Some(gc) = crate::vm::thread::current_gc_state() else {
// No interpreter is running: the shared context builds its own objects
// this way. They are left unowned, so every interpreter collects them.
unsafe { state.track_object(obj, GC_NO_OWNER) };
return;
};
// SAFETY: as in `current_owner`.
let gc = unsafe { gc.as_ref() };
unsafe { state.track_object(obj, gc.owner) };
state.maybe_collect(gc);
}
/// Get a reference to the GC state.
///
/// In threading mode this is a true global (OnceLock).
/// In non-threading mode this is thread-local, because PyRwLock/PyMutex
/// use Cell-based locks that are not Sync.
///
/// Every interpreter's tracked objects live in these lists, because untracking
/// happens in `default_dealloc`, where no interpreter is in scope to route to.
/// What a collection *acts on* is still one interpreter's own objects, selected
/// by the `gc_owner` tag; [`GcInterpreterState`] holds the rest of the state
/// that goes with that. The counts here, and so `gc.get_count()` and
/// `gc.get_freeze_count()`, stay process-wide: they measure how full these
/// lists are.
pub fn gc_state() -> &'static GcState {
rustpython_common::static_cell! {
static GC_STATE: GcState;
@@ -1102,18 +1364,21 @@ pub fn gc_state() -> &'static GcState {
mod tests {
use super::*;
fn interpreter_state() -> GcInterpreterState {
GcInterpreterState::new(crate::vm::Context::genesis())
}
#[test]
fn gc_state_default() {
let state = GcState::new();
let state = interpreter_state();
assert!(state.is_enabled());
assert_eq!(state.get_debug(), GcDebugFlags::empty());
assert_eq!(state.get_threshold(), (2000, 10, 0));
assert_eq!(state.get_count(), (0, 0, 0));
}
#[test]
fn gc_enable_disable() {
let state = GcState::new();
let state = interpreter_state();
assert!(state.is_enabled());
state.disable();
assert!(!state.is_enabled());
@@ -1123,18 +1388,29 @@ mod tests {
#[test]
fn gc_threshold() {
let state = GcState::new();
let state = interpreter_state();
state.set_threshold(100, Some(20), Some(30));
assert_eq!(state.get_threshold(), (100, 20, 30));
}
#[test]
fn gc_debug_flags() {
let state = GcState::new();
let state = interpreter_state();
state.set_debug(GcDebugFlags::STATS | GcDebugFlags::COLLECTABLE);
assert_eq!(
state.get_debug(),
GcDebugFlags::STATS | GcDebugFlags::COLLECTABLE
);
}
/// Live interpreters never share an owner tag, or their collections would
/// reach each other's objects.
#[test]
fn gc_owner_tags_are_distinct_while_live() {
let first = interpreter_state();
let second = interpreter_state();
assert_ne!(first.owner, second.owner);
assert_ne!(first.owner, GC_NO_OWNER);
assert_ne!(second.owner, GC_NO_OWNER);
}
}
+5 -1
View File
@@ -109,7 +109,11 @@ pub use self::object::{
AsObject, Py, PyAtomicRef, PyExact, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact,
PyResult, PyStackRef, PyWeakRef,
};
pub use self::vm::{Context, Interpreter, InterpreterBuilder, Settings, VirtualMachine};
pub use self::vm::runtime;
pub use self::vm::{
Context, Interpreter, InterpreterBuilder, InterpreterInfo, InterpreterWhence,
MAIN_INTERPRETER_ID, Settings, VirtualMachine,
};
pub use rustpython_common as common;
pub use rustpython_compiler_core::{bytecode, frozen};
+74 -4
View File
@@ -303,6 +303,18 @@ bitflags::bitflags! {
/// GC generation constants
pub(crate) const GC_UNTRACKED: u8 = 0xFF;
pub(crate) const GC_PERMANENT: u8 = 3;
/// Width of an interpreter's `gc_owner` tag.
///
/// Sized to the padding the header alignment already forces, so the tag costs
/// no space on either pointer width. Running out of tags is not an error: an
/// interpreter that gets none uses [`GC_NO_OWNER`] and its objects stay
/// collectable by every interpreter, which is how they behaved before tagging.
pub(crate) type GcOwner = u16;
/// `gc_owner` of an object that belongs to no single interpreter: everything
/// the shared context allocates, and anything allocated with no interpreter
/// current. Every interpreter collects these.
pub(crate) const GC_NO_OWNER: GcOwner = 0;
/// Link implementation for GC intrusive linked list tracking
pub(crate) struct GcLink;
@@ -389,6 +401,10 @@ pub(super) struct PyInner<T> {
/// GC generation index (0-2=gen, GC_PERMANENT=permanent, GC_UNTRACKED=not tracked).
/// Uses PyAtomic for interior mutability (writes happen through &self under list locks).
pub(super) gc_generation: PyAtomic<u8>,
/// Interpreter that tracked this object, or `GC_NO_OWNER`. Written by
/// `track_object`; read to scope a collection to one interpreter.
/// Sits in what would otherwise be padding, so it costs no space.
pub(super) gc_owner: PyAtomic<GcOwner>,
/// Intrusive linked list pointers for GC generational tracking
pub(super) gc_pointers: Pointers<PyObject>,
@@ -398,6 +414,11 @@ pub(super) struct PyInner<T> {
}
pub(crate) const SIZEOF_PYOBJECT_HEAD: usize = core::mem::size_of::<PyInner<()>>();
// ref_count, vtable, gc_pointers (two) and typ are one word each; the gc bits,
// generation and owner share the word of padding their alignment forces. Adding
// to that group is free only while this holds.
const _: () = assert!(SIZEOF_PYOBJECT_HEAD == 6 * core::mem::size_of::<usize>());
impl<T> PyInner<T> {
/// Read type flags and member_count via raw pointers to avoid Stacked Borrows
/// violations during bootstrap, where type objects have self-referential typ pointers.
@@ -1052,6 +1073,16 @@ impl InstanceDict {
self.d.read().clone()
}
/// Run `f` on the dict without cloning it.
///
/// For callers that only need to look at the dict — a predicate, a version
/// stamp — this drops the refcount round-trip [`Self::get`] pays. `f` runs
/// under the read guard, so it must not run Python or take this lock again.
#[inline]
pub(crate) fn with<R>(&self, f: impl FnOnce(Option<&Py<crate::builtins::PyDict>>) -> R) -> R {
f(self.d.read().as_deref())
}
#[inline]
pub(crate) fn set(&self, d: Option<PyDictRef>) {
self.replace(d);
@@ -1216,6 +1247,7 @@ impl<T: PyPayload + core::fmt::Debug> PyInner<T> {
vtable: PyObjVTable::of::<T>(),
gc_bits: Radium::new(0),
gc_generation: Radium::new(GC_UNTRACKED),
gc_owner: Radium::new(GC_NO_OWNER),
gc_pointers: Pointers::new(),
typ: PyAtomicRef::from(typ),
payload,
@@ -1228,6 +1260,7 @@ impl<T: PyPayload + core::fmt::Debug> PyInner<T> {
vtable: PyObjVTable::of::<T>(),
gc_bits: Radium::new(0),
gc_generation: Radium::new(GC_UNTRACKED),
gc_owner: Radium::new(GC_NO_OWNER),
gc_pointers: Pointers::new(),
typ: PyAtomicRef::from(typ),
payload,
@@ -1615,6 +1648,28 @@ impl PyObject {
self.instance_dict().and_then(|d| d.get())
}
/// Whether this object currently has an instance dict, without cloning it.
///
/// `false` both for an object with no dict slot and for one whose slot is
/// still empty, which is what `dict().is_none()` reports.
#[inline(always)]
pub fn has_instance_dict(&self) -> bool {
self.instance_dict()
.is_some_and(|d| d.with(|dict| dict.is_some()))
}
/// Run `f` on the instance dict without cloning it; see [`InstanceDict::with`].
#[inline(always)]
pub(crate) fn with_instance_dict<R>(
&self,
f: impl FnOnce(Option<&Py<crate::builtins::PyDict>>) -> R,
) -> R {
match self.instance_dict() {
Some(d) => d.with(f),
None => f(None),
}
}
/// Set the dict field. Returns `Err(dict)` if this object does not have a dict field
/// in the first place.
pub fn set_dict(&self, dict: Option<PyDictRef>) -> Result<(), Option<PyDictRef>> {
@@ -1734,6 +1789,20 @@ impl PyObject {
self.0.gc_generation.store(generation, Ordering::Relaxed);
}
/// The interpreter whose collections consider this object.
#[inline]
pub(crate) fn gc_owner(&self) -> GcOwner {
self.0.gc_owner.load(Ordering::Relaxed)
}
/// Set the owning interpreter. Written by `track_object` before the object
/// enters a generation list, and reset to `GC_NO_OWNER` when the owning
/// interpreter goes away.
#[inline]
pub(crate) fn set_gc_owner(&self, owner: GcOwner) {
self.0.gc_owner.store(owner, Ordering::Relaxed);
}
/// _PyObject_GC_TRACK
#[inline]
pub(crate) fn set_gc_tracked(&self) {
@@ -2392,12 +2461,11 @@ impl<T: PyPayload + crate::object::MaybeTraverse + core::fmt::Debug> PyRef<T> {
if (<T as crate::object::MaybeTraverse>::HAS_TRAVERSE || has_dict || is_heaptype)
&& !T::NEW_REF_UNTRACKED
{
let gc = crate::gc_state::gc_state();
// Tracks under the interpreter running now and collects if this
// allocation pushed gen0 past its threshold.
unsafe {
gc.track_object(ptr.cast());
crate::gc_state::track_new_object(ptr.cast());
}
// Check if automatic GC should run
gc.maybe_collect();
}
Self { ptr }
@@ -2645,6 +2713,7 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
vtable: PyObjVTable::of::<PyType>(),
gc_bits: Radium::new(0),
gc_generation: Radium::new(GC_UNTRACKED),
gc_owner: Radium::new(GC_NO_OWNER),
gc_pointers: Pointers::new(),
payload: type_payload,
},
@@ -2660,6 +2729,7 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
vtable: PyObjVTable::of::<PyType>(),
gc_bits: Radium::new(0),
gc_generation: Radium::new(GC_UNTRACKED),
gc_owner: Radium::new(GC_NO_OWNER),
gc_pointers: Pointers::new(),
payload: object_payload,
},
+1 -1
View File
@@ -9,5 +9,5 @@ pub use self::core::*;
pub use self::ext::*;
pub use self::payload::*;
pub(crate) use core::SIZEOF_PYOBJECT_HEAD;
pub(crate) use core::{GC_PERMANENT, GC_UNTRACKED, GcLink};
pub(crate) use core::{GC_NO_OWNER, GC_PERMANENT, GC_UNTRACKED, GcLink, GcOwner};
pub use traverse::{MaybeTraverse, Traverse, TraverseFn};
+3 -2
View File
@@ -230,7 +230,6 @@ impl PyObject {
dict: Option<PyDictRef>,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
let name = name_str.as_wtf8();
let obj_cls = self.class();
let cls_attr_name = vm.ctx.interned_str(name_str);
let cls_attr = match cls_attr_name.and_then(|name| obj_cls.get_attr(name)) {
@@ -251,7 +250,9 @@ impl PyObject {
let dict = dict.or_else(|| self.dict());
let attr = if let Some(dict) = dict {
dict.get_item_opt(name, vm)?
// `Py<PyStr>` rather than its `&Wtf8`: the key type carries the
// cached hash and compares interned keys by pointer.
dict.get_item_opt(name_str, vm)?
} else {
None
};
+1 -3
View File
@@ -10,13 +10,11 @@ pub(crate) mod _ast {
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::{PyDictRef, PySet, PyStr, PyTupleRef, PyType, PyTypeRef},
class::{PyClassImpl, StaticType},
common::wtf8::Wtf8Buf,
function::{ArgIterable, FuncArgs, KwArgs, PyMethodDef, PyMethodFlags},
stdlib::_ast::repr,
types::{Constructor, Initializer},
warn,
};
use indexmap::IndexMap;
#[pyattr]
#[pyclass(module = "_ast", name = "AST")]
#[derive(Debug, PyPayload)]
@@ -295,7 +293,7 @@ pub(crate) mod _ast {
.map_err(|_| vm.new_type_error("keywords must be strings"))?;
Ok((key.as_wtf8().to_owned(), value))
})
.collect::<PyResult<IndexMap<Wtf8Buf, PyObjectRef>>>()?;
.collect::<PyResult<crate::function::KwArgsMap<PyObjectRef>>>()?;
let result = type_obj.call(FuncArgs::new(vec![], KwArgs::new(kwargs)), vm)?;
Ok(result)
}
+1 -2
View File
@@ -1,6 +1,5 @@
use super::base::{CDATA_BUFFER_METHODS, PyCData, PyCField, StgInfo, StgInfoFlags};
use crate::builtins::{PyList, PyStr, PyTuple, PyType, PyTypeRef, PyUtf8Str};
use crate::common::wtf8::Wtf8Buf;
use crate::convert::ToPyObject;
use crate::function::{FuncArgs, OptionalArg, PySetterValue};
use crate::protocol::{BufferDescriptor, PyBuffer, PyNumberMethods};
@@ -713,7 +712,7 @@ impl PyCStructure {
self_obj: &Py<Self>,
type_obj: &Py<PyType>,
args: &[PyObjectRef],
kwargs: &indexmap::IndexMap<Wtf8Buf, PyObjectRef>,
kwargs: &crate::function::KwArgsMap<PyObjectRef>,
index: usize,
vm: &VirtualMachine,
) -> PyResult<usize> {
+1 -2
View File
@@ -1,7 +1,6 @@
use super::base::{CDATA_BUFFER_METHODS, StgInfoFlags};
use super::{PyCData, PyCField, StgInfo};
use crate::builtins::{PyList, PyStr, PyTuple, PyType, PyTypeRef, PyUtf8Str};
use crate::common::wtf8::Wtf8Buf;
use crate::convert::ToPyObject;
use crate::function::{ArgBytesLike, FuncArgs, OptionalArg, PySetterValue};
use crate::protocol::{BufferDescriptor, PyBuffer};
@@ -582,7 +581,7 @@ impl PyCUnion {
self_obj: &Py<Self>,
type_obj: &Py<PyType>,
args: &[PyObjectRef],
kwargs: &indexmap::IndexMap<Wtf8Buf, PyObjectRef>,
kwargs: &crate::function::KwArgsMap<PyObjectRef>,
index: usize,
vm: &VirtualMachine,
) -> PyResult<usize> {
+1 -2
View File
@@ -15,7 +15,6 @@ mod _functools {
recursion::ReprGuard,
types::{Callable, Constructor, GetDescriptor, Representable},
};
use indexmap::IndexMap;
use rustpython_common::wtf8::Wtf8Buf;
#[derive(FromArgs)]
@@ -432,7 +431,7 @@ mod _functools {
combined_args.extend(new_args_iter.cloned());
// Merge keywords from self.keywords and args.kwargs
let mut final_kwargs = IndexMap::new();
let mut final_kwargs = crate::function::KwArgsMap::default();
// Add keywords from self.keywords
for (key, value) in &*keywords {
+4 -1
View File
@@ -4078,7 +4078,10 @@ mod _io {
vm.new_runtime_error(format!("reentrant call inside {type_name}.__repr__"))
);
};
let Some(data) = zelf.data.lock() else {
// Detach while blocked, like `lock_opt`: another thread can be
// stopped holding this mutex, and blocking on it while attached
// would leave no safepoint for that stop to complete at.
let Some(data) = zelf.data.lock_wrapped(|do_lock| vm.allow_threads(do_lock)) else {
// Reentrant call
return Ok(vm.ctx.new_str(Wtf8Buf::from(format!("<{type_name}>"))));
};
+3 -1
View File
@@ -177,7 +177,9 @@ pub(crate) mod _signal {
module: &Py<crate::builtins::PyModule>,
vm: &VirtualMachine,
) {
if vm.state.config.settings.install_signal_handlers {
// Process-global signal disposition is owned by the main interpreter only.
// Subinterpreters (PEP 734) must not reinstall SIGINT / probe handlers.
if vm.state.is_main_interpreter() && vm.state.config.settings.install_signal_handlers {
let sig_dfl = vm.new_pyobj(SIG_DFL as u8);
let sig_ign = vm.new_pyobj(SIG_IGN as u8);
+32 -50
View File
@@ -796,9 +796,8 @@ pub(crate) mod _thread {
}
#[pyfunction]
fn _is_main_interpreter() -> bool {
// RustPython only has one interpreter
true
fn _is_main_interpreter(vm: &VirtualMachine) -> bool {
vm.state.is_main_interpreter()
}
/// Initialize the main thread ident. Should be called once at interpreter startup.
@@ -1194,8 +1193,8 @@ pub(crate) mod _thread {
{
use core::sync::atomic::Ordering;
let current_ident = get_ident();
vm.state.stop_the_world.stop_the_world(vm);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(vm); }
vm.state.stop_the_world.stop_the_world(&vm.state);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(&vm.state); }
let registry = vm.state.thread_frames.lock();
registry
.iter()
@@ -1218,26 +1217,10 @@ pub(crate) mod _thread {
let iframe_ptr = slot.top_iframe.load(Ordering::Relaxed)
as *const crate::frame::InterpreterFrame;
if !iframe_ptr.is_null() {
// Materialize the entire frame chain and link
// retained_back so f_back works after STW ends.
let mut cur = iframe_ptr;
let mut child_fo: Option<crate::PyRef<crate::frame::FrameObject>> =
None;
while !cur.is_null() {
let iframe = unsafe { &*cur };
let fo = iframe.materialize(vm).to_owned();
if let Some(child) = child_fo.take() {
let mut guard = child.iframe().cold().retained_back.lock();
if guard.is_none() {
*guard = Some(fo.clone());
}
}
child_fo = Some(fo);
cur = iframe.previous();
}
let iframe = unsafe { &*iframe_ptr };
let fo = iframe.materialize(vm);
Some((*id, fo.to_owned()))
// SAFETY: world stopped -> owning thread parked.
let fo = unsafe { iframe.materialize_detached_chain(vm) };
Some((*id, fo))
} else {
None
}
@@ -1250,8 +1233,8 @@ pub(crate) mod _thread {
{
use core::sync::atomic::Ordering;
let current_ident = get_ident();
vm.state.stop_the_world.stop_the_world(vm);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(vm); }
vm.state.stop_the_world.stop_the_world(&vm.state);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(&vm.state); }
let registry = vm.state.thread_frames.lock();
registry
.iter()
@@ -1267,24 +1250,10 @@ pub(crate) mod _thread {
let iframe_ptr = slot.top_iframe.load(Ordering::Relaxed)
as *const crate::frame::InterpreterFrame;
if !iframe_ptr.is_null() {
let mut cur = iframe_ptr;
let mut child_fo: Option<crate::PyRef<crate::frame::FrameObject>> =
None;
while !cur.is_null() {
let iframe = unsafe { &*cur };
let fo = iframe.materialize(vm).to_owned();
if let Some(child) = child_fo.take() {
let mut guard = child.iframe().cold().retained_back.lock();
if guard.is_none() {
*guard = Some(fo.clone());
}
}
child_fo = Some(fo);
cur = iframe.previous();
}
let iframe = unsafe { &*iframe_ptr };
let fo = iframe.materialize(vm);
Some((*id, fo.to_owned()))
// SAFETY: world stopped -> owning thread parked.
let fo = unsafe { iframe.materialize_detached_chain(vm) };
Some((*id, fo))
} else {
// Fall back to frames stack for FrameObject-only path
let frames = slot.frames.lock();
@@ -1386,6 +1355,19 @@ pub(crate) mod _thread {
}
}
/// Take a thread handle's completion mutex, detaching first.
///
/// A joiner holds this mutex across its `allow_threads` wait, so it can
/// still hold it when stop-the-world stops it. An attached thread that
/// blocked on it would never reach a safepoint, so the stop could never
/// complete and the holder would never be resumed to release it.
fn lock_done<'a>(
lock: &'a parking_lot::Mutex<bool>,
vm: &VirtualMachine,
) -> parking_lot::MutexGuard<'a, bool> {
vm.allow_threads(|| lock.lock())
}
/// Reset a parking_lot::Mutex to unlocked state after fork.
#[cfg(all(unix, feature = "host_env"))]
fn reinit_parking_lot_mutex<T: ?Sized>(mutex: &parking_lot::Mutex<T>) {
@@ -1468,7 +1450,7 @@ pub(crate) mod _thread {
// Wait for thread completion using Condvar (supports timeout)
// Loop to handle spurious wakeups
let (lock, cvar) = &**done_event;
let mut done = lock.lock();
let mut done = lock_done(lock, vm);
// ThreadHandle_join semantics: self-join/finalizing checks
// apply only while target thread has not reported it is exiting yet.
@@ -1528,7 +1510,7 @@ pub(crate) mod _thread {
drop(inner_guard);
// Wait on done_event
let (lock, cvar) = &**done_event;
let mut done = lock.lock();
let mut done = lock_done(lock, vm);
while !*done {
vm.allow_threads(|| cvar.wait(&mut done));
}
@@ -1590,7 +1572,7 @@ pub(crate) mod _thread {
remove_from_shutdown_handles(vm, inner, done_event);
let (lock, cvar) = &**done_event;
*lock.lock() = true;
*lock_done(lock, vm) = true;
cvar.notify_all();
Ok(())
}
@@ -1660,7 +1642,7 @@ pub(crate) mod _thread {
// before returning True.
let done = {
let (lock, _) = &*self.done_event;
*lock.lock()
*lock_done(lock, vm)
};
if !done {
return Ok(false);
@@ -1856,7 +1838,7 @@ pub(crate) mod _thread {
// Starting a handle always resets the completion event.
{
let (done_lock, _) = &*handle.done_event;
*done_lock.lock() = false;
*lock_done(done_lock, vm) = false;
}
// Add non-daemon threads to shutdown registry so _shutdown() will wait for them
@@ -1938,7 +1920,7 @@ pub(crate) mod _thread {
// This must be LAST to ensure all cleanup is complete before join() returns
{
let (lock, cvar) = &*done_event_for_cleanup;
*lock.lock() = true;
*lock_done(lock, vm) = true;
cvar.notify_all();
}
}
@@ -1973,7 +1955,7 @@ pub(crate) mod _thread {
}
{
let (done_lock, done_cvar) = &*handle.done_event;
*done_lock.lock() = true;
*lock_done(done_lock, vm) = true;
done_cvar.notify_all();
}
if !daemon {
+15 -6
View File
@@ -8,7 +8,7 @@ mod _winapi {
use crate::{
Py, PyObjectRef, PyPayload, PyResult, TryFromObject, VirtualMachine,
builtins::PyStrRef,
common::lock::PyMutex,
common::lock::{PyMutex, PyMutexGuard},
convert::ToPyException,
function::{ArgMapping, ArgSequence, OptionalArg},
types::Constructor,
@@ -566,9 +566,18 @@ mod _winapi {
.map_err(|e| e.to_pyexception(vm))
}
/// Take `inner`, detaching while blocked.
///
/// `GetOverlappedResult` holds this mutex across its `allow_threads`
/// wait, so a stopped thread can still be holding it. Blocking on it
/// while attached would leave no safepoint for that stop to complete at.
fn lock_inner(&self, vm: &VirtualMachine) -> PyMutexGuard<'_, host_overlapped::Operation> {
vm.allow_threads(|| self.inner.lock())
}
#[pymethod]
fn GetOverlappedResult(&self, wait: bool, vm: &VirtualMachine) -> PyResult<(u32, u32)> {
let mut inner = self.inner.lock();
let mut inner = self.lock_inner(vm);
vm.allow_threads(|| inner.get_result(wait))
.map(|result| (result.transferred, result.error))
.map_err(|e| e.to_pyexception(vm))
@@ -576,7 +585,7 @@ mod _winapi {
#[pymethod]
fn getbuffer(&self, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
let inner = self.inner.lock();
let inner = self.lock_inner(vm);
if !inner.is_completed() {
return Err(vm.new_value_error(
"can't get read buffer before GetOverlappedResult() signals the operation completed",
@@ -589,13 +598,13 @@ mod _winapi {
#[pymethod]
fn cancel(&self, vm: &VirtualMachine) -> PyResult<()> {
let mut inner = self.inner.lock();
let mut inner = self.lock_inner(vm);
inner.cancel().map_err(|e| e.to_pyexception(vm))
}
#[pygetset]
fn event(&self) -> isize {
let inner = self.inner.lock();
fn event(&self, vm: &VirtualMachine) -> isize {
let inner = self.lock_inner(vm);
inner.event() as isize
}
}
+33 -27
View File
@@ -23,20 +23,20 @@ mod gc {
/// Enable automatic garbage collection.
#[pyfunction]
fn enable() {
gc_state::gc_state().enable();
fn enable(vm: &VirtualMachine) {
vm.state.gc.enable();
}
/// Disable automatic garbage collection.
#[pyfunction]
fn disable() {
gc_state::gc_state().disable();
fn disable(vm: &VirtualMachine) {
vm.state.gc.disable();
}
/// Return true if automatic gc is enabled.
#[pyfunction]
fn isenabled() -> bool {
gc_state::gc_state().is_enabled()
fn isenabled(vm: &VirtualMachine) -> bool {
vm.state.gc.is_enabled()
}
/// Run a garbage collection. Returns the number of unreachable objects found.
@@ -58,15 +58,14 @@ mod gc {
invoke_callbacks(vm, "start", generation_num as usize, &Default::default());
// Manual gc.collect() should run even if GC is disabled
let gc = gc_state::gc_state();
let gc = &vm.state.gc;
let result = gc.collect_force(generation_num as usize);
// Move objects from gc_state.garbage to vm.ctx.gc_garbage (for DEBUG_SAVEALL)
// Publish what the collection saved as gc.garbage (for DEBUG_SAVEALL)
{
let mut state_garbage = gc.garbage.lock();
if !state_garbage.is_empty() {
let py_garbage = &vm.ctx.gc_garbage;
let mut garbage_vec = py_garbage.borrow_vec_mut();
let mut garbage_vec = gc.py_garbage.borrow_vec_mut();
for obj in state_garbage.drain(..) {
garbage_vec.push(obj);
}
@@ -82,7 +81,7 @@ mod gc {
/// Return the current collection thresholds as a tuple.
#[pyfunction]
fn get_threshold(vm: &VirtualMachine) -> PyObjectRef {
let (t0, t1, t2) = gc_state::gc_state().get_threshold();
let (t0, t1, t2) = vm.state.gc.get_threshold();
vm.ctx
.new_tuple(vec![
vm.ctx.new_int(t0).into(),
@@ -94,8 +93,13 @@ mod gc {
/// Set the collection thresholds.
#[pyfunction]
fn set_threshold(threshold0: u32, threshold1: OptionalArg<u32>, threshold2: OptionalArg<u32>) {
gc_state::gc_state().set_threshold(
fn set_threshold(
threshold0: u32,
threshold1: OptionalArg<u32>,
threshold2: OptionalArg<u32>,
vm: &VirtualMachine,
) {
vm.state.gc.set_threshold(
threshold0,
threshold1.into_option(),
threshold2.into_option(),
@@ -117,20 +121,22 @@ mod gc {
/// Return the current debugging flags.
#[pyfunction]
fn get_debug() -> u32 {
gc_state::gc_state().get_debug().bits()
fn get_debug(vm: &VirtualMachine) -> u32 {
vm.state.gc.get_debug().bits()
}
/// Set the debugging flags.
#[pyfunction]
fn set_debug(flags: u32) {
gc_state::gc_state().set_debug(gc_state::GcDebugFlags::from_bits_truncate(flags));
fn set_debug(flags: u32, vm: &VirtualMachine) {
vm.state
.gc
.set_debug(gc_state::GcDebugFlags::from_bits_truncate(flags));
}
/// Return a list of per-generation gc stats.
#[pyfunction]
fn get_stats(vm: &VirtualMachine) -> PyResult<PyListRef> {
let stats = gc_state::gc_state().get_stats();
let stats = vm.state.gc.get_stats();
let mut result = Vec::with_capacity(3);
for stat in &stats {
@@ -165,7 +171,7 @@ mod gc {
{
return Err(vm.new_value_error(format!("generation must be in range(0, 3), not {g}")));
}
let objects = gc_state::gc_state().get_objects(generation_opt);
let objects = vm.state.gc.get_objects(generation_opt);
Ok(vm.ctx.new_list(objects))
}
@@ -208,7 +214,7 @@ mod gc {
let mut result = Vec::new();
// Scan all tracked objects across all generations
let all_objects = gc_state::gc_state().get_objects(None);
let all_objects = vm.state.gc.get_objects(None);
for obj in all_objects {
let obj_ptr = obj.as_ref() as *const crate::PyObject as usize;
if stack_frames.contains(&obj_ptr) {
@@ -241,14 +247,14 @@ mod gc {
/// Freeze all objects tracked by gc.
#[pyfunction]
fn freeze() {
gc_state::gc_state().freeze();
fn freeze(vm: &VirtualMachine) {
vm.state.gc.freeze();
}
/// Unfreeze all objects in the permanent generation.
#[pyfunction]
fn unfreeze() {
gc_state::gc_state().unfreeze();
fn unfreeze(vm: &VirtualMachine) {
vm.state.gc.unfreeze();
}
/// Return the number of objects in the permanent generation.
@@ -260,13 +266,13 @@ mod gc {
/// gc.garbage - list of uncollectable objects
#[pyattr]
fn garbage(vm: &VirtualMachine) -> PyListRef {
vm.ctx.gc_garbage.clone()
vm.state.gc.py_garbage.clone()
}
/// gc.callbacks - list of callbacks to be invoked
#[pyattr]
fn callbacks(vm: &VirtualMachine) -> PyListRef {
vm.ctx.gc_callbacks.clone()
vm.state.gc.py_callbacks.clone()
}
/// Helper function to invoke GC callbacks
@@ -276,7 +282,7 @@ mod gc {
generation: usize,
result: &gc_state::CollectResult,
) {
let callbacks_list = &vm.ctx.gc_callbacks;
let callbacks_list = &vm.state.gc.py_callbacks;
let callbacks: Vec<PyObjectRef> = callbacks_list.borrow_vec().to_vec();
if callbacks.is_empty() {
return;
+66 -3
View File
@@ -626,10 +626,17 @@ pub mod module {
crate::stdlib::_imp::acquire_imp_lock_for_fork(vm);
#[cfg(feature = "threading")]
vm.state.stop_the_world.stop_the_world(vm);
vm.state.stop_the_world.stop_the_world(&vm.state);
}
fn py_os_after_fork_child(vm: &VirtualMachine) {
// The interpreter registry is reachable from every thread, so repair it
// before anything enumerates interpreters.
#[cfg(all(unix, feature = "threading"))]
unsafe {
crate::vm::runtime::reinit_after_fork()
};
#[cfg(feature = "threading")]
vm.state.stop_the_world.reset_after_fork();
@@ -639,6 +646,12 @@ pub mod module {
#[cfg(feature = "threading")]
reinit_locks_after_fork(vm);
// The collector stops every interpreter, so interpreters other than the
// forking one must be repaired too; otherwise the child's first
// collection waits for threads that did not survive the fork.
#[cfg(all(unix, feature = "threading"))]
reinit_other_interpreters_after_fork(vm);
// Reinit per-object IO buffer locks on std streams.
// BufferedReader/Writer/TextIOWrapper use PyThreadMutex which can be
// held by dead parent threads, causing deadlocks on any IO in the child.
@@ -719,17 +732,67 @@ pub mod module {
// Codec registry RwLock
vm.state.codec_registry.reinit_after_fork();
// GC state (multiple Mutex + RwLock)
// GC state (multiple Mutex + RwLock), shared lists and this
// interpreter's own policy state.
crate::gc_state::gc_state().reinit_after_fork();
vm.state.gc.reinit_after_fork();
// Import lock (RawReentrantMutex<RawMutex, RawThreadId>)
crate::stdlib::_imp::reinit_imp_lock_after_fork();
}
}
/// Repair every live interpreter other than the forking one after `fork()`.
///
/// Only the forking thread survives, so each other interpreter is left with
/// slots for threads that no longer exist (still ATTACHED if they were
/// running bytecode) and possibly locks or stop-the-world flags held by
/// them. Since a collection stops all interpreters, that state would hang
/// the child's first collection.
///
/// # Safety
/// Must only be called after `fork()` in the child, when no other threads exist.
#[cfg(all(unix, feature = "threading"))]
fn reinit_other_interpreters_after_fork(vm: &VirtualMachine) {
use rustpython_common::lock::reinit_mutex_after_fork;
for state in crate::vm::runtime::live_interpreter_states() {
if state.interpreter_id == vm.state.interpreter_id {
continue;
}
unsafe {
reinit_mutex_after_fork(&state.before_forkers);
reinit_mutex_after_fork(&state.after_forkers_child);
reinit_mutex_after_fork(&state.after_forkers_parent);
reinit_mutex_after_fork(&state.atexit_funcs);
reinit_mutex_after_fork(&state.global_trace_func);
reinit_mutex_after_fork(&state.global_profile_func);
reinit_mutex_after_fork(&state.type_mutex);
reinit_mutex_after_fork(&state.monitoring);
reinit_mutex_after_fork(&state.thread_frames);
reinit_mutex_after_fork(&state.thread_handles);
reinit_mutex_after_fork(&state.shutdown_handles);
state.codec_registry.reinit_after_fork();
state.gc.reinit_after_fork();
}
state.stop_the_world.reset_after_fork();
// Every thread registered here belongs to the parent, including any
// slot the forking thread itself registered before the fork.
state.thread_frames.lock().clear();
state.thread_handles.lock().clear();
state.shutdown_handles.lock().clear();
}
crate::vm::thread::purge_other_interpreter_slots_after_fork(vm.state.interpreter_id);
}
fn py_os_after_fork_parent(vm: &VirtualMachine) {
#[cfg(feature = "threading")]
vm.state.stop_the_world.start_the_world(vm);
vm.state.stop_the_world.start_the_world(&vm.state);
#[cfg(feature = "threading")]
crate::stdlib::_imp::release_imp_lock_after_fork_parent();
+2 -1
View File
@@ -669,7 +669,8 @@ pub mod sys {
"_multiarch" => ctx.new_str(multiarch()),
"version" => PyVersionInfo::from_data(VersionInfoData::IMPLEMENTATION, vm),
"hexversion" => ctx.new_int(version::VERSION_HEX_IMPL),
"supports_isolated_interpreters" => ctx.new_bool(false),
"supports_isolated_interpreters" =>
ctx.new_bool(crate::vm::runtime::SUPPORTS_ISOLATED_INTERPRETERS),
})
}
+1 -1
View File
@@ -529,7 +529,7 @@ fn update_events_mask(vm: &VirtualMachine, state: &MonitoringState) {
// own local events), preventing e.g. INSTRUCTION from being applied to
// unrelated code objects.
// Re-instrument all frames on the current thread's stack, including
// stack-allocated iframes (with_iframe path) that have no FrameObject.
// data stack frames that have no FrameObject.
{
let mut cur = crate::vm::thread::get_current_frame();
while !cur.is_null() {
+8 -8
View File
@@ -54,10 +54,7 @@ pub struct Context {
pub(crate) string_pool: StringPool,
pub(crate) slot_new_wrapper: PyMethodDef,
pub names: ConstName,
// GC module state (callbacks and garbage lists)
pub gc_callbacks: PyListRef,
pub gc_garbage: PyListRef,
}
macro_rules! declare_const_name {
@@ -363,8 +360,6 @@ impl Context {
let empty_bytes = create_object(PyBytes::from(Vec::new()), types.bytes_type);
// GC callbacks and garbage lists
let gc_callbacks = PyRef::new_ref(PyList::default(), types.list_type.to_owned(), None);
let gc_garbage = PyRef::new_ref(PyList::default(), types.list_type.to_owned(), None);
Self {
true_value,
@@ -387,9 +382,6 @@ impl Context {
string_pool,
slot_new_wrapper,
names,
gc_callbacks,
gc_garbage,
}
}
@@ -442,6 +434,14 @@ impl Context {
PyInt::from(i).into_ref(self)
}
/// Borrow a cached small integer whose lifetime is tied to this context.
#[inline(always)]
pub(crate) fn cached_int(&self, i: i32) -> &PyIntRef {
debug_assert!(Self::INT_CACHE_POOL_RANGE.contains(&i));
let inner_idx = (i - Self::INT_CACHE_POOL_MIN) as usize;
&self.int_cache_pool[inner_idx]
}
#[inline]
pub fn new_bigint(&self, i: &BigInt) -> PyIntRef {
if let Some(i) = i.to_i32()
File diff suppressed because it is too large. Load diff
+160 -121
View File
@@ -13,6 +13,7 @@ mod interpreter;
mod method;
#[cfg(feature = "rustpython-compiler")]
mod python_run;
pub mod runtime;
mod setting;
pub mod thread;
mod vm_new;
@@ -61,16 +62,22 @@ use std::{
pub use context::Context;
pub use interpreter::{Interpreter, InterpreterBuilder};
pub(crate) use method::PyMethod;
pub use runtime::{InterpreterInfo, InterpreterWhence, MAIN_INTERPRETER_ID};
pub use setting::{CheckHashPycsMode, Paths, PyConfig, Settings};
pub const MAX_MEMORY_SIZE: usize = isize::MAX as usize;
// Objects are live when they are on stack, or referenced by a name (for now)
/// Top level container of a python virtual machine. In theory you could
/// create more instances of this struct and have them operate fully isolated.
/// Per-thread execution context for a single interpreter (≈ CPython `PyThreadState`).
///
/// To construct this, please refer to the [`Interpreter`]
/// A `VirtualMachine` holds thread-local eval state (exceptions, recursion, frames,
/// datastack) plus shared references to interpreter-owned data (`state`,
/// `builtins`, `sys_module`, `ctx`). Multiple VMs may share the same
/// [`PyGlobalState`] via `VirtualMachine::new_thread`; distinct interpreters
/// each have their own `PyGlobalState` (see [`Interpreter::create_subinterpreter`]).
///
/// To construct the main VM of an interpreter, use [`Interpreter`].
pub struct VirtualMachine {
pub builtins: PyRef<PyModule>,
pub sys_module: PyRef<PyModule>,
@@ -110,11 +117,11 @@ pub struct VirtualMachine {
/// pointer here before returning `ExecutionResult::TailCall`.
/// Access only via `set_pending_tailcall` / `take_pending_tailcall`.
pending_tailcall_frame: Cell<Option<PendingFrame>>,
/// Owned references that keep callee raw pointers valid during TailCall.
/// Set by `tailcall_prepare_frame`, drained by the trampoline into
/// its local `owned_refs` Vec. Uses UnsafeCell because the VM is
/// per-thread and this field is only accessed on the owning thread.
pub(crate) pending_tailcall_refs: core::cell::UnsafeCell<Vec<PyObjectRef>>,
/// Owned reference that keeps callee raw pointers valid during TailCall.
/// Set by the exact-call handlers and moved into the trampoline's
/// `SuspendedFrame`. Uses UnsafeCell because the VM is per-thread and this
/// field is only accessed on the owning thread.
pending_tailcall_owner: core::cell::UnsafeCell<Option<PyObjectRef>>,
}
/// Non-owning frame pointer for the non-unix threading frames stack.
@@ -257,9 +264,9 @@ impl StopTheWorldState {
}
#[inline]
fn init_thread_countdown(&self, vm: &VirtualMachine) -> i64 {
fn init_thread_countdown(&self, state: &PyGlobalState) -> i64 {
let requester = self.requester.load(Ordering::Relaxed);
let registry = vm.state.thread_frames.lock();
let registry = state.thread_frames.lock();
// Keep requested/count initialization serialized with thread-slot
// registration (which also takes this lock), matching the
// HEAD_LOCK-guarded stop-the-world bookkeeping.
@@ -288,10 +295,10 @@ impl StopTheWorldState {
/// Try to CAS detached threads directly to SUSPENDED and check whether
/// stop countdown reached zero after parking detached threads.
fn park_detached_threads(&self, vm: &VirtualMachine) -> bool {
fn park_detached_threads(&self, state: &PyGlobalState) -> bool {
use thread::{THREAD_ATTACHED, THREAD_DETACHED, THREAD_SUSPENDED};
let requester = self.requester.load(Ordering::Relaxed);
let registry = vm.state.thread_frames.lock();
let registry = state.thread_frames.lock();
let mut attached_seen = 0u64;
let mut forced_parks = 0u64;
@@ -413,23 +420,23 @@ impl StopTheWorldState {
/// Takes the shared exclusion first so at most one requester (fork or GC)
/// drives the stop→start span at a time; it is released by
/// `start_the_world`/`reset_after_fork`.
pub fn stop_the_world(&self, vm: &VirtualMachine) {
pub fn stop_the_world(&self, state: &PyGlobalState) {
self.acquire_exclusion();
let start = std::time::Instant::now();
let requester_ident = crate::stdlib::_thread::get_ident();
self.requester.store(requester_ident, Ordering::Relaxed);
self.stats_stop_calls.fetch_add(1, Ordering::Relaxed);
let initial_countdown = self.init_thread_countdown(vm);
let initial_countdown = self.init_thread_countdown(state);
stw_trace(format_args!("stop begin requester={requester_ident}"));
// Park detached threads and set stop bits, then confirm every other
// thread is SUSPENDED. The completion condition is level-triggered
// (`all_non_requester_suspended`) so an already-suspended thread that
// was counted but will not notify again cannot stall the stop.
self.park_detached_threads(vm);
if initial_countdown == 0 || self.all_non_requester_suspended(vm) {
self.park_detached_threads(state);
if initial_countdown == 0 || self.all_non_requester_suspended(state) {
self.world_stopped.store(true, Ordering::Release);
#[cfg(debug_assertions)]
self.debug_assert_all_non_requester_suspended(vm);
self.debug_assert_all_non_requester_suspended(state);
stw_trace(format_args!(
"stop end requester={requester_ident} wait_ns=0 polls=0"
));
@@ -438,8 +445,8 @@ impl StopTheWorldState {
let mut polls = 0u64;
loop {
self.park_detached_threads(vm);
if self.all_non_requester_suspended(vm) {
self.park_detached_threads(state);
if self.all_non_requester_suspended(state) {
break;
}
polls = polls.saturating_add(1);
@@ -447,7 +454,7 @@ impl StopTheWorldState {
// Re-check under the wait mutex first to avoid a lost-wake race:
// a thread may have suspended and notified right before we enter wait.
let guard = self.notify_mutex.lock().unwrap();
if self.all_non_requester_suspended(vm) {
if self.all_non_requester_suspended(state) {
drop(guard);
break;
}
@@ -476,18 +483,18 @@ impl StopTheWorldState {
}
self.world_stopped.store(true, Ordering::Release);
#[cfg(debug_assertions)]
self.debug_assert_all_non_requester_suspended(vm);
self.debug_assert_all_non_requester_suspended(state);
stw_trace(format_args!(
"stop end requester={requester_ident} wait_ns={wait_ns} polls={polls}"
));
}
/// Resume all suspended threads (`start_the_world`).
pub fn start_the_world(&self, vm: &VirtualMachine) {
pub fn start_the_world(&self, state: &PyGlobalState) {
use thread::{THREAD_DETACHED, THREAD_SUSPENDED};
let requester = self.requester.load(Ordering::Relaxed);
stw_trace(format_args!("start begin requester={requester}"));
let registry = vm.state.thread_frames.lock();
let registry = state.thread_frames.lock();
// Clear the request flag BEFORE waking threads. Otherwise a thread
// returning from allow_threads → attach_thread could observe
// `requested == true`, re-suspend itself, and stay parked forever.
@@ -521,7 +528,7 @@ impl StopTheWorldState {
self.thread_countdown.store(0, Ordering::Release);
self.requester.store(0, Ordering::Relaxed);
#[cfg(debug_assertions)]
self.debug_assert_all_non_requester_detached(vm);
self.debug_assert_all_non_requester_detached(state);
// Release the exclusion last, ending the stop→start span so the next
// requester (fork or GC) can proceed.
self.release_exclusion();
@@ -604,10 +611,10 @@ impl StopTheWorldState {
/// lost-decrement race under rapid back-to-back stops: a thread that is
/// already SUSPENDED when a new stop counts it neither notifies nor is
/// force-parked again, so an edge-based countdown could never reach zero.
fn all_non_requester_suspended(&self, vm: &VirtualMachine) -> bool {
fn all_non_requester_suspended(&self, state: &PyGlobalState) -> bool {
use thread::THREAD_SUSPENDED;
let requester = self.requester.load(Ordering::Relaxed);
let registry = vm.state.thread_frames.lock();
let registry = state.thread_frames.lock();
#[expect(
clippy::iter_over_hash_type,
@@ -625,10 +632,10 @@ impl StopTheWorldState {
}
#[cfg(debug_assertions)]
fn debug_assert_all_non_requester_suspended(&self, vm: &VirtualMachine) {
fn debug_assert_all_non_requester_suspended(&self, state: &PyGlobalState) {
use thread::THREAD_SUSPENDED;
let requester = self.requester.load(Ordering::Relaxed);
let registry = vm.state.thread_frames.lock();
let registry = state.thread_frames.lock();
#[expect(
clippy::iter_over_hash_type,
@@ -648,10 +655,10 @@ impl StopTheWorldState {
}
#[cfg(debug_assertions)]
fn debug_assert_all_non_requester_detached(&self, vm: &VirtualMachine) {
fn debug_assert_all_non_requester_detached(&self, state: &PyGlobalState) {
use thread::THREAD_SUSPENDED;
let requester = self.requester.load(Ordering::Relaxed);
let registry = vm.state.thread_frames.lock();
let registry = state.thread_frames.lock();
#[expect(
clippy::iter_over_hash_type,
@@ -732,7 +739,20 @@ pub(crate) struct CallableCache {
pub builtin_any: Option<PyObjectRef>,
}
/// Per-interpreter shared state (≈ CPython `PyInterpreterState`).
///
/// Not process-global: each [`Interpreter`] (main or subinterpreter) owns its own
/// `PyGlobalState`. Process-wide pieces live elsewhere (`Context::genesis`,
/// GC, the interpreter registry in [`runtime`]).
pub struct PyGlobalState {
/// Unique process-global interpreter id (main is [`MAIN_INTERPRETER_ID`]).
pub interpreter_id: i64,
/// How this interpreter was created.
pub whence: runtime::InterpreterWhence,
/// True for every top-level (non-sub) interpreter, each of which keeps its
/// own signal and main-thread bookkeeping. Only the first one registered
/// becomes *the* process main — see [`runtime::main_interpreter_id`].
pub is_main: bool,
pub config: PyConfig,
pub module_defs: BTreeMap<&'static str, &'static builtins::PyModuleDef>,
pub frozen: HashMap<&'static str, FrozenModule, rapidhash::quality::RandomState>,
@@ -777,6 +797,16 @@ pub struct PyGlobalState {
/// Stop-the-world state for pre-fork thread suspension
#[cfg(feature = "threading")]
pub stop_the_world: StopTheWorldState,
/// This interpreter's garbage collector policy and results.
pub gc: crate::gc_state::GcInterpreterState,
}
impl PyGlobalState {
#[inline]
#[must_use]
pub fn is_main_interpreter(&self) -> bool {
self.is_main
}
}
pub fn process_hash_secret_seed() -> u32 {
@@ -828,11 +858,12 @@ pub(crate) struct IframeEntryState {
struct SuspendedFrame {
iframe: *mut crate::frame::InterpreterFrame,
entry_state: IframeEntryState,
/// Owned references that keep callee's raw pointers (code, globals,
/// builtins borrowed from PyFunction) valid. Drained from
/// `vm.pending_tailcall_refs` when the callee's TailCall is consumed.
/// Dropped when this SuspendedFrame is popped (after callee returns/errors).
owned_refs: Vec<PyObjectRef>,
/// Function that owns the callee's raw pointers (code, globals, builtins,
/// closure, and func_obj). Moved from `vm.pending_tailcall_owner` when the
/// callee's TailCall is consumed.
/// Dropped as soon as this SuspendedFrame is popped — the callee has
/// returned or raised and its frame is already released by then.
callee_owner: PyObjectRef,
/// True for the initial frame passed into the trampoline by the caller.
/// The caller owns the datastack allocation for the entry frame, so the
/// trampoline must NOT release it — only callee-allocated frames are
@@ -865,6 +896,13 @@ impl VirtualMachine {
unsafe { (*self.datastack.get()).push(size) }
}
/// Bump-allocate a full frame, returning whether the same cleared LIFO
/// block and size were reused.
#[inline(always)]
pub(crate) fn datastack_push_frame(&self, size: usize) -> (*mut u8, bool) {
unsafe { (*self.datastack.get()).push_frame(size) }
}
/// Check whether the thread data stack currently has room for `size` bytes.
#[inline(always)]
pub(crate) fn datastack_has_space(&self, size: usize) -> bool {
@@ -881,6 +919,12 @@ impl VirtualMachine {
unsafe { (*self.datastack.get()).pop(base) }
}
/// Pop a full frame after its localsplus slots have been cleared.
#[inline(always)]
pub(crate) unsafe fn datastack_pop_frame(&self, base: *mut u8, size: usize) {
unsafe { (*self.datastack.get()).pop_frame(base, size) }
}
/// Temporarily detach the current thread (ATTACHED → DETACHED) while
/// running `f`, then re-attach afterwards. Allows `stop_the_world` to
/// park this thread during blocking syscalls.
@@ -955,7 +999,7 @@ impl VirtualMachine {
callable_cache: CallableCache::default(),
audit_hooks: RefCell::new(vec![]),
pending_tailcall_frame: Cell::new(None),
pending_tailcall_refs: core::cell::UnsafeCell::new(Vec::with_capacity(2)),
pending_tailcall_owner: core::cell::UnsafeCell::new(None),
};
if vm.state.hash_secret.hash_str("")
@@ -1083,9 +1127,12 @@ impl VirtualMachine {
assert!(!self.initialized, "Double Initialize Error");
// Initialize main thread ident before any threading operations
// Process main-thread identity is owned by the main interpreter only
// (used for signal handling / `_thread._is_main_interpreter` helpers).
#[cfg(feature = "threading")]
stdlib::_thread::init_main_thread_ident(self);
if self.state.is_main_interpreter() {
stdlib::_thread::init_main_thread_ident(self);
}
stdlib::builtins::init_module(self, &self.builtins);
let callable_cache_init = self.init_callable_cache();
@@ -1406,6 +1453,22 @@ impl VirtualMachine {
.set(Some(PendingFrame(core::ptr::NonNull::from(iframe))));
}
/// Store the function that owns the fields borrowed by the pending callee.
#[inline(always)]
pub(crate) fn set_pending_tailcall_owner(&self, owner: PyObjectRef) {
let slot = unsafe { &mut *self.pending_tailcall_owner.get() };
debug_assert!(slot.is_none(), "pending TailCall owner was not consumed");
*slot = Some(owner);
}
/// Take the pending callee owner, resetting the side channel.
#[inline(always)]
fn take_pending_tailcall_owner(&self) -> PyObjectRef {
unsafe { &mut *self.pending_tailcall_owner.get() }
.take()
.expect("TailCall without pending owner")
}
/// Take the pending tailcall frame pointer, resetting the side channel.
#[inline(always)]
fn take_pending_tailcall(&self) -> *mut crate::frame::InterpreterFrame {
@@ -1466,18 +1529,11 @@ impl VirtualMachine {
}
let initial_ptr = self.take_pending_tailcall();
// Drain the refs that keep the initial callee's raw pointers alive.
#[allow(
clippy::drain_collect,
reason = "`pending_tailcall_refs`'s allocation is intentionally reused"
)]
let initial_refs = unsafe { &mut *self.pending_tailcall_refs.get() }
.drain(..)
.collect();
let initial_owner = self.take_pending_tailcall_owner();
frame_stack.push(SuspendedFrame {
iframe: iframe as *mut crate::frame::InterpreterFrame,
entry_state,
owned_refs: initial_refs,
callee_owner: initial_owner,
is_entry: true,
});
let mut action = Action::EnterCallee(initial_ptr);
@@ -1490,8 +1546,8 @@ impl VirtualMachine {
Ok(state) => state,
Err(exc) => {
unsafe {
if let Some(base) = callee.release_datastack_frame() {
self.datastack_pop(base);
if let Some((base, size)) = callee.release_datastack_frame() {
self.datastack_pop_frame(base, size);
}
}
action = Action::Unwind(exc);
@@ -1502,17 +1558,11 @@ impl VirtualMachine {
let result = crate::frame::run_iframe(callee, self);
match result {
Ok(ExecutionResult::TailCall) => {
#[allow(
clippy::drain_collect,
reason = "`pending_tailcall_refs`'s allocation is intentionally reused"
)]
let refs = unsafe { &mut *self.pending_tailcall_refs.get() }
.drain(..)
.collect();
let callee_owner = self.take_pending_tailcall_owner();
frame_stack.push(SuspendedFrame {
iframe: callee_ptr,
entry_state: callee_entry,
owned_refs: refs,
callee_owner,
is_entry: false,
});
action = Action::EnterCallee(self.take_pending_tailcall());
@@ -1520,8 +1570,8 @@ impl VirtualMachine {
Ok(ExecutionResult::Return(value)) => {
self.exit_iframe(callee_entry);
unsafe {
if let Some(base) = callee.release_datastack_frame() {
self.datastack_pop(base);
if let Some((base, size)) = callee.release_datastack_frame() {
self.datastack_pop_frame(base, size);
}
}
action = Action::ReturnValue(value);
@@ -1530,8 +1580,8 @@ impl VirtualMachine {
Err(exc) => {
self.exit_iframe(callee_entry);
unsafe {
if let Some(base) = callee.release_datastack_frame() {
self.datastack_pop(base);
if let Some((base, size)) = callee.release_datastack_frame() {
self.datastack_pop_frame(base, size);
}
}
action = Action::Unwind(exc);
@@ -1547,38 +1597,38 @@ impl VirtualMachine {
let SuspendedFrame {
iframe: caller_iframe_ptr,
entry_state: caller_entry,
owned_refs: _caller_refs,
callee_owner,
is_entry: caller_is_entry,
} = caller;
// The callee's frame was released before this action was
// formed, and a materialized frame object holds its own
// references, so nothing borrows the callee's function any
// more. Release it here, at the callee's return, rather than
// holding it across the caller's next stretch of bytecode.
drop(callee_owner);
let caller_iframe = unsafe { &mut *caller_iframe_ptr };
caller_iframe.localsplus.push_stack(value);
let result = crate::frame::run_iframe(caller_iframe, self);
match result {
Ok(ExecutionResult::TailCall) => {
#[allow(
clippy::drain_collect,
reason = "`pending_tailcall_refs`'s allocation is intentionally reused"
)]
let refs = unsafe { &mut *self.pending_tailcall_refs.get() }
.drain(..)
.collect();
drop(_caller_refs);
let next_callee_owner = self.take_pending_tailcall_owner();
frame_stack.push(SuspendedFrame {
iframe: caller_iframe_ptr,
entry_state: caller_entry,
owned_refs: refs,
callee_owner: next_callee_owner,
is_entry: caller_is_entry,
});
action = Action::EnterCallee(self.take_pending_tailcall());
}
Ok(ExecutionResult::Return(value)) => {
drop(_caller_refs);
self.exit_iframe(caller_entry);
if !caller_is_entry {
unsafe {
if let Some(base) = caller_iframe.release_datastack_frame() {
self.datastack_pop(base);
if let Some((base, size)) =
caller_iframe.release_datastack_frame()
{
self.datastack_pop_frame(base, size);
}
}
}
@@ -1586,12 +1636,13 @@ impl VirtualMachine {
}
Ok(ExecutionResult::Yield(_)) => panic!("Yield in non-generator frame"),
Err(exc) => {
drop(_caller_refs);
self.exit_iframe(caller_entry);
if !caller_is_entry {
unsafe {
if let Some(base) = caller_iframe.release_datastack_frame() {
self.datastack_pop(base);
if let Some((base, size)) =
caller_iframe.release_datastack_frame()
{
self.datastack_pop_frame(base, size);
}
}
}
@@ -1607,9 +1658,13 @@ impl VirtualMachine {
let SuspendedFrame {
iframe: caller_iframe_ptr,
entry_state: caller_entry,
owned_refs: _caller_refs,
callee_owner,
is_entry: caller_is_entry,
} = caller;
// Released at the callee's return, for the same reason as
// in `ReturnValue`: the exception carries owned references
// through its traceback, not borrows into the callee frame.
drop(callee_owner);
let caller_iframe = unsafe { &mut *caller_iframe_ptr };
let handled =
@@ -1621,31 +1676,23 @@ impl VirtualMachine {
let result = crate::frame::run_iframe(caller_iframe, self);
match result {
Ok(ExecutionResult::TailCall) => {
#[allow(
clippy::drain_collect,
reason = "`pending_tailcall_refs`'s allocation is intentionally reused"
)]
let refs = unsafe { &mut *self.pending_tailcall_refs.get() }
.drain(..)
.collect();
drop(_caller_refs);
let next_callee_owner = self.take_pending_tailcall_owner();
frame_stack.push(SuspendedFrame {
iframe: caller_iframe_ptr,
entry_state: caller_entry,
owned_refs: refs,
callee_owner: next_callee_owner,
is_entry: caller_is_entry,
});
action = Action::EnterCallee(self.take_pending_tailcall());
}
Ok(ExecutionResult::Return(value)) => {
drop(_caller_refs);
self.exit_iframe(caller_entry);
if !caller_is_entry {
unsafe {
if let Some(base) =
if let Some((base, size)) =
caller_iframe.release_datastack_frame()
{
self.datastack_pop(base);
self.datastack_pop_frame(base, size);
}
}
}
@@ -1655,14 +1702,13 @@ impl VirtualMachine {
panic!("Yield in non-generator frame")
}
Err(new_exc) => {
drop(_caller_refs);
self.exit_iframe(caller_entry);
if !caller_is_entry {
unsafe {
if let Some(base) =
if let Some((base, size)) =
caller_iframe.release_datastack_frame()
{
self.datastack_pop(base);
self.datastack_pop_frame(base, size);
}
}
}
@@ -1671,12 +1717,13 @@ impl VirtualMachine {
}
}
Ok(Some(ExecutionResult::Return(value))) => {
drop(_caller_refs);
self.exit_iframe(caller_entry);
if !caller_is_entry {
unsafe {
if let Some(base) = caller_iframe.release_datastack_frame() {
self.datastack_pop(base);
if let Some((base, size)) =
caller_iframe.release_datastack_frame()
{
self.datastack_pop_frame(base, size);
}
}
}
@@ -1686,12 +1733,13 @@ impl VirtualMachine {
panic!("Unexpected execution result in trampoline unwind")
}
Err(new_exc) => {
drop(_caller_refs);
self.exit_iframe(caller_entry);
if !caller_is_entry {
unsafe {
if let Some(base) = caller_iframe.release_datastack_frame() {
self.datastack_pop(base);
if let Some((base, size)) =
caller_iframe.release_datastack_frame()
{
self.datastack_pop_frame(base, size);
}
}
}
@@ -1783,14 +1831,14 @@ impl VirtualMachine {
// Phase 4: GC collect — modules removed from sys.modules are freed,
// exposing cycles (e.g., dict ↔ function.__globals__). GC collects
// these and calls __del__ while module dicts are still intact.
crate::gc_state::gc_state().collect_force(2);
self.state.gc.collect_force(2);
// Phase 5: Clear module dicts in reverse import order using 2-pass algorithm.
// Skip builtins and sys — those are cleared last.
self.finalize_clear_module_dicts(&module_weakrefs);
// Phase 6: GC collect — pick up anything freed by dict clearing.
crate::gc_state::gc_state().collect_force(2);
self.state.gc.collect_force(2);
// Phase 7: Clear sys and builtins dicts last
self.finalize_clear_sys_builtins_dict();
@@ -2148,7 +2196,7 @@ impl VirtualMachine {
self.restore_exception(saved_exc);
}
// Clear previous before popping — it may point to a stack-allocated
// iframe that will be freed when the caller's with_iframe exits.
// iframe that will be freed when the caller releases its frame.
{
#[allow(unused_imports)]
use rustpython_common::atomic::Radium;
@@ -2261,6 +2309,9 @@ impl VirtualMachine {
core::sync::atomic::Ordering::Relaxed,
);
}
// The slots above are the last write this thread makes into
// the frame object, so it is now readable from anywhere.
fo.iframe().detach();
if !old_chain.is_null() {
let prev_iframe = unsafe { &*old_chain };
let back_fo = prev_iframe.materialize_chain(self);
@@ -2277,7 +2328,7 @@ impl VirtualMachine {
self.restore_exception(saved_exc);
}
// Clear previous before popping — it may point to a stack-allocated
// iframe that will be freed when the caller's with_iframe exits.
// iframe that will be freed when the caller releases its frame.
{
#[allow(unused_imports)]
use rustpython_common::atomic::Radium;
@@ -2300,8 +2351,10 @@ impl VirtualMachine {
if mat_ptr != 0 {
let fo = unsafe { &*(mat_ptr as *const crate::Py<crate::frame::FrameObject>) };
unsafe {
crate::gc_state::gc_state()
.track_object(core::ptr::NonNull::from(fo.as_object()));
crate::gc_state::gc_state().track_object(
core::ptr::NonNull::from(fo.as_object()),
crate::gc_state::current_owner(),
);
let live_iframe = &*iframe_ptr;
live_iframe.cold().temporary_refs.lock().clear();
}
@@ -2309,20 +2362,6 @@ impl VirtualMachine {
}
}
pub fn with_iframe<R>(
&self,
iframe: &mut crate::frame::InterpreterFrame,
f: impl FnOnce(&mut crate::frame::InterpreterFrame) -> PyResult<R>,
) -> PyResult<R> {
let state = self.enter_iframe(iframe)?;
// Ensure exit_iframe runs even if f(iframe) panics.
let guard = scopeguard::guard(state, |s| self.exit_iframe(s));
let result = f(iframe);
let state = scopeguard::ScopeGuard::into_inner(guard);
self.exit_iframe(state);
result
}
/// FrameObject execution for generator/coroutine resume.
/// Pushes a new exc_info slot (gi_exc_state) onto the chain,
/// linking the generator's saved handled-exception.
@@ -2364,7 +2403,7 @@ impl VirtualMachine {
frame.iframe().owner.store(old_owner, core::sync::atomic::Ordering::Release);
self.pop_exception();
// Clear previous before popping — it may point to a stack-allocated
// iframe that will be freed when the caller's with_iframe exits.
// iframe that will be freed when the caller releases its frame.
{
#[allow(unused_imports)]
use rustpython_common::atomic::Radium;
@@ -2780,7 +2819,7 @@ impl VirtualMachine {
#[cfg(feature = "threading")]
pub(crate) fn run_scheduled_gc(&self) {
if crate::signal::take_gc_scheduled() {
crate::gc_state::gc_state().collect(0);
self.state.gc.collect(0);
}
}
+326
View File
@@ -0,0 +1,326 @@
//! Process-global runtime support for multiple interpreters (PEP 734 preparation).
//!
//! CPython maps roughly as:
//! - this module ≈ `_PyRuntimeState.interpreters` + ID allocation
//! - [`crate::vm::PyGlobalState`] ≈ `PyInterpreterState`
//! - [`crate::VirtualMachine`] ≈ `PyThreadState` (plus shared refs to interpreter state)
//!
//! Multiple [`crate::Interpreter`] instances can coexist in one process. Each owns
//! an isolated `PyGlobalState` (modules, codecs, thread registry, stop-the-world, …)
//! while sharing the process-wide [`crate::Context`] (builtin types / immortals).
use crate::common::rc::PyRc;
use crate::vm::PyGlobalState;
use core::sync::atomic::{AtomicI64, Ordering};
use parking_lot::Mutex;
use std::collections::HashMap;
/// Where an interpreter state came from (mirrors CPython `_PyInterpreterState_GetWhence`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum InterpreterWhence {
/// Unknown / not recorded.
Unknown = 0,
/// Created as the process main interpreter at runtime init.
Runtime = 1,
/// Legacy C-API creation path (reserved for C-API parity).
LegacyCapi = 2,
/// Modern C-API creation path (reserved for C-API parity).
Capi = 3,
/// Cross-interpreter C-API (reserved).
Xi = 4,
/// Created via the stdlib / Rust subinterpreter API (PEP 734).
Stdlib = 5,
}
impl InterpreterWhence {
#[must_use]
pub const fn as_i32(self) -> i32 {
self as i32
}
}
/// Snapshot of a registered interpreter for enumeration APIs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InterpreterInfo {
pub id: i64,
pub whence: InterpreterWhence,
}
struct RegistryEntry {
whence: InterpreterWhence,
/// Weak handle so the registry does not keep interpreters alive.
/// Type matches `PyRc` (Arc when threading, Rc otherwise).
#[cfg(feature = "threading")]
state: alloc::sync::Weak<PyGlobalState>,
#[cfg(not(feature = "threading"))]
state: alloc::rc::Weak<PyGlobalState>,
}
/// `main_id` value before any main interpreter has been registered.
const NO_MAIN_INTERPRETER: i64 = -1;
struct InterpreterRegistry {
next_id: AtomicI64,
/// Id of the first registered `is_main` interpreter (PEP 734 `get_main()`),
/// or [`NO_MAIN_INTERPRETER`].
main_id: AtomicI64,
/// id → entry. Main interpreter is always id 0 when created first.
entries: Mutex<HashMap<i64, RegistryEntry>>,
}
impl InterpreterRegistry {
fn new() -> Self {
Self {
// Monotonic ids starting at 0. Concurrent Interpreter construction
// (e.g. cargo test threads) must never share an id.
next_id: AtomicI64::new(0),
main_id: AtomicI64::new(NO_MAIN_INTERPRETER),
entries: Mutex::new(HashMap::new()),
}
}
}
/// The interpreter registry.
///
/// With `threading` this is one process-global table. Without it, `PyRc` is
/// `Rc` and each OS thread owns an independent `Context::genesis()` and
/// `GcState`, so the registry is thread-local for the same reason `gc_state()`
/// is: an `Rc` handle must never be reachable from another thread.
/// `static_cell!` provides exactly that split.
fn registry() -> &'static InterpreterRegistry {
rustpython_common::static_cell! {
static REGISTRY: InterpreterRegistry;
}
REGISTRY.get_or_init(InterpreterRegistry::new)
}
/// Conventional id of the first process main interpreter when allocation is
/// sequential (CPython parity). Concurrent construction may assign other ids;
/// use [`PyGlobalState::is_main`] / [`crate::Interpreter::is_main`] to identify
/// a main interpreter, not this constant alone.
pub const MAIN_INTERPRETER_ID: i64 = 0;
/// Backs `sys.implementation.supports_isolated_interpreters`.
///
/// The Rust substrate already isolates interpreters (`PyGlobalState` per
/// interpreter, per-interpreter thread slots / stop-the-world). This stays
/// `false` until the Python-facing `_interpreters` module is wired up; flip it
/// in the commit that lands `_interpreters`.
pub const SUPPORTS_ISOLATED_INTERPRETERS: bool = false;
/// Id of the main interpreter (PEP 734 `get_main()`), or `None` before any
/// interpreter has been created.
///
/// This is distinct from [`PyGlobalState::is_main`]: every top-level (non-sub)
/// interpreter carries `is_main` for its own signal / main-thread bookkeeping,
/// but only the first one registered becomes *the* main.
#[must_use]
pub fn main_interpreter_id() -> Option<i64> {
match registry().main_id.load(Ordering::Acquire) {
NO_MAIN_INTERPRETER => None,
id => Some(id),
}
}
/// Allocate a unique interpreter id.
///
/// Ids are strictly monotonic and never reused for the lifetime of the
/// registry, so concurrent `Interpreter` construction (parallel unit tests,
/// multi-threaded embedding) never shares an id. Without `threading` the
/// registry — like `Context::genesis()` and the GC state — is per OS thread, so
/// ids are unique within a thread rather than across the process.
pub(crate) fn alloc_interpreter_id() -> i64 {
registry().next_id.fetch_add(1, Ordering::Relaxed)
}
/// Gate between registering an interpreter and a collection's stop-the-world.
///
/// A collection snapshots the registry, stops every interpreter in the
/// snapshot, and then reads tracked objects with those threads parked. An
/// interpreter that registered after the snapshot was taken would not be in it,
/// so nothing would stop it, and its bootstrap — which runs Python and mutates
/// the shared generation lists — would run underneath that scan. Registration
/// therefore waits for an in-flight stop to end; the next collection's snapshot
/// then contains the new interpreter.
fn admission() -> &'static Mutex<()> {
static ADMISSION: std::sync::OnceLock<Mutex<()>> = std::sync::OnceLock::new();
ADMISSION.get_or_init(|| Mutex::new(()))
}
/// Take the admission gate for the duration of a stop-the-world.
#[cfg(feature = "threading")]
pub(crate) fn lock_admission_for_stop() -> parking_lot::MutexGuard<'static, ()> {
admission().lock()
}
/// Add the registry entry, behind the admission gate.
///
/// Only ever called with this thread detached, because the gate is held across
/// a stop-the-world: an attached thread waiting here, or re-attaching while
/// holding the gate, would leave that stop no safepoint to complete at. Nothing
/// under the gate blocks or allocates a tracked object, so this cannot re-enter
/// the collection it waits for.
fn insert_registry_entry(state: &PyRc<PyGlobalState>) {
let _admission = admission().lock();
let mut entries = registry().entries.lock();
// Entries are weak and an interpreter's lifetime is decided by its last
// `PyRc<PyGlobalState>` — which outlives the `Interpreter` handle whenever
// `new_thread()` workers are still running — so nothing removes them at a
// fixed point. Reap the dead ones here to bound the table instead.
entries.retain(|_, entry| entry.state.strong_count() > 0);
entries.insert(
state.interpreter_id,
RegistryEntry {
whence: state.whence,
state: PyRc::downgrade(state),
},
);
}
/// Register an interpreter state in the registry.
pub(crate) fn register_interpreter(state: &PyRc<PyGlobalState>) {
let id = state.interpreter_id;
if state.is_main {
// First `is_main` interpreter defines the main for `get_main()`.
// Additional top-level Interpreters (embedding) keep their own `is_main`
// flag but do not displace the recorded main.
let _ = registry().main_id.compare_exchange(
NO_MAIN_INTERPRETER,
id,
Ordering::AcqRel,
Ordering::Relaxed,
);
}
// A subinterpreter is registered by a thread that is running its parent, so
// detach for the whole insert rather than only for the wait.
let detached = crate::vm::thread::try_with_current_vm(|vm| {
vm.allow_threads(|| insert_registry_entry(state))
});
if detached.is_none() {
insert_registry_entry(state);
}
}
/// Look up a live interpreter state by id.
#[must_use]
pub fn lookup_interpreter(id: i64) -> Option<PyRc<PyGlobalState>> {
let entries = registry().entries.lock();
entries.get(&id).and_then(|e| e.state.upgrade())
}
/// List all currently registered (still-alive) interpreters.
#[must_use]
pub fn list_interpreters() -> Vec<InterpreterInfo> {
let entries = registry().entries.lock();
let mut out: Vec<InterpreterInfo> = entries
.iter()
.filter_map(|(&id, entry)| {
// Drop dead weak refs from the listing.
if entry.state.strong_count() == 0 {
return None;
}
Some(InterpreterInfo {
id,
whence: entry.whence,
})
})
.collect();
out.sort_by_key(|info| info.id);
out
}
/// Number of registered interpreters that are still alive.
#[must_use]
pub fn interpreter_count() -> usize {
list_interpreters().len()
}
/// Reset the registry's locks after `fork()`.
///
/// The tables are reachable from every thread, so a thread that died in the
/// fork may have left one locked; the child would then deadlock the first time
/// it enumerates interpreters (which the collector now does on every stop).
///
/// # Safety
/// Must only be called after `fork()` in the child process, when no other
/// threads exist and the calling thread holds none of these locks.
#[cfg(all(unix, feature = "threading"))]
pub unsafe fn reinit_after_fork() {
unsafe {
crate::common::lock::reinit_mutex_after_fork(&registry().entries);
crate::common::lock::reinit_mutex_after_fork(owned_interpreters());
crate::common::lock::reinit_mutex_after_fork(admission());
}
}
/// All live interpreter states, ordered by id.
///
/// Used by the cyclic collector, which must stop every interpreter's threads
/// (not just the collecting one) because GC-tracked objects from all
/// interpreters share one object graph. Ordering is deterministic so that
/// multiple stop-the-world requesters always take exclusions in the same order.
#[must_use]
pub fn live_interpreter_states() -> Vec<PyRc<PyGlobalState>> {
let entries = registry().entries.lock();
let mut states: Vec<(i64, PyRc<PyGlobalState>)> = entries
.iter()
.filter_map(|(&id, entry)| entry.state.upgrade().map(|state| (id, state)))
.collect();
drop(entries);
states.sort_by_key(|(id, _)| *id);
states.into_iter().map(|(_, state)| state).collect()
}
/// Runtime-owned interpreters (the ownership anchor for the Python
/// `_interpreters` API).
///
/// A Rust [`crate::Interpreter`] handle is normally owned by its Rust caller.
/// For PEP 734, `_interpreters.create()` returns only an id and the runtime
/// must keep the interpreter alive until `_interpreters.destroy(id)`. These
/// functions hold that ownership, keyed by interpreter id, while the weak
/// [`registry`] above still drives enumeration and lookup.
///
/// Only available with the `threading` feature: a runtime-owned interpreter is
/// reachable from other OS threads, which requires `Interpreter: Send` (true
/// only when `PyObjectRef` is `Arc`-backed).
#[cfg(feature = "threading")]
fn owned_interpreters() -> &'static Mutex<HashMap<i64, crate::Interpreter>> {
use std::sync::OnceLock;
static OWNED: OnceLock<Mutex<HashMap<i64, crate::Interpreter>>> = OnceLock::new();
OWNED.get_or_init(|| Mutex::new(HashMap::new()))
}
/// Transfer ownership of `interp` to the runtime, returning its id.
#[cfg(feature = "threading")]
pub fn store_owned_interpreter(interp: crate::Interpreter) -> i64 {
let id = interp.id();
// Ids are strictly monotonic, so this never displaces (and drops) an
// existing entry under the lock.
owned_interpreters().lock().insert(id, interp);
id
}
/// Reclaim a runtime-owned interpreter, removing it from the owner table.
///
/// The returned handle is dropped by the caller *outside* the owner lock; its
/// `Drop` unregisters the interpreter from the weak [`registry`].
#[cfg(feature = "threading")]
#[must_use]
pub fn take_owned_interpreter(id: i64) -> Option<crate::Interpreter> {
owned_interpreters().lock().remove(&id)
}
/// Whether `id` refers to a runtime-owned interpreter.
#[cfg(feature = "threading")]
#[must_use]
pub fn is_owned_interpreter(id: i64) -> bool {
owned_interpreters().lock().contains_key(&id)
}
/// Number of runtime-owned interpreters currently alive.
#[cfg(feature = "threading")]
#[must_use]
pub fn owned_interpreter_count() -> usize {
owned_interpreters().lock().len()
}
+2
View File
@@ -25,6 +25,7 @@ pub struct Paths {
/// Combined configuration: user settings + computed paths
/// CPython directly exposes every fields under both of them.
/// We separate them to maintain better ownership discipline.
#[derive(Clone)]
pub struct PyConfig {
pub settings: Settings,
pub paths: Paths,
@@ -39,6 +40,7 @@ impl PyConfig {
/// User-configurable settings for the python vm.
#[non_exhaustive]
#[derive(Clone)]
pub struct Settings {
/// -I
pub isolated: bool,
+261 -113
View File
@@ -8,11 +8,6 @@ use alloc::sync::Arc;
#[cfg(all(unix, feature = "threading"))]
use crate::frame::FrameObject;
use crate::frame::InterpreterFrame;
#[cfg(all(unix, feature = "threading"))]
use crate::{AsObject, Py, PyObject, VirtualMachine};
#[cfg(all(not(unix), feature = "threading"))]
use crate::{AsObject, PyObject, VirtualMachine};
#[cfg(not(feature = "threading"))]
use crate::{AsObject, PyObject, VirtualMachine};
#[cfg(all(unix, feature = "threading"))]
use core::sync::atomic::AtomicPtr;
@@ -22,6 +17,8 @@ use core::{
sync::atomic::{AtomicUsize, Ordering},
};
use itertools::Itertools;
#[cfg(feature = "threading")]
use std::collections::HashMap;
use std::thread_local;
// Thread states for stop-the-world support.
@@ -90,7 +87,17 @@ thread_local! {
pub(crate) static COROUTINE_ORIGIN_TRACKING_DEPTH: Cell<u32> = const { Cell::new(0) };
/// Current thread's slot for sys._current_frames() and sys._current_exceptions()
/// Per-interpreter thread slots for this OS thread (PEP 734 multi-interpreter).
///
/// CPython keeps a `PyThreadState` per (thread, interpreter) pair. RustPython
/// mirrors that: each interpreter's `PyGlobalState.thread_frames` gets its own
/// [`ThreadSlot`] for this OS thread. `CURRENT_THREAD_SLOT` always points at
/// the slot for the currently entered interpreter.
#[cfg(feature = "threading")]
static INTERP_THREAD_SLOTS: RefCell<HashMap<i64, CurrentFrameSlot>> =
RefCell::new(HashMap::new());
/// Current thread's slot for the currently entered interpreter.
#[cfg(feature = "threading")]
static CURRENT_THREAD_SLOT: RefCell<Option<CurrentFrameSlot>> = const { RefCell::new(None) };
@@ -110,6 +117,21 @@ thread_local! {
static CURRENT_TOP_FRAME_SLOT: Cell<*const AtomicPtr<FrameObject>> =
const { Cell::new(core::ptr::null()) };
/// Cached pointer to this thread's `ThreadSlot::top_iframe` for the hot
/// light-frame push/pop path. The slot's Arc keeps the pointee alive.
#[cfg(feature = "threading")]
static CURRENT_TOP_IFRAME_SLOT: Cell<*const AtomicUsize> =
const { Cell::new(core::ptr::null()) };
/// Cached pointer to this thread's `ThreadSlot::stop_requested`, for the
/// safepoint the dispatch loop takes once per instruction. Reading it
/// through `CURRENT_THREAD_SLOT` costs a `RefCell` borrow — two stores to
/// thread-local memory — where this costs one relaxed load. The slot's Arc
/// keeps the pointee alive, as with the frame pointers above.
#[cfg(feature = "threading")]
static CURRENT_STOP_REQUESTED: Cell<*const core::sync::atomic::AtomicBool> =
const { Cell::new(core::ptr::null()) };
}
#[must_use]
@@ -131,15 +153,42 @@ pub fn with_current_vm<R>(f: impl FnOnce(&VirtualMachine) -> R) -> R {
}
fn set_current_vm<R>(vm: &VirtualMachine, f: impl FnOnce() -> R) -> R {
// Attach to this VM's interpreter, detaching the enclosing one if this is a
// switch between interpreters on the same OS thread.
#[cfg(feature = "threading")]
let switched = begin_interpreter_section(vm);
VM_STACK.with(|vms| {
vms.borrow_mut().push(vm.into());
scopeguard::defer! {
vms.borrow_mut().pop();
#[cfg(feature = "threading")]
end_interpreter_section(switched);
}
f()
})
}
/// Pointer to the GC state of the interpreter running on this thread.
///
/// The pointee belongs to the `PyGlobalState` of the VM on top of `VM_STACK`,
/// which is borrowed for the whole `set_current_vm` scope — so the pointer stays
/// valid as long as the caller remains inside that scope.
pub(crate) fn current_gc_state() -> Option<NonNull<crate::gc_state::GcInterpreterState>> {
// Reached from every tracked allocation, including ones a thread-local
// destructor makes while the VM stack is being torn down, so neither a
// destroyed key nor an outstanding borrow may panic here.
VM_STACK
.try_with(|vms| {
let vm = vms.try_borrow().ok()?.last().copied()?;
// SAFETY: entries in VM_STACK either borrow a VM for the dynamic
// scope of a set_current_vm()/enter_vm() call or point at GILSTATE_VM.
Some(NonNull::from(&unsafe { vm.as_ref() }.state.gc))
})
.ok()
.flatten()
}
pub fn try_with_current_vm<R>(f: impl FnOnce(&VirtualMachine) -> R) -> Option<R> {
VM_STACK.with(|vms| {
let vm = vms.borrow().last().copied()?;
@@ -150,27 +199,8 @@ pub fn try_with_current_vm<R>(f: impl FnOnce(&VirtualMachine) -> R) -> Option<R>
}
pub fn enter_vm<R>(vm: &VirtualMachine, f: impl FnOnce() -> R) -> R {
// Outermost enter_vm: transition DETACHED → ATTACHED
#[cfg(feature = "threading")]
let was_outermost = !current_vm_is_set();
// Initialize thread slot for this thread if not already done
#[cfg(feature = "threading")]
init_thread_slot_if_needed(vm);
#[cfg(feature = "threading")]
if was_outermost {
attach_thread(vm);
}
scopeguard::defer! {
// Outermost exit: transition ATTACHED → DETACHED
#[cfg(feature = "threading")]
if was_outermost {
detach_thread();
}
}
// Attach/detach is handled by `set_current_vm`, which pairs it with the
// VM_STACK push so that switching interpreters mid-stack stays consistent.
set_current_vm(vm, f)
}
@@ -188,29 +218,19 @@ pub fn enter_vm<R>(vm: &VirtualMachine, f: impl FnOnce() -> R) -> R {
#[must_use]
pub(crate) struct VmBootstrapGuard {
#[cfg(feature = "threading")]
was_outermost: bool,
switched: bool,
}
impl VmBootstrapGuard {
pub(crate) fn new(vm: &VirtualMachine) -> Self {
// Outermost: transition DETACHED → ATTACHED
#[cfg(feature = "threading")]
let was_outermost = !current_vm_is_set();
// Initialize thread slot for this thread if not already done
#[cfg(feature = "threading")]
init_thread_slot_if_needed(vm);
#[cfg(feature = "threading")]
if was_outermost {
attach_thread(vm);
}
let switched = begin_interpreter_section(vm);
VM_STACK.with(|vms| vms.borrow_mut().push(vm.into()));
Self {
#[cfg(feature = "threading")]
was_outermost,
switched,
}
}
}
@@ -221,11 +241,8 @@ impl Drop for VmBootstrapGuard {
vms.borrow_mut().pop();
});
// Outermost exit: transition ATTACHED → DETACHED
#[cfg(feature = "threading")]
if self.was_outermost {
detach_thread();
}
end_interpreter_section(self.switched);
}
}
@@ -287,7 +304,13 @@ pub fn restore_current_thread(state: SavedThreadState) {
// SAFETY: borrowed VMs remain alive for the dynamic save/restore scope,
// while an owned GILState VM was restored above before this dereference.
attach_thread(unsafe { vm.as_ref() });
let vm = unsafe { vm.as_ref() };
// Point CURRENT_THREAD_SLOT at the restored interpreter before attach.
// After subinterpreter bootstrap, CURRENT may still refer to the temporary
// subinterpreter slot (DETACHED); attaching that would leave the parent
// slot detached and later confuse outermost detach.
init_thread_slot_if_needed(vm);
attach_thread(vm);
VM_STACK.with(|vms| *vms.borrow_mut() = vm_stack);
}
@@ -340,43 +363,130 @@ pub fn release_current_thread(state: CurrentVmAttachState) {
detach_thread();
}
/// Initialize thread slot for current thread if not already initialized.
/// Called automatically by enter_vm().
/// Ensure this OS thread has a [`ThreadSlot`] registered with `vm`'s interpreter
/// and make it the current slot.
///
/// Called automatically by `enter_vm()` / `VmBootstrapGuard` whenever a VM
/// becomes current. Switching between interpreters on the same OS thread swaps
/// `CURRENT_THREAD_SLOT` to that interpreter's slot (creating one if needed).
#[cfg(feature = "threading")]
fn init_thread_slot_if_needed(vm: &VirtualMachine) {
CURRENT_THREAD_SLOT.with(|slot| {
if slot.borrow().is_none() {
let thread_id = crate::stdlib::_thread::get_ident();
let mut registry = vm.state.thread_frames.lock();
let new_slot = Arc::new(ThreadSlot {
#[cfg(unix)]
top_frame: AtomicPtr::new(core::ptr::null_mut()),
top_iframe: AtomicUsize::new(0),
#[cfg(not(unix))]
frames: parking_lot::Mutex::new(Vec::new()),
exception: crate::PyAtomicRef::from(None::<PyBaseExceptionRef>),
state: core::sync::atomic::AtomicI32::new(
if vm.state.stop_the_world.requested.load(Ordering::Acquire) {
// Match init_threadstate(): new thread-state starts
// suspended while stop-the-world is active.
THREAD_SUSPENDED
} else {
THREAD_DETACHED
},
),
stop_requested: core::sync::atomic::AtomicBool::new(false),
thread: std::thread::current(),
qsbr: crate::object::qsbr::QSBR.register(),
});
registry.insert(thread_id, new_slot.clone());
drop(registry);
#[cfg(all(unix, feature = "threading"))]
CURRENT_TOP_FRAME_SLOT.with(|c| c.set(&new_slot.top_frame));
*slot.borrow_mut() = Some(new_slot);
let slot = ensure_thread_slot(vm);
set_current_thread_slot(slot);
}
/// Look up (creating if needed) this thread's [`ThreadSlot`] for `vm`'s
/// interpreter, without making it the current slot.
#[cfg(feature = "threading")]
fn ensure_thread_slot(vm: &VirtualMachine) -> CurrentFrameSlot {
let interp_id = vm.state.interpreter_id;
INTERP_THREAD_SLOTS.with(|slots| {
let mut slots = slots.borrow_mut();
if let Some(existing) = slots.get(&interp_id) {
return existing.clone();
}
let thread_id = crate::stdlib::_thread::get_ident();
let mut registry = vm.state.thread_frames.lock();
let new_slot = Arc::new(ThreadSlot {
#[cfg(unix)]
top_frame: AtomicPtr::new(core::ptr::null_mut()),
top_iframe: AtomicUsize::new(0),
#[cfg(not(unix))]
frames: parking_lot::Mutex::new(Vec::new()),
exception: crate::PyAtomicRef::from(None::<PyBaseExceptionRef>),
state: core::sync::atomic::AtomicI32::new(
if vm.state.stop_the_world.requested.load(Ordering::Acquire) {
// Match init_threadstate(): new thread-state starts
// suspended while stop-the-world is active.
THREAD_SUSPENDED
} else {
THREAD_DETACHED
},
),
stop_requested: core::sync::atomic::AtomicBool::new(false),
thread: std::thread::current(),
qsbr: crate::object::qsbr::QSBR.register(),
});
registry.insert(thread_id, new_slot.clone());
drop(registry);
slots.insert(interp_id, new_slot.clone());
new_slot
})
}
/// Make `slot` the current thread slot (and the cached top-frame pointer).
#[cfg(feature = "threading")]
fn set_current_thread_slot(slot: CurrentFrameSlot) {
#[cfg(unix)]
CURRENT_TOP_FRAME_SLOT.with(|c| c.set(&slot.top_frame));
CURRENT_TOP_IFRAME_SLOT.with(|c| c.set(&slot.top_iframe));
CURRENT_STOP_REQUESTED.with(|c| c.set(&slot.stop_requested));
CURRENT_THREAD_SLOT.with(|current| {
*current.borrow_mut() = Some(slot);
});
}
/// Whether the current thread slot is ATTACHED.
#[cfg(feature = "threading")]
fn current_slot_is_attached() -> bool {
CURRENT_THREAD_SLOT.with(|slot| {
slot.borrow()
.as_ref()
.is_some_and(|s| s.state.load(Ordering::Acquire) == THREAD_ATTACHED)
})
}
/// Attach this thread to `vm`'s interpreter for the duration of a section,
/// detaching whichever interpreter it was attached to (≈ `_PyThreadState_Swap`).
///
/// A thread must never be ATTACHED to two interpreters at once: stop-the-world
/// treats an ATTACHED slot as "running this interpreter's bytecode" and a
/// DETACHED slot as parkable without cooperation, so running interpreter B's
/// code while B's slot is DETACHED would let a collector conclude B is stopped
/// while this thread keeps mutating the (process-global) object graph.
///
/// Returns whether the attachment changed, i.e. whether the matching
/// [`end_interpreter_section`] must undo it.
#[cfg(feature = "threading")]
fn begin_interpreter_section(vm: &VirtualMachine) -> bool {
let target = ensure_thread_slot(vm);
let already_current = CURRENT_THREAD_SLOT.with(|slot| {
slot.borrow()
.as_ref()
.is_some_and(|s| Arc::ptr_eq(s, &target))
});
if already_current && current_slot_is_attached() {
// Nested section in the same interpreter: already attached.
return false;
}
if !already_current && current_slot_is_attached() {
detach_thread();
}
set_current_thread_slot(target);
attach_thread(vm);
true
}
/// Undo [`begin_interpreter_section`]: detach this interpreter and re-attach the
/// enclosing one, if any. Call after the VM has been popped from `VM_STACK`.
#[cfg(feature = "threading")]
fn end_interpreter_section(switched: bool) {
if !switched {
return;
}
if current_slot_is_attached() {
detach_thread();
}
// The enclosing section, if any, is the VM now on top of the stack.
if let Some(vm_ptr) = VM_STACK.with(|vms| vms.borrow().last().copied()) {
// SAFETY: entries on VM_STACK are valid for their enter/set_current_vm scope.
let vm = unsafe { vm_ptr.as_ref() };
set_current_thread_slot(ensure_thread_slot(vm));
attach_thread(vm);
}
}
/// Transition DETACHED → ATTACHED. Blocks if the thread was SUSPENDED by
/// a stop-the-world request (like `_PyThreadState_Attach` + `tstate_wait_attach`).
#[cfg(feature = "threading")]
@@ -598,10 +708,12 @@ fn do_suspend(stw: &super::StopTheWorldState) {
#[inline]
#[must_use]
pub fn stop_requested_for_current_thread() -> bool {
CURRENT_THREAD_SLOT.with(|slot| {
slot.borrow()
.as_ref()
.is_some_and(|s| s.stop_requested.load(Ordering::Relaxed))
CURRENT_STOP_REQUESTED.with(|cached| {
let flag = cached.get();
// SAFETY: the pointer is non-null only while `CURRENT_THREAD_SLOT`
// holds the `Arc<ThreadSlot>` that owns the flag; both are cleared
// together in `cleanup_current_thread_frames`.
!flag.is_null() && unsafe { &*flag }.load(Ordering::Relaxed)
})
}
@@ -692,27 +804,28 @@ pub fn set_current_frame(frame: *const InterpreterFrame) -> *const InterpreterFr
// sys._current_frames).
#[cfg(feature = "threading")]
{
CURRENT_THREAD_SLOT.with(|slot| {
if let Some(s) = slot.borrow().as_ref() {
if !frame.is_null() {
#[cfg(unix)]
{
let frame_obj = unsafe { (*frame).frame_obj() };
let fo_ptr = match frame_obj {
Some(py) => {
py as *const Py<FrameObject> as *const FrameObject
as *mut FrameObject
}
None => core::ptr::null_mut(),
};
s.top_frame.store(fo_ptr, Ordering::Relaxed);
}
s.top_iframe.store(frame as usize, Ordering::Relaxed);
CURRENT_TOP_IFRAME_SLOT.with(|slot| {
let slot = slot.get();
if !slot.is_null() {
unsafe { &*slot }.store(frame as usize, Ordering::Relaxed);
}
});
#[cfg(unix)]
CURRENT_TOP_FRAME_SLOT.with(|slot| {
let slot = slot.get();
if !slot.is_null() {
let fo_ptr = if frame.is_null() {
core::ptr::null_mut()
} else {
#[cfg(unix)]
s.top_frame.store(core::ptr::null_mut(), Ordering::Relaxed);
s.top_iframe.store(0, Ordering::Relaxed);
}
let frame_obj = unsafe { (*frame).frame_obj() };
// The payload address, which is what the cross-thread
// reader hands to `Py::from_payload_ptr`. The `Py` address
// would be off by the object header.
frame_obj.map_or(core::ptr::null_mut(), |py| {
core::ptr::from_ref::<FrameObject>(py).cast_mut()
})
};
unsafe { &*slot }.store(fo_ptr, Ordering::Relaxed);
}
});
}
@@ -759,16 +872,21 @@ pub fn get_all_current_exceptions(vm: &VirtualMachine) -> Vec<(u64, Option<PyBas
.collect()
}
/// Cleanup thread slot for the current thread. Called at thread exit.
/// Cleanup thread slot for the current thread in `vm`'s interpreter.
/// Called at thread exit (or when leaving an interpreter permanently).
#[cfg(feature = "threading")]
pub fn cleanup_current_thread_frames(vm: &VirtualMachine) {
let thread_id = crate::stdlib::_thread::get_ident();
let interp_id = vm.state.interpreter_id;
// Prefer the slot registered for this interpreter; fall back to CURRENT.
let slot_for_interp = INTERP_THREAD_SLOTS.with(|slots| slots.borrow_mut().remove(&interp_id));
let current_slot = CURRENT_THREAD_SLOT.with(|slot| slot.borrow().as_ref().cloned());
let slot_to_clean = slot_for_interp.or(current_slot);
// A dying thread should not remain logically ATTACHED while its
// thread-state slot is being removed.
#[cfg(feature = "threading")]
if let Some(slot) = &current_slot {
if let Some(slot) = &slot_to_clean {
let _ = slot.state.compare_exchange(
THREAD_ATTACHED,
THREAD_DETACHED,
@@ -779,7 +897,7 @@ pub fn cleanup_current_thread_frames(vm: &VirtualMachine) {
// Guard against OS thread-id reuse races: only remove the registry entry
// if it still points at this thread's own slot.
let _removed = if let Some(slot) = &current_slot {
let _removed = if let Some(slot) = &slot_to_clean {
let mut registry = vm.state.thread_frames.lock();
match registry.get(&thread_id) {
Some(registered) if Arc::ptr_eq(registered, slot) => registry.remove(&thread_id),
@@ -789,7 +907,6 @@ pub fn cleanup_current_thread_frames(vm: &VirtualMachine) {
None
};
#[cfg(feature = "threading")]
if let Some(slot) = &_removed
&& vm.state.stop_the_world.requested.load(Ordering::Acquire)
&& thread_id != vm.state.stop_the_world.requester_ident()
@@ -799,12 +916,23 @@ pub fn cleanup_current_thread_frames(vm: &VirtualMachine) {
// Unblock requester countdown progress.
vm.state.stop_the_world.notify_thread_gone();
}
// Clear the cached top-frame pointer before dropping the slot Arc so no
// later `set_current_frame` dereferences freed slot memory.
#[cfg(all(unix, feature = "threading"))]
CURRENT_TOP_FRAME_SLOT.with(|c| c.set(core::ptr::null()));
// If CURRENT pointed at the cleaned slot, clear it (and top-frame cache).
CURRENT_THREAD_SLOT.with(|s| {
*s.borrow_mut() = None;
let clear = match (s.borrow().as_ref(), slot_to_clean.as_ref()) {
(Some(cur), Some(cleaned)) => Arc::ptr_eq(cur, cleaned),
(Some(_), None) => false,
(None, _) => false,
};
if clear {
*s.borrow_mut() = None;
#[cfg(all(unix, feature = "threading"))]
CURRENT_TOP_FRAME_SLOT.with(|c| c.set(core::ptr::null()));
#[cfg(feature = "threading")]
CURRENT_TOP_IFRAME_SLOT.with(|c| c.set(core::ptr::null()));
#[cfg(feature = "threading")]
CURRENT_STOP_REQUESTED.with(|c| c.set(core::ptr::null()));
}
});
}
@@ -843,7 +971,7 @@ pub fn reinit_frame_slot_after_fork(vm: &VirtualMachine) {
core::ptr::null_mut()
} else {
match unsafe { (*top_iframe).frame_obj() } {
Some(fo) => fo as *const Py<FrameObject> as *const FrameObject as *mut FrameObject,
Some(fo) => core::ptr::from_ref::<FrameObject>(fo).cast_mut(),
None => core::ptr::null_mut(),
}
}
@@ -865,6 +993,10 @@ pub fn reinit_frame_slot_after_fork(vm: &VirtualMachine) {
});
#[cfg(all(unix, feature = "threading"))]
CURRENT_TOP_FRAME_SLOT.with(|c| c.set(&new_slot.top_frame));
#[cfg(feature = "threading")]
CURRENT_TOP_IFRAME_SLOT.with(|c| c.set(&new_slot.top_iframe));
#[cfg(feature = "threading")]
CURRENT_STOP_REQUESTED.with(|c| c.set(&new_slot.stop_requested));
// Lock is safe: reinit_locks_after_fork() already reset it to unlocked.
let mut registry = vm.state.thread_frames.lock();
@@ -873,7 +1005,23 @@ pub fn reinit_frame_slot_after_fork(vm: &VirtualMachine) {
drop(registry);
CURRENT_THREAD_SLOT.with(|s| {
*s.borrow_mut() = Some(new_slot);
*s.borrow_mut() = Some(new_slot.clone());
});
INTERP_THREAD_SLOTS.with(|slots| {
slots.borrow_mut().insert(vm.state.interpreter_id, new_slot);
});
}
/// Drop this thread's cached slots for every interpreter except `keep_id`.
///
/// After `fork()` only the calling thread survives, and the other
/// interpreters' registries are cleared; a cached slot would otherwise stay
/// current for an interpreter that no longer lists it, hiding the thread from
/// that interpreter's stop-the-world. The next enter builds a fresh slot.
#[cfg(feature = "threading")]
pub fn purge_other_interpreter_slots_after_fork(keep_id: i64) {
INTERP_THREAD_SLOTS.with(|slots| {
slots.borrow_mut().retain(|&id, _| id == keep_id);
});
}
@@ -1023,7 +1171,7 @@ impl VirtualMachine {
callable_cache: self.callable_cache.clone(),
audit_hooks: RefCell::new(vec![]),
pending_tailcall_frame: Cell::new(None),
pending_tailcall_refs: core::cell::UnsafeCell::new(Vec::with_capacity(2)),
pending_tailcall_owner: core::cell::UnsafeCell::new(None),
};
ThreadedVirtualMachine { vm }
}