pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Segmentation fault possibly related to thread locals

Open squidfunk opened this issue 1 month ago • 1 comments

Bug Description

We're using PyO3 for Zensical and get a segmentation fault which seems to be related to the cleanup of thread locals. Specifically, calling torch.set_default_device("cpu") triggers the issue. We're calling Python from a worker thread. When moving the call to the main thread, the segfault disappears.

This is the core of this minimal reproduction (repo):

py.detach(|| {
    thread::spawn(|| {
        Python::attach(|py| {
            let code = CString::new(r"
import torch
print('Hello from Python')
torch.set_default_device('cpu')
").unwrap();
            py.run(&code, None, None).expect("Failed to run Python code");
        });
    }).join().expect("Thread panicked");
});

When removing wrapping with thread:spawn, the problem disappears.

Steps to Reproduce

git clone [email protected]:squidfunk/zensical-issue-71.git
cd zensical-issue-71
uv venv
. ./.venv/bin/activate
uv sync
uv run maturin develop
python python/demo/main.py

Backtrace


Your operating system and version

macOS 13.7

Your Python version (python --version)

3.13.6

Your Rust version (rustc --version)

rustc 1.93.0

Your PyO3 version

0.27.1

How did you install python? Did you use a virtualenv?

uv + virtualenv

Additional Info

Likely related:

  • https://github.com/PyO3/pyo3/issues/5359
  • https://github.com/PyO3/pyo3/issues/2611
  • https://github.com/zensical/zensical/issues/71

squidfunk avatar Nov 13 '25 10:11 squidfunk

Possibly also related is #5467

davidhewitt avatar Nov 18 '25 11:11 davidhewitt