1#[cfg(pyo3_disable_reference_pool)]
4use crate::impl_::panic::PanicTrap;
5use crate::{ffi, Python};
6#[cfg(not(pyo3_disable_reference_pool))]
7use once_cell::sync::Lazy;
8use std::cell::Cell;
9use std::{mem, ptr::NonNull, sync};
10
11static START: sync::Once = sync::Once::new();
12
13std::thread_local! {
14 static GIL_COUNT: Cell<isize> = const { Cell::new(0) };
24}
25
26const GIL_LOCKED_DURING_TRAVERSE: isize = -1;
27
28#[inline(always)]
35fn gil_is_acquired() -> bool {
36 GIL_COUNT.try_with(|c| c.get() > 0).unwrap_or(false)
37}
38
39#[cfg(not(any(PyPy, GraalPy)))]
62pub fn prepare_freethreaded_python() {
63 START.call_once_force(|_| unsafe {
67 if ffi::Py_IsInitialized() == 0 {
69 ffi::Py_InitializeEx(0);
70
71 ffi::PyEval_SaveThread();
73 }
74 });
75}
76
77#[cfg(not(any(PyPy, GraalPy)))]
109pub unsafe fn with_embedded_python_interpreter<F, R>(f: F) -> R
110where
111 F: for<'p> FnOnce(Python<'p>) -> R,
112{
113 assert_eq!(
114 ffi::Py_IsInitialized(),
115 0,
116 "called `with_embedded_python_interpreter` but a Python interpreter is already running."
117 );
118
119 ffi::Py_InitializeEx(0);
120
121 let result = {
122 let guard = GILGuard::assume();
123 let py = guard.python();
124 py.import("threading").unwrap();
127
128 f(py)
130 };
131
132 ffi::Py_Finalize();
134
135 result
136}
137
138pub(crate) enum GILGuard {
140 Assumed,
142 Ensured { gstate: ffi::PyGILState_STATE },
144}
145
146impl GILGuard {
147 pub(crate) fn acquire() -> Self {
153 if gil_is_acquired() {
154 return unsafe { Self::assume() };
156 }
157
158 cfg_if::cfg_if! {
165 if #[cfg(all(feature = "auto-initialize", not(any(PyPy, GraalPy))))] {
166 prepare_freethreaded_python();
167 } else {
168 #[cfg(not(any(PyPy, GraalPy)))]
173 if option_env!("CARGO_PRIMARY_PACKAGE").is_some() {
174 prepare_freethreaded_python();
175 }
176
177 START.call_once_force(|_| unsafe {
178 assert_ne!(
182 ffi::Py_IsInitialized(),
183 0,
184 "The Python interpreter is not initialized and the `auto-initialize` \
185 feature is not enabled.\n\n\
186 Consider calling `pyo3::prepare_freethreaded_python()` before attempting \
187 to use Python APIs."
188 );
189 });
190 }
191 }
192
193 unsafe { Self::acquire_unchecked() }
195 }
196
197 pub(crate) unsafe fn acquire_unchecked() -> Self {
203 if gil_is_acquired() {
204 return Self::assume();
205 }
206
207 let gstate = ffi::PyGILState_Ensure(); increment_gil_count();
209
210 #[cfg(not(pyo3_disable_reference_pool))]
211 if let Some(pool) = Lazy::get(&POOL) {
212 pool.update_counts(Python::assume_gil_acquired());
213 }
214 GILGuard::Ensured { gstate }
215 }
216
217 pub(crate) unsafe fn assume() -> Self {
219 increment_gil_count();
220 let guard = GILGuard::Assumed;
221 #[cfg(not(pyo3_disable_reference_pool))]
222 if let Some(pool) = Lazy::get(&POOL) {
223 pool.update_counts(guard.python());
224 }
225 guard
226 }
227
228 #[inline]
230 pub fn python(&self) -> Python<'_> {
231 unsafe { Python::assume_gil_acquired() }
232 }
233}
234
235impl Drop for GILGuard {
237 fn drop(&mut self) {
238 match self {
239 GILGuard::Assumed => {}
240 GILGuard::Ensured { gstate } => unsafe {
241 ffi::PyGILState_Release(*gstate);
243 },
244 }
245 decrement_gil_count();
246 }
247}
248
249type PyObjVec = Vec<NonNull<ffi::PyObject>>;
251
252#[cfg(not(pyo3_disable_reference_pool))]
253struct ReferencePool {
255 pending_decrefs: sync::Mutex<PyObjVec>,
256}
257
258#[cfg(not(pyo3_disable_reference_pool))]
259impl ReferencePool {
260 const fn new() -> Self {
261 Self {
262 pending_decrefs: sync::Mutex::new(Vec::new()),
263 }
264 }
265
266 fn register_decref(&self, obj: NonNull<ffi::PyObject>) {
267 self.pending_decrefs.lock().unwrap().push(obj);
268 }
269
270 fn update_counts(&self, _py: Python<'_>) {
271 let mut pending_decrefs = self.pending_decrefs.lock().unwrap();
272 if pending_decrefs.is_empty() {
273 return;
274 }
275
276 let decrefs = mem::take(&mut *pending_decrefs);
277 drop(pending_decrefs);
278
279 for ptr in decrefs {
280 unsafe { ffi::Py_DECREF(ptr.as_ptr()) };
281 }
282 }
283}
284
285#[cfg(not(pyo3_disable_reference_pool))]
286unsafe impl Send for ReferencePool {}
287
288#[cfg(not(pyo3_disable_reference_pool))]
289unsafe impl Sync for ReferencePool {}
290
291#[cfg(not(pyo3_disable_reference_pool))]
292static POOL: Lazy<ReferencePool> = Lazy::new(ReferencePool::new);
293
294pub(crate) struct SuspendGIL {
296 count: isize,
297 tstate: *mut ffi::PyThreadState,
298}
299
300impl SuspendGIL {
301 pub(crate) unsafe fn new() -> Self {
302 let count = GIL_COUNT.with(|c| c.replace(0));
303 let tstate = ffi::PyEval_SaveThread();
304
305 Self { count, tstate }
306 }
307}
308
309impl Drop for SuspendGIL {
310 fn drop(&mut self) {
311 GIL_COUNT.with(|c| c.set(self.count));
312 unsafe {
313 ffi::PyEval_RestoreThread(self.tstate);
314
315 #[cfg(not(pyo3_disable_reference_pool))]
317 if let Some(pool) = Lazy::get(&POOL) {
318 pool.update_counts(Python::assume_gil_acquired());
319 }
320 }
321 }
322}
323
324pub(crate) struct LockGIL {
326 count: isize,
327}
328
329impl LockGIL {
330 pub fn during_traverse() -> Self {
332 Self::new(GIL_LOCKED_DURING_TRAVERSE)
333 }
334
335 fn new(reason: isize) -> Self {
336 let count = GIL_COUNT.with(|c| c.replace(reason));
337
338 Self { count }
339 }
340
341 #[cold]
342 fn bail(current: isize) {
343 match current {
344 GIL_LOCKED_DURING_TRAVERSE => panic!(
345 "Access to the GIL is prohibited while a __traverse__ implmentation is running."
346 ),
347 _ => panic!("Access to the GIL is currently prohibited."),
348 }
349 }
350}
351
352impl Drop for LockGIL {
353 fn drop(&mut self) {
354 GIL_COUNT.with(|c| c.set(self.count));
355 }
356}
357
358#[cfg(feature = "py-clone")]
364#[track_caller]
365pub unsafe fn register_incref(obj: NonNull<ffi::PyObject>) {
366 if gil_is_acquired() {
367 ffi::Py_INCREF(obj.as_ptr())
368 } else {
369 panic!("Cannot clone pointer into Python heap without the GIL being held.");
370 }
371}
372
373#[track_caller]
382pub unsafe fn register_decref(obj: NonNull<ffi::PyObject>) {
383 if gil_is_acquired() {
384 ffi::Py_DECREF(obj.as_ptr())
385 } else {
386 #[cfg(not(pyo3_disable_reference_pool))]
387 POOL.register_decref(obj);
388 #[cfg(all(
389 pyo3_disable_reference_pool,
390 not(pyo3_leak_on_drop_without_reference_pool)
391 ))]
392 {
393 let _trap = PanicTrap::new("Aborting the process to avoid panic-from-drop.");
394 panic!("Cannot drop pointer into Python heap without the GIL being held.");
395 }
396 }
397}
398
399#[inline(always)]
401fn increment_gil_count() {
402 let _ = GIL_COUNT.try_with(|c| {
404 let current = c.get();
405 if current < 0 {
406 LockGIL::bail(current);
407 }
408 c.set(current + 1);
409 });
410}
411
412#[inline(always)]
414fn decrement_gil_count() {
415 let _ = GIL_COUNT.try_with(|c| {
417 let current = c.get();
418 debug_assert!(
419 current > 0,
420 "Negative GIL count detected. Please report this error to the PyO3 repo as a bug."
421 );
422 c.set(current - 1);
423 });
424}
425
426#[cfg(test)]
427mod tests {
428 use super::GIL_COUNT;
429 #[cfg(not(pyo3_disable_reference_pool))]
430 use super::{gil_is_acquired, POOL};
431 use crate::{ffi, PyObject, Python};
432 use crate::{gil::GILGuard, types::any::PyAnyMethods};
433 use std::ptr::NonNull;
434
435 fn get_object(py: Python<'_>) -> PyObject {
436 py.eval(ffi::c_str!("object()"), None, None)
437 .unwrap()
438 .unbind()
439 }
440
441 #[cfg(not(pyo3_disable_reference_pool))]
442 fn pool_dec_refs_does_not_contain(obj: &PyObject) -> bool {
443 !POOL
444 .pending_decrefs
445 .lock()
446 .unwrap()
447 .contains(&unsafe { NonNull::new_unchecked(obj.as_ptr()) })
448 }
449
450 #[cfg(not(any(pyo3_disable_reference_pool, Py_GIL_DISABLED)))]
453 fn pool_dec_refs_contains(obj: &PyObject) -> bool {
454 POOL.pending_decrefs
455 .lock()
456 .unwrap()
457 .contains(&unsafe { NonNull::new_unchecked(obj.as_ptr()) })
458 }
459
460 #[test]
461 fn test_pyobject_drop_with_gil_decreases_refcnt() {
462 Python::with_gil(|py| {
463 let obj = get_object(py);
464
465 let reference = obj.clone_ref(py);
467
468 assert_eq!(obj.get_refcnt(py), 2);
469 #[cfg(not(pyo3_disable_reference_pool))]
470 assert!(pool_dec_refs_does_not_contain(&obj));
471
472 drop(reference);
474
475 assert_eq!(obj.get_refcnt(py), 1);
476 #[cfg(not(any(pyo3_disable_reference_pool)))]
477 assert!(pool_dec_refs_does_not_contain(&obj));
478 });
479 }
480
481 #[test]
482 #[cfg(all(not(pyo3_disable_reference_pool), not(target_arch = "wasm32")))] fn test_pyobject_drop_without_gil_doesnt_decrease_refcnt() {
484 let obj = Python::with_gil(|py| {
485 let obj = get_object(py);
486 let reference = obj.clone_ref(py);
488
489 assert_eq!(obj.get_refcnt(py), 2);
490 assert!(pool_dec_refs_does_not_contain(&obj));
491
492 std::thread::spawn(move || drop(reference)).join().unwrap();
494
495 assert_eq!(obj.get_refcnt(py), 2);
498 #[cfg(not(Py_GIL_DISABLED))]
499 assert!(pool_dec_refs_contains(&obj));
500 obj
501 });
502
503 #[allow(unused)]
505 Python::with_gil(|py| {
506 #[cfg(not(Py_GIL_DISABLED))]
510 assert_eq!(obj.get_refcnt(py), 1);
511 assert!(pool_dec_refs_does_not_contain(&obj));
512 });
513 }
514
515 #[test]
516 #[allow(deprecated)]
517 fn test_gil_counts() {
518 let get_gil_count = || GIL_COUNT.with(|c| c.get());
520
521 assert_eq!(get_gil_count(), 0);
522 Python::with_gil(|_| {
523 assert_eq!(get_gil_count(), 1);
524
525 let pool = unsafe { GILGuard::assume() };
526 assert_eq!(get_gil_count(), 2);
527
528 let pool2 = unsafe { GILGuard::assume() };
529 assert_eq!(get_gil_count(), 3);
530
531 drop(pool);
532 assert_eq!(get_gil_count(), 2);
533
534 Python::with_gil(|_| {
535 assert_eq!(get_gil_count(), 3);
537 });
538 assert_eq!(get_gil_count(), 2);
539
540 drop(pool2);
541 assert_eq!(get_gil_count(), 1);
542 });
543 assert_eq!(get_gil_count(), 0);
544 }
545
546 #[test]
547 fn test_allow_threads() {
548 assert!(!gil_is_acquired());
549
550 Python::with_gil(|py| {
551 assert!(gil_is_acquired());
552
553 py.allow_threads(move || {
554 assert!(!gil_is_acquired());
555
556 Python::with_gil(|_| assert!(gil_is_acquired()));
557
558 assert!(!gil_is_acquired());
559 });
560
561 assert!(gil_is_acquired());
562 });
563
564 assert!(!gil_is_acquired());
565 }
566
567 #[cfg(feature = "py-clone")]
568 #[test]
569 #[should_panic]
570 fn test_allow_threads_updates_refcounts() {
571 Python::with_gil(|py| {
572 let obj = get_object(py);
574 assert!(obj.get_refcnt(py) == 1);
575 py.allow_threads(|| obj.clone());
577 });
578 }
579
580 #[test]
581 fn dropping_gil_does_not_invalidate_references() {
582 Python::with_gil(|py| {
584 let obj = Python::with_gil(|_| py.eval(ffi::c_str!("object()"), None, None).unwrap());
585
586 assert_eq!(obj.get_refcnt(), 1);
588 })
589 }
590
591 #[cfg(feature = "py-clone")]
592 #[test]
593 fn test_clone_with_gil() {
594 Python::with_gil(|py| {
595 let obj = get_object(py);
596 let count = obj.get_refcnt(py);
597
598 #[allow(clippy::redundant_clone)]
600 let c = obj.clone();
601 assert_eq!(count + 1, c.get_refcnt(py));
602 })
603 }
604
605 #[test]
606 #[cfg(not(pyo3_disable_reference_pool))]
607 fn test_update_counts_does_not_deadlock() {
608 use crate::ffi;
612 use crate::gil::GILGuard;
613
614 Python::with_gil(|py| {
615 let obj = get_object(py);
616
617 unsafe extern "C" fn capsule_drop(capsule: *mut ffi::PyObject) {
618 let pool = GILGuard::assume();
621
622 PyObject::from_owned_ptr(
624 pool.python(),
625 ffi::PyCapsule_GetPointer(capsule, std::ptr::null()) as _,
626 );
627 }
628
629 let ptr = obj.into_ptr();
630
631 let capsule =
632 unsafe { ffi::PyCapsule_New(ptr as _, std::ptr::null(), Some(capsule_drop)) };
633
634 POOL.register_decref(NonNull::new(capsule).unwrap());
635
636 POOL.update_counts(py);
638 })
639 }
640
641 #[test]
642 #[cfg(not(pyo3_disable_reference_pool))]
643 fn test_gil_guard_update_counts() {
644 use crate::gil::GILGuard;
645
646 Python::with_gil(|py| {
647 let obj = get_object(py);
648
649 POOL.register_decref(NonNull::new(obj.clone_ref(py).into_ptr()).unwrap());
652 #[cfg(not(Py_GIL_DISABLED))]
653 assert!(pool_dec_refs_contains(&obj));
654 let _guard = GILGuard::acquire();
655 assert!(pool_dec_refs_does_not_contain(&obj));
656
657 POOL.register_decref(NonNull::new(obj.clone_ref(py).into_ptr()).unwrap());
660 #[cfg(not(Py_GIL_DISABLED))]
661 assert!(pool_dec_refs_contains(&obj));
662 let _guard2 = unsafe { GILGuard::assume() };
663 assert!(pool_dec_refs_does_not_contain(&obj));
664 })
665 }
666}