diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-06-16 19:39:18 -0700 |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-06-16 19:39:18 -0700 |
commit | 821ae956ccb2d1ef5b16a7e056afb3b1b3df19e7 (patch) | |
tree | 70ff1f8b7b63e5a07584e0fd8deb917017073dc7 /Python | |
parent | 80db9e42e6eb7b2de89bca454c253cbfe509b748 (diff) | |
download | cpython-821ae956ccb2d1ef5b16a7e056afb3b1b3df19e7.tar.gz |
avoid crashes and lockups from daemon threads during interpreter shutdown (#1856)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ceval.c | 6 | ||||
-rw-r--r-- | Python/pythonrun.c | 9 | ||||
-rw-r--r-- | Python/thread_pthread.h | 4 |
3 files changed, 16 insertions, 3 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 18bc66b871..b1f5e8eedb 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -355,6 +355,12 @@ PyEval_RestoreThread(PyThreadState *tstate) if (interpreter_lock) { int err = errno; PyThread_acquire_lock(interpreter_lock, 1); + /* _Py_Finalizing is protected by the GIL */ + if (_Py_Finalizing && tstate != _Py_Finalizing) { + PyThread_release_lock(interpreter_lock); + PyThread_exit_thread(); + assert(0); /* unreachable */ + } errno = err; } #endif diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 2a40c0b570..748a63b955 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -91,6 +91,8 @@ int _Py_QnewFlag = 0; int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */ +PyThreadState *_Py_Finalizing = NULL; + /* Hack to force loading of object files */ int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \ @@ -163,6 +165,7 @@ Py_InitializeEx(int install_sigs) if (initialized) return; initialized = 1; + _Py_Finalizing = NULL; if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') Py_DebugFlag = add_flag(Py_DebugFlag, p); @@ -422,12 +425,16 @@ Py_Finalize(void) * the threads created via Threading. */ call_sys_exitfunc(); - initialized = 0; /* 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; + /* Disable signal handling */ PyOS_FiniInterrupts(); diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index c9ed796cd0..0c1fdfea2a 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -242,9 +242,9 @@ void PyThread_exit_thread(void) { dprintf(("PyThread_exit_thread called\n")); - if (!initialized) { + if (!initialized) exit(0); - } + pthread_exit(0); } #ifdef USE_SEMAPHORES |