mirror of
				https://github.com/python/cpython.git
				synced 2025-11-01 06:01:29 +00:00 
			
		
		
		
	gh-91231: Add shutdown_timeout to multiprocessing BaseManager (#32112)
Add an optional keyword 'shutdown_timeout' parameter to the multiprocessing.BaseManager constructor. Kill the process if terminate() takes longer than the timeout. Multiprocessing tests pass test.support.SHORT_TIMEOUT to BaseManager.shutdown_timeout.
This commit is contained in:
		
							parent
							
								
									74070085da
								
							
						
					
					
						commit
						061a8bf77c
					
				
					 4 changed files with 49 additions and 20 deletions
				
			
		|  | @ -497,7 +497,7 @@ class BaseManager(object): | |||
|     _Server = Server | ||||
| 
 | ||||
|     def __init__(self, address=None, authkey=None, serializer='pickle', | ||||
|                  ctx=None): | ||||
|                  ctx=None, *, shutdown_timeout=1.0): | ||||
|         if authkey is None: | ||||
|             authkey = process.current_process().authkey | ||||
|         self._address = address     # XXX not final address if eg ('', 0) | ||||
|  | @ -507,6 +507,7 @@ def __init__(self, address=None, authkey=None, serializer='pickle', | |||
|         self._serializer = serializer | ||||
|         self._Listener, self._Client = listener_client[serializer] | ||||
|         self._ctx = ctx or get_context() | ||||
|         self._shutdown_timeout = shutdown_timeout | ||||
| 
 | ||||
|     def get_server(self): | ||||
|         ''' | ||||
|  | @ -570,8 +571,8 @@ def start(self, initializer=None, initargs=()): | |||
|         self._state.value = State.STARTED | ||||
|         self.shutdown = util.Finalize( | ||||
|             self, type(self)._finalize_manager, | ||||
|             args=(self._process, self._address, self._authkey, | ||||
|                   self._state, self._Client), | ||||
|             args=(self._process, self._address, self._authkey, self._state, | ||||
|                   self._Client, self._shutdown_timeout), | ||||
|             exitpriority=0 | ||||
|             ) | ||||
| 
 | ||||
|  | @ -656,7 +657,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): | |||
|         self.shutdown() | ||||
| 
 | ||||
|     @staticmethod | ||||
|     def _finalize_manager(process, address, authkey, state, _Client): | ||||
|     def _finalize_manager(process, address, authkey, state, _Client, | ||||
|                           shutdown_timeout): | ||||
|         ''' | ||||
|         Shutdown the manager process; will be registered as a finalizer | ||||
|         ''' | ||||
|  | @ -671,15 +673,17 @@ def _finalize_manager(process, address, authkey, state, _Client): | |||
|             except Exception: | ||||
|                 pass | ||||
| 
 | ||||
|             process.join(timeout=1.0) | ||||
|             process.join(timeout=shutdown_timeout) | ||||
|             if process.is_alive(): | ||||
|                 util.info('manager still alive') | ||||
|                 if hasattr(process, 'terminate'): | ||||
|                     util.info('trying to `terminate()` manager process') | ||||
|                     process.terminate() | ||||
|                     process.join(timeout=0.1) | ||||
|                     process.join(timeout=shutdown_timeout) | ||||
|                     if process.is_alive(): | ||||
|                         util.info('manager still alive after terminate') | ||||
|                         process.kill() | ||||
|                         process.join() | ||||
| 
 | ||||
|         state.value = State.SHUTDOWN | ||||
|         try: | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner