comparison Python/pythonrun.c @ 69833:c892b0321d23

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.
author Antoine Pitrou <solipsis@pitrou.net>
date Wed, 04 May 2011 20:04:29 +0200
parents ce859b848879 2a19d09b08f8
children 8752a81ada2d
comparison
equal deleted inserted replaced
69831:29504a0ab56d 69833:c892b0321d23
91 int Py_FrozenFlag; /* Needed by getpath.c */ 91 int Py_FrozenFlag; /* Needed by getpath.c */
92 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */ 92 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
93 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ 93 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
94 int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ 94 int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
95 95
96 PyThreadState *_Py_Finalizing = NULL;
97
96 /* PyModule_GetWarningsModule is no longer necessary as of 2.6 98 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
97 since _warnings is builtin. This API should not be used. */ 99 since _warnings is builtin. This API should not be used. */
98 PyObject * 100 PyObject *
99 PyModule_GetWarningsModule(void) 101 PyModule_GetWarningsModule(void)
100 { 102 {
189 extern void _Py_ReadyTypes(void); 191 extern void _Py_ReadyTypes(void);
190 192
191 if (initialized) 193 if (initialized)
192 return; 194 return;
193 initialized = 1; 195 initialized = 1;
196 _Py_Finalizing = NULL;
194 197
195 #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE) 198 #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
196 /* Set up the LC_CTYPE locale, so we can obtain 199 /* Set up the LC_CTYPE locale, so we can obtain
197 the locale's charset without having to switch 200 the locale's charset without having to switch
198 locales. */ 201 locales. */
393 * Note that Threading.py uses an exit func to do a join on all the 396 * Note that Threading.py uses an exit func to do a join on all the
394 * threads created thru it, so this also protects pending imports in 397 * threads created thru it, so this also protects pending imports in
395 * the threads created via Threading. 398 * the threads created via Threading.
396 */ 399 */
397 call_py_exitfuncs(); 400 call_py_exitfuncs();
398 initialized = 0;
399
400 /* Flush stdout+stderr */
401 flush_std_files();
402 401
403 /* Get current thread state and interpreter pointer */ 402 /* Get current thread state and interpreter pointer */
404 tstate = PyThreadState_GET(); 403 tstate = PyThreadState_GET();
405 interp = tstate->interp; 404 interp = tstate->interp;
405
406 /* Remaining threads (e.g. daemon threads) will automatically exit
407 after taking the GIL (in PyEval_RestoreThread()). */
408 _Py_Finalizing = tstate;
409 initialized = 0;
410
411 /* Flush stdout+stderr */
412 flush_std_files();
406 413
407 /* Disable signal handling */ 414 /* Disable signal handling */
408 PyOS_FiniInterrupts(); 415 PyOS_FiniInterrupts();
409 416
410 /* Clear type lookup cache */ 417 /* Clear type lookup cache */