Segmentation fault possibly related to thread locals
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
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:
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 git@github.com: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:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by cloning zensical-issue-71 and running the listed uv, maturin, and Python commands to reproduce the crash. Compare the Python::attach call on the worker thread with the version without thread::spawn, focusing on cleanup of thread locals around torch.set_default_device("cpu"). Done means the minimal reproduction no longer segfaults when Python runs from the worker thread.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100