summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-05-04 18:04:29 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-05-04 18:04:29 (GMT)
commitfc20b0c65c446f79e39d006f3cffed03aa745380 (patch)
treed26d9bdfcce057847cc8b878621ac59ad547c695 /Python
parentc17c6d95cf989a89d77ab4a655599531479bd268 (diff)
parent0d5e52d3469a310001afe50689f77ddba6d554d1 (diff)
downloadcpython-fc20b0c65c446f79e39d006f3cffed03aa745380.zip
cpython-fc20b0c65c446f79e39d006f3cffed03aa745380.tar.gz
cpython-fc20b0c65c446f79e39d006f3cffed03aa745380.tar.bz2
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.
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c6
-rw-r--r--Python/pythonrun.c15
-rw-r--r--Python/thread_pthread.h4
3 files changed, 19 insertions, 6 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 50d5aac..5627e83 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -440,6 +440,12 @@ PyEval_RestoreThread(PyThreadState *tstate)
if (gil_created()) {
int err = errno;
take_gil(tstate);
+ /* _Py_Finalizing is protected by the GIL */
+ if (_Py_Finalizing && tstate != _Py_Finalizing) {
+ drop_gil(tstate);
+ PyThread_exit_thread();
+ assert(0); /* unreachable */
+ }
errno = err;
}
#endif
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index ad31613..ebc4f1c 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -93,6 +93,8 @@ int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
+PyThreadState *_Py_Finalizing = NULL;
+
/* PyModule_GetWarningsModule is no longer necessary as of 2.6
since _warnings is builtin. This API should not be used. */
PyObject *
@@ -191,6 +193,7 @@ Py_InitializeEx(int install_sigs)
if (initialized)
return;
initialized = 1;
+ _Py_Finalizing = NULL;
#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
/* Set up the LC_CTYPE locale, so we can obtain
@@ -395,15 +398,19 @@ Py_Finalize(void)
* the threads created via Threading.
*/
call_py_exitfuncs();
- initialized = 0;
-
- /* Flush stdout+stderr */
- flush_std_files();
/* Get current thread state and interpreter pointer */
tstate = PyThreadState_GET();
interp = tstate->interp;
+ /* Remaining threads (e.g. daemon threads) will automatically exit
+ after taking the GIL (in PyEval_RestoreThread()). */
+ _Py_Finalizing = tstate;
+ initialized = 0;
+
+ /* Flush stdout+stderr */
+ flush_std_files();
+
/* Disable signal handling */
PyOS_FiniInterrupts();
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index 7d36b92..40ebaf6 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -244,9 +244,9 @@ void
PyThread_exit_thread(void)
{
dprintf(("PyThread_exit_thread called\n"));
- if (!initialized) {
+ if (!initialized)
exit(0);
- }
+ pthread_exit(0);
}
#ifdef USE_SEMAPHORES