New submission from STINNER Victor <vstin...@redhat.com>:

Py_EndInterpreter() calls threading._shutdown() which waits for non-daemon 
threads spawned in the subinterpreters. Problem: daemon threads continue to run 
after threading._shutdown(), but they rely on an interpreter which is being 
finalized and then deleted.

Attached example shows the problem:

$ ./python subinterp_daemon_thread.py 
hello from daemon thread
Fatal Python error: Py_EndInterpreter: not the last thread

Current thread 0x00007f13e5926740 (most recent call first):
  File "subinterp_daemon_thread.py", line 23 in <module>
Aborted (core dumped)

Catching the bug in Py_EndInterpreter() is too late. IMHO we must simply deny 
daemon threads by design in subinterpreters for safety.

In the main interpreter, we provide best effort to prevent crash at exit, but 
IMHO the implementation is ugly :-( ceval.c uses exit_thread_if_finalizing(): 
it immediately exit the current daemon thread if the threads attempts to 
acquire or release the GIL, whereas the interpreter is gone. Problem: we cannot 
release/clear some data structure at Python exit because of that. So 
Py_Finalize() may leak some memory by design, because of daemon threads.

IMHO we can be way stricter in subinterpreters.

I suggest to only modify Python 3.9.

----------
components: Interpreter Core
files: subinterp_daemon_thread.py
messages: 345485
nosy: vstinner
priority: normal
severity: normal
status: open
title: Daemon threads must be forbidden in subinterpreters
versions: Python 3.9
Added file: https://bugs.python.org/file48417/subinterp_daemon_thread.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37266>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to