Skip to content

Fatal python error when detach in a rust thread during finalization #6084

Closed

Description

@anuraaga

Bug Description

I am examining a bug report in a PyO3 project - curioswitch/pyqwest#164

It is essentially in the class of "abort instead of hang on finalization" bugs fixed broadly in Python 3.14 itself, with PyO3 fixing the issue on previous versions for attach in #4874, but there is still an issue with detach, if not when detaching at least when reattaching. In fact, Python 3.14 specifically added the hanging behavior for PyEval_RestoreThread as well (also PyEval_AcquireThread though I'm not sure if PyO3 uses that)

https://docs.python.org/3/c-api/threads.html#c.PyEval_RestoreThread

So in principle, I guess the exact same change needs to be done to thread restoration as well.

Unfortunately, I was unable to create a reliable unit test for this. I could get this different error message, but I think it's a different issue.

I could come up with a test that fails reliably without the fix! Sent #6085 with the proposal

Steps to Reproduce

Taken from original bug report - it fails reliably for me as well.

docker run --rm python:3.11-slim bash -c '
pip install -q pyqwest==0.6.0
cat > /tmp/r.py <<"PY"
import threading, time, pyqwest
def h(u, t):
    c = pyqwest.SyncClient()
    while True:
        try: c.get(u, timeout=t)
        except Exception: pass
for _ in range(16): threading.Thread(target=h, args=("http://127.0.0.1:9/", 1.0), daemon=True).start()
for _ in range(8):  threading.Thread(target=h, args=("http://192.0.2.1:81/", 5.0), daemon=True).start()
time.sleep(1.0)
PY
for i in $(seq 1 12); do python /tmp/r.py; echo "iter $i exit=$?"; done
'

Backtrace

#2  abort ()
#4  __libc_fatal ()                                  "FATAL: exception not rethrown"
#6  __rustc::__rust_panic_cleanup ()
#7  std::panicking::catch_unwind::cleanup ()
#8  pyo3::impl_::trampoline::trampoline ()
#9  pyo3::impl_::trampoline::fastcall_cfunction_with_keywords ()
#10 method_vectorcall_FASTCALL_KEYWORDS ()
...
#22 thread_run () at ./Modules/_threadmodule.c:1124
#23 pythread_wrapper () at Python/thread_pthread.h:241

Your operating system and version

Linux

Your Python version (python --version)

Python 3.11

Your Rust version (rustc --version)

1.95.0 (59807616e 2026-04-14)

Your PyO3 version

0.28.3

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

manylinux docker image

Additional Info

No response

Activity

  1. added theissue type on Jun 1, 2026
  2. changed the title [-]Fatal python error when finishing detach in a rust thread[/-] [+]Fatal python error when detach in a rust thread during finalization[/+] on Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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