mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	gh-87135: Hang non-main threads that attempt to acquire the GIL during finalization (GH-105805)
Instead of surprise crashes and memory corruption, we now hang threads that attempt to re-enter the Python interpreter after Python runtime finalization has started. These are typically daemon threads (our long standing mis-feature) but could also be threads spawned by extension modules that then try to call into Python. This marks the `PyThread_exit_thread` public C API as deprecated as there is no plausible safe way to accomplish that on any supported platform in the face of things like C++ code with finalizers anywhere on a thread's stack. Doing this was the least bad option. Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
		
							parent
							
								
									113b2d7583
								
							
						
					
					
						commit
						8cc5aa47ee
					
				
					 10 changed files with 247 additions and 29 deletions
				
			
		| 
						 | 
				
			
			@ -2020,7 +2020,7 @@ _Py_Finalize(_PyRuntimeState *runtime)
 | 
			
		|||
    /* Ensure that remaining threads are detached */
 | 
			
		||||
    _PyEval_StopTheWorldAll(runtime);
 | 
			
		||||
 | 
			
		||||
    /* Remaining daemon threads will automatically exit
 | 
			
		||||
    /* Remaining daemon threads will be trapped in PyThread_hang_thread
 | 
			
		||||
       when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
 | 
			
		||||
    _PyInterpreterState_SetFinalizing(tstate->interp, tstate);
 | 
			
		||||
    _PyRuntimeState_SetFinalizing(runtime, tstate);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue