This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2021-06-16.15:11:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1623856268.21.0.375741727442.issue44434@roundup.psfhosted.org>
In-reply-to
Content
PyThread_exit_thread() was modified in 2011 to fix daemon threads:

commit 0d5e52d3469a310001afe50689f77ddba6d554d1
Author: Antoine Pitrou <solipsis@pitrou.net>
Date:   Wed May 4 20:02:30 2011 +0200

    Issue #1856: Avoid crashes and lockups when daemon threads run while the
    interpreter is shutting down; instead, these threads are now killed when
    they try to take the GIL.

 PyThread_exit_thread(void)
 {
     dprintf(("PyThread_exit_thread called\n"));
-    if (!initialized) {
+    if (!initialized)
         exit(0);
-    }
+    pthread_exit(0);
 }


This change remains important for Python/ceval.c. When a daemon thread tries to acquire the GIL, it calls PyThread_exit_thread() if Python already exited to exit immediately the thread. Example from take_gil():

    if (tstate_must_exit(tstate)) {
        /* bpo-39877: If Py_Finalize() has been called and tstate is not the
           thread which called Py_Finalize(), exit immediately the thread.

           This code path can be reached by a daemon thread after Py_Finalize()
           completes. In this case, tstate is a dangling pointer: points to
           PyThreadState freed memory. */
        PyThread_exit_thread();
    }

See also my articles on daemon threads fixes:

* https://vstinner.github.io/gil-bugfixes-daemon-threads-python39.html
* https://vstinner.github.io/daemon-threads-python-finalization-python32.html
History
Date User Action Args
2021-06-16 15:11:08vstinnersetrecipients: + vstinner
2021-06-16 15:11:08vstinnersetmessageid: <1623856268.21.0.375741727442.issue44434@roundup.psfhosted.org>
2021-06-16 15:11:08vstinnerlinkissue44434 messages
2021-06-16 15:11:07vstinnercreate