You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
test_finalize_with_trace (in test_threading) sometimes fails because of failing to destroy the GIL (in _PyEval_FiniThreads()). This can be reproduced quite reliably by launching several copies in parallel:
What happens is that pthread_mutex_destroy() fails with EBUSY. According to the POSIX man page:
“[EBUSY]
The implementation has detected an attempt to destroy the object referenced by mutex while it is locked or referenced (for example, while being used in a pthread_cond_timedwait() or pthread_cond_wait()) by another thread.”
After a bit of tracing, it becomes clear that Py_Finalize() calls _PyEval_FiniThreads() while another thread is taking the GIL (take_gil()). Unfortunately, this is not a situation we can avoid, since we rely on process exit to kill lingering threads: arbitrary CPython code may still be running in parallel while we are finalizing interpreter structures.
Therefore, it is likely that _PyEval_FiniThreads() should avoid destroying the mutex at all. Indeed, if we destroy the mutex, it is possible that a lingering thread tries to retake the GIL after waking up from a system call (Py_END_ALLOW_THREADS), and fails because of another fatal error ("Fatal Python error: pthread_mutex_lock(gil_mutex) failed").
It is similar, but the issue is slightly different. Fixing bpo-1856 with the proposed resolution (the "stay_forever" flag) won't solve this, because the GIL mutex will still refuse to be destroyed if other threads reference it at the same time.
Moving the _PyEval_FiniThreads() call to Py_Initialize() solves the issue:
diff -r 9e49082da463 Python/pythonrun.c--- a/Python/pythonrun.c Mon Sep 20 12:46:56 2010 +0200+++ b/Python/pythonrun.c Mon Sep 20 13:41:47 2010 +0200@@ -217,8 +217,15 @@ Py_InitializeEx(int install_sigs)
Py_FatalError("Py_Initialize: can't make first thread");
(void) PyThreadState_Swap(tstate);
- /* auto-thread-state API, if available */
#ifdef WITH_THREAD
+ /* We can't call _PyEval_FiniThreads() in Py_Finalize because+ destroying the GIL might fail when it is being referenced from+ another running thread (see issue python/cpython#54110).+ Instead we destroy the previously created GIL here, which ensures+ that we can call Py_Initialize / Py_Finalize multiple times. */+ _PyEval_FiniThreads();++ /* Auto-thread-state API */
_PyGILState_Init(interp, tstate);
#endif /* WITH_THREAD */
@@ -514,10 +521,6 @@ Py_Finalize(void)
PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
-#ifdef WITH_THREAD- _PyEval_FiniThreads();-#endif-
#ifdef Py_TRACE_REFS
/* Display addresses (& refcnts) of all objects still alive.
* An address can be used to find the repr of the object, printed
Activity
pitrou commentedon Sep 19, 2010
test_finalize_with_trace (in test_threading) sometimes fails because of failing to destroy the GIL (in _PyEval_FiniThreads()). This can be reproduced quite reliably by launching several copies in parallel:
What happens is that pthread_mutex_destroy() fails with EBUSY. According to the POSIX man page:
“[EBUSY]
The implementation has detected an attempt to destroy the object referenced by mutex while it is locked or referenced (for example, while being used in a pthread_cond_timedwait() or pthread_cond_wait()) by another thread.”
After a bit of tracing, it becomes clear that Py_Finalize() calls _PyEval_FiniThreads() while another thread is taking the GIL (take_gil()). Unfortunately, this is not a situation we can avoid, since we rely on process exit to kill lingering threads: arbitrary CPython code may still be running in parallel while we are finalizing interpreter structures.
Therefore, it is likely that _PyEval_FiniThreads() should avoid destroying the mutex at all. Indeed, if we destroy the mutex, it is possible that a lingering thread tries to retake the GIL after waking up from a system call (Py_END_ALLOW_THREADS), and fails because of another fatal error ("Fatal Python error: pthread_mutex_lock(gil_mutex) failed").
amauryfa commentedon Sep 20, 2010
looks similar to bpo-1856
pitrou commentedon Sep 20, 2010
It is similar, but the issue is slightly different. Fixing bpo-1856 with the proposed resolution (the "stay_forever" flag) won't solve this, because the GIL mutex will still refuse to be destroyed if other threads reference it at the same time.
pitrou commentedon Sep 20, 2010
Moving the _PyEval_FiniThreads() call to Py_Initialize() solves the issue:
pitrou commentedon Sep 20, 2010
Committed in r84927.