mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	[3.9] bpo-37788: Fix reference leak when Thread is never joined (GH-26103) (GH-26142)
When a Thread is not joined after it has stopped, its lock may remain in the _shutdown_locks set until interpreter shutdown.  If many threads are created this way, the _shutdown_locks set could therefore grow endlessly.  To avoid such a situation, purge expired locks each time a new one is added or removed..
(cherry picked from commit c10c2ec7a0)
Co-authored-by: Antoine Pitrou <antoine@python.org>
Automerge-Triggered-By: GH:pitrou
			
			
This commit is contained in:
		
							parent
							
								
									fa9de0c383
								
							
						
					
					
						commit
						b30b25b266
					
				
					 3 changed files with 27 additions and 1 deletions
				
			
		|  | @ -805,6 +805,14 @@ def __del__(self): | ||||||
|         """) |         """) | ||||||
|         self.assertEqual(out.rstrip(), b"thread_dict.atexit = 'value'") |         self.assertEqual(out.rstrip(), b"thread_dict.atexit = 'value'") | ||||||
| 
 | 
 | ||||||
|  |     def test_leak_without_join(self): | ||||||
|  |         # bpo-37788: Test that a thread which is not joined explicitly | ||||||
|  |         # does not leak. Test written for reference leak checks. | ||||||
|  |         def noop(): pass | ||||||
|  |         with support.wait_threads_exit(): | ||||||
|  |             threading.Thread(target=noop).start() | ||||||
|  |             # Thread.join() is not called | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| class ThreadJoinOnShutdown(BaseTestCase): | class ThreadJoinOnShutdown(BaseTestCase): | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -755,12 +755,27 @@ def _newname(template="Thread-%d"): | ||||||
| _active = {}    # maps thread id to Thread object | _active = {}    # maps thread id to Thread object | ||||||
| _limbo = {} | _limbo = {} | ||||||
| _dangling = WeakSet() | _dangling = WeakSet() | ||||||
|  | 
 | ||||||
| # Set of Thread._tstate_lock locks of non-daemon threads used by _shutdown() | # Set of Thread._tstate_lock locks of non-daemon threads used by _shutdown() | ||||||
| # to wait until all Python thread states get deleted: | # to wait until all Python thread states get deleted: | ||||||
| # see Thread._set_tstate_lock(). | # see Thread._set_tstate_lock(). | ||||||
| _shutdown_locks_lock = _allocate_lock() | _shutdown_locks_lock = _allocate_lock() | ||||||
| _shutdown_locks = set() | _shutdown_locks = set() | ||||||
| 
 | 
 | ||||||
|  | def _maintain_shutdown_locks(): | ||||||
|  |     """ | ||||||
|  |     Drop any shutdown locks that don't correspond to running threads anymore. | ||||||
|  | 
 | ||||||
|  |     Calling this from time to time avoids an ever-growing _shutdown_locks | ||||||
|  |     set when Thread objects are not joined explicitly. See bpo-37788. | ||||||
|  | 
 | ||||||
|  |     This must be called with _shutdown_locks_lock acquired. | ||||||
|  |     """ | ||||||
|  |     # If a lock was released, the corresponding thread has exited | ||||||
|  |     to_remove = [lock for lock in _shutdown_locks if not lock.locked()] | ||||||
|  |     _shutdown_locks.difference_update(to_remove) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| # Main class for threads | # Main class for threads | ||||||
| 
 | 
 | ||||||
| class Thread: | class Thread: | ||||||
|  | @ -932,6 +947,7 @@ def _set_tstate_lock(self): | ||||||
| 
 | 
 | ||||||
|         if not self.daemon: |         if not self.daemon: | ||||||
|             with _shutdown_locks_lock: |             with _shutdown_locks_lock: | ||||||
|  |                 _maintain_shutdown_locks() | ||||||
|                 _shutdown_locks.add(self._tstate_lock) |                 _shutdown_locks.add(self._tstate_lock) | ||||||
| 
 | 
 | ||||||
|     def _bootstrap_inner(self): |     def _bootstrap_inner(self): | ||||||
|  | @ -987,7 +1003,8 @@ def _stop(self): | ||||||
|         self._tstate_lock = None |         self._tstate_lock = None | ||||||
|         if not self.daemon: |         if not self.daemon: | ||||||
|             with _shutdown_locks_lock: |             with _shutdown_locks_lock: | ||||||
|                 _shutdown_locks.discard(lock) |                 # Remove our lock and other released locks from _shutdown_locks | ||||||
|  |                 _maintain_shutdown_locks() | ||||||
| 
 | 
 | ||||||
|     def _delete(self): |     def _delete(self): | ||||||
|         "Remove current thread from the dict of currently running threads." |         "Remove current thread from the dict of currently running threads." | ||||||
|  |  | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | Fix a reference leak when a Thread object is never joined. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Antoine Pitrou
						Antoine Pitrou