Skip to content

Segmentation fault possibly related to thread locals #5626

@squidfunk

Description

@squidfunk

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions