GeezerVison is showing github.com — Avoid attaching C API worker templates under the interpreter lock (#9… · RustPython/RustPython@ec299f3
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: crates/capi/src/pylifecycle.rs +24-1Lines changed: 24 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,7 @@ pub(crate) fn request_vm_from_interpreter() -> ThreadedVirtualMachine { |
19 | 19 | get_main_interpreter() |
20 | 20 | .as_ref() |
21 | 21 | .expect("Interpreter not initialized") |
22 | | - .enter(|vm| vm.new_thread()) |
| 22 | + .new_thread() |
23 | 23 | } |
24 | 24 |
|
25 | 25 | #[unsafe(no_mangle)] |
@@ -123,4 +123,27 @@ mod tests { |
123 | 123 |
|
124 | 124 | assert!(unsafe { pyo3::ffi::Py_Version } >= 0x030d0000); |
125 | 125 | } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn requesting_thread_vm_does_not_attach() { |
| 129 | + use rustpython_vm::vm::thread::{current_thread_slot, current_vm_is_set}; |
| 130 | + |
| 131 | + Python::attach(|py| { |
| 132 | + let handle = std::thread::spawn(|| { |
| 133 | + let before = (current_vm_is_set(), current_thread_slot().is_some()); |
| 134 | + let worker_vm = super::request_vm_from_interpreter(); |
| 135 | + let after = (current_vm_is_set(), current_thread_slot().is_some()); |
| 136 | + (worker_vm, before, after) |
| 137 | + }); |
| 138 | + let (worker_vm, before, after) = py.detach(|| handle.join().unwrap()); |
| 139 | + // Drop cloned Python references under the parent interpreter context. |
| 140 | + drop(worker_vm); |
| 141 | + assert_eq!(before, (false, false), "worker must start outside any VM"); |
| 142 | + assert_eq!( |
| 143 | + after, |
| 144 | + (false, false), |
| 145 | + "creating a worker VM must not attach under MAIN_INTERP" |
| 146 | + ); |
| 147 | + }); |
| 148 | + } |
126 | 149 | } |