blob: a4683339820f5fec01e80ce087dc3333293c42a5 [file] [log] [blame]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001#
2# Module providing various facilities to other parts of the package
3#
4# multiprocessing/util.py
5#
R. David Murray3fc969a2010-12-14 01:38:16 +00006# Copyright (c) 2006-2008, R Oudkerk
Richard Oudkerk3e268aa2012-04-30 12:13:55 +01007# Licensed to PSF under a Contributor Agreement.
Benjamin Petersone711caf2008-06-11 16:44:04 +00008#
9
Richard Oudkerk739ae562012-05-25 13:54:53 +010010import os
Benjamin Petersone711caf2008-06-11 16:44:04 +000011import itertools
Victor Stinnera6d865c2016-03-25 09:29:50 +010012import sys
Benjamin Petersone711caf2008-06-11 16:44:04 +000013import weakref
Benjamin Petersone711caf2008-06-11 16:44:04 +000014import atexit
15import threading # we want threading to install it's
16 # cleanup function before multiprocessing does
Antoine Pitrouebdcd852012-05-18 18:33:07 +020017from subprocess import _args_from_interpreter_flags
Benjamin Petersone711caf2008-06-11 16:44:04 +000018
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010019from . import process
Benjamin Petersone711caf2008-06-11 16:44:04 +000020
21__all__ = [
22 'sub_debug', 'debug', 'info', 'sub_warning', 'get_logger',
23 'log_to_stderr', 'get_temp_dir', 'register_after_fork',
Jesse Noller41faa542009-01-25 03:45:53 +000024 'is_exiting', 'Finalize', 'ForkAwareThreadLock', 'ForkAwareLocal',
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010025 'close_all_fds_except', 'SUBDEBUG', 'SUBWARNING',
Benjamin Petersone711caf2008-06-11 16:44:04 +000026 ]
27
28#
29# Logging
30#
31
32NOTSET = 0
33SUBDEBUG = 5
34DEBUG = 10
35INFO = 20
36SUBWARNING = 25
37
38LOGGER_NAME = 'multiprocessing'
39DEFAULT_LOGGING_FORMAT = '[%(levelname)s/%(processName)s] %(message)s'
40
41_logger = None
42_log_to_stderr = False
43
44def sub_debug(msg, *args):
45 if _logger:
46 _logger.log(SUBDEBUG, msg, *args)
47
48def debug(msg, *args):
49 if _logger:
50 _logger.log(DEBUG, msg, *args)
51
52def info(msg, *args):
53 if _logger:
54 _logger.log(INFO, msg, *args)
55
56def sub_warning(msg, *args):
57 if _logger:
58 _logger.log(SUBWARNING, msg, *args)
59
60def get_logger():
61 '''
62 Returns logger used by multiprocessing
63 '''
64 global _logger
Florent Xicluna04842a82011-11-11 20:05:50 +010065 import logging
Benjamin Petersone711caf2008-06-11 16:44:04 +000066
Jesse Noller41faa542009-01-25 03:45:53 +000067 logging._acquireLock()
68 try:
69 if not _logger:
Benjamin Petersone711caf2008-06-11 16:44:04 +000070
Jesse Noller41faa542009-01-25 03:45:53 +000071 _logger = logging.getLogger(LOGGER_NAME)
72 _logger.propagate = 0
Benjamin Petersone711caf2008-06-11 16:44:04 +000073
Jesse Noller41faa542009-01-25 03:45:53 +000074 # XXX multiprocessing should cleanup before logging
75 if hasattr(atexit, 'unregister'):
76 atexit.unregister(_exit_function)
77 atexit.register(_exit_function)
78 else:
79 atexit._exithandlers.remove((_exit_function, (), {}))
80 atexit._exithandlers.append((_exit_function, (), {}))
81
82 finally:
83 logging._releaseLock()
Benjamin Petersone711caf2008-06-11 16:44:04 +000084
85 return _logger
86
Benjamin Petersone711caf2008-06-11 16:44:04 +000087def log_to_stderr(level=None):
88 '''
89 Turn on logging and add a handler which prints to stderr
90 '''
91 global _log_to_stderr
92 import logging
Jesse Noller41faa542009-01-25 03:45:53 +000093
Benjamin Petersone711caf2008-06-11 16:44:04 +000094 logger = get_logger()
95 formatter = logging.Formatter(DEFAULT_LOGGING_FORMAT)
96 handler = logging.StreamHandler()
97 handler.setFormatter(formatter)
98 logger.addHandler(handler)
Jesse Noller41faa542009-01-25 03:45:53 +000099
100 if level:
Benjamin Petersone711caf2008-06-11 16:44:04 +0000101 logger.setLevel(level)
102 _log_to_stderr = True
Jesse Noller41faa542009-01-25 03:45:53 +0000103 return _logger
Benjamin Petersone711caf2008-06-11 16:44:04 +0000104
Pablo Galindo6012f302020-03-09 13:48:01 +0000105
106# Abstract socket support
107
108def _platform_supports_abstract_sockets():
109 if sys.platform == "linux":
110 return True
111 if hasattr(sys, 'getandroidapilevel'):
112 return True
113 return False
114
115
116def is_abstract_socket_namespace(address):
117 if not address:
118 return False
119 if isinstance(address, bytes):
120 return address[0] == 0
121 elif isinstance(address, str):
122 return address[0] == "\0"
123 raise TypeError('address type of {address!r} unrecognized')
124
125
126abstract_sockets_supported = _platform_supports_abstract_sockets()
127
Benjamin Petersone711caf2008-06-11 16:44:04 +0000128#
129# Function returning a temp directory which will be removed on exit
130#
131
Victor Stinner9d405542019-07-04 12:28:55 +0200132def _remove_temp_dir(rmtree, tempdir):
133 rmtree(tempdir)
134
135 current_process = process.current_process()
136 # current_process() can be None if the finalizer is called
137 # late during Python finalization
138 if current_process is not None:
139 current_process._config['tempdir'] = None
140
Benjamin Petersone711caf2008-06-11 16:44:04 +0000141def get_temp_dir():
142 # get name of a temp directory which will be automatically cleaned up
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100143 tempdir = process.current_process()._config.get('tempdir')
144 if tempdir is None:
Benjamin Petersone711caf2008-06-11 16:44:04 +0000145 import shutil, tempfile
146 tempdir = tempfile.mkdtemp(prefix='pymp-')
147 info('created temp directory %s', tempdir)
Victor Stinner9d405542019-07-04 12:28:55 +0200148 # keep a strong reference to shutil.rmtree(), since the finalizer
149 # can be called late during Python shutdown
150 Finalize(None, _remove_temp_dir, args=(shutil.rmtree, tempdir),
151 exitpriority=-100)
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100152 process.current_process()._config['tempdir'] = tempdir
153 return tempdir
Benjamin Petersone711caf2008-06-11 16:44:04 +0000154
155#
156# Support for reinitialization of objects when bootstrapping a child process
157#
158
159_afterfork_registry = weakref.WeakValueDictionary()
160_afterfork_counter = itertools.count()
161
162def _run_after_forkers():
163 items = list(_afterfork_registry.items())
164 items.sort()
165 for (index, ident, func), obj in items:
166 try:
167 func(obj)
168 except Exception as e:
169 info('after forker raised exception %s', e)
170
171def register_after_fork(obj, func):
172 _afterfork_registry[(next(_afterfork_counter), id(obj), func)] = obj
173
174#
175# Finalization using weakrefs
176#
177
178_finalizer_registry = {}
179_finalizer_counter = itertools.count()
180
181
182class Finalize(object):
183 '''
184 Class which supports object finalization using weakrefs
185 '''
186 def __init__(self, obj, callback, args=(), kwargs=None, exitpriority=None):
Allen W. Smith, Ph.Dbd73e722017-08-29 17:52:18 -0500187 if (exitpriority is not None) and not isinstance(exitpriority,int):
188 raise TypeError(
189 "Exitpriority ({0!r}) must be None or int, not {1!s}".format(
190 exitpriority, type(exitpriority)))
Benjamin Petersone711caf2008-06-11 16:44:04 +0000191
192 if obj is not None:
193 self._weakref = weakref.ref(obj, self)
Allen W. Smith, Ph.Dbd73e722017-08-29 17:52:18 -0500194 elif exitpriority is None:
195 raise ValueError("Without object, exitpriority cannot be None")
Benjamin Petersone711caf2008-06-11 16:44:04 +0000196
197 self._callback = callback
198 self._args = args
199 self._kwargs = kwargs or {}
200 self._key = (exitpriority, next(_finalizer_counter))
Richard Oudkerk739ae562012-05-25 13:54:53 +0100201 self._pid = os.getpid()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000202
203 _finalizer_registry[self._key] = self
204
Antoine Pitrou71a28a92011-07-09 01:03:00 +0200205 def __call__(self, wr=None,
206 # Need to bind these locally because the globals can have
207 # been cleared at shutdown
208 _finalizer_registry=_finalizer_registry,
Richard Oudkerkad064442012-06-04 18:58:59 +0100209 sub_debug=sub_debug, getpid=os.getpid):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000210 '''
211 Run the callback unless it has already been called or cancelled
212 '''
213 try:
214 del _finalizer_registry[self._key]
215 except KeyError:
216 sub_debug('finalizer no longer registered')
217 else:
Richard Oudkerkad064442012-06-04 18:58:59 +0100218 if self._pid != getpid():
Richard Oudkerk739ae562012-05-25 13:54:53 +0100219 sub_debug('finalizer ignored because different process')
220 res = None
221 else:
222 sub_debug('finalizer calling %s with args %s and kwargs %s',
223 self._callback, self._args, self._kwargs)
224 res = self._callback(*self._args, **self._kwargs)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000225 self._weakref = self._callback = self._args = \
226 self._kwargs = self._key = None
227 return res
228
229 def cancel(self):
230 '''
231 Cancel finalization of the object
232 '''
233 try:
234 del _finalizer_registry[self._key]
235 except KeyError:
236 pass
237 else:
238 self._weakref = self._callback = self._args = \
239 self._kwargs = self._key = None
240
241 def still_active(self):
242 '''
243 Return whether this finalizer is still waiting to invoke callback
244 '''
245 return self._key in _finalizer_registry
246
247 def __repr__(self):
248 try:
249 obj = self._weakref()
250 except (AttributeError, TypeError):
251 obj = None
252
253 if obj is None:
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300254 return '<%s object, dead>' % self.__class__.__name__
Benjamin Petersone711caf2008-06-11 16:44:04 +0000255
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300256 x = '<%s object, callback=%s' % (
257 self.__class__.__name__,
258 getattr(self._callback, '__name__', self._callback))
Benjamin Petersone711caf2008-06-11 16:44:04 +0000259 if self._args:
260 x += ', args=' + str(self._args)
261 if self._kwargs:
262 x += ', kwargs=' + str(self._kwargs)
263 if self._key[0] is not None:
Min ho Kim39d87b52019-08-31 06:21:19 +1000264 x += ', exitpriority=' + str(self._key[0])
Benjamin Petersone711caf2008-06-11 16:44:04 +0000265 return x + '>'
266
267
268def _run_finalizers(minpriority=None):
269 '''
270 Run all finalizers whose exit priority is not None and at least minpriority
271
272 Finalizers with highest priority are called first; finalizers with
273 the same priority will be called in reverse order of creation.
274 '''
Alexander Belopolskyf36c49d2012-09-09 13:20:58 -0400275 if _finalizer_registry is None:
276 # This function may be called after this module's globals are
277 # destroyed. See the _exit_function function in this module for more
278 # notes.
279 return
Alexander Belopolsky7f704c12012-09-09 13:25:06 -0400280
Benjamin Petersone711caf2008-06-11 16:44:04 +0000281 if minpriority is None:
Antoine Pitrou1eb6c002017-06-13 17:10:39 +0200282 f = lambda p : p[0] is not None
Benjamin Petersone711caf2008-06-11 16:44:04 +0000283 else:
Antoine Pitrou1eb6c002017-06-13 17:10:39 +0200284 f = lambda p : p[0] is not None and p[0] >= minpriority
Benjamin Petersone711caf2008-06-11 16:44:04 +0000285
Antoine Pitrou1eb6c002017-06-13 17:10:39 +0200286 # Careful: _finalizer_registry may be mutated while this function
287 # is running (either by a GC run or by another thread).
Benjamin Petersone711caf2008-06-11 16:44:04 +0000288
Antoine Pitrou1eb6c002017-06-13 17:10:39 +0200289 # list(_finalizer_registry) should be atomic, while
290 # list(_finalizer_registry.items()) is not.
291 keys = [key for key in list(_finalizer_registry) if f(key)]
292 keys.sort(reverse=True)
293
294 for key in keys:
295 finalizer = _finalizer_registry.get(key)
296 # key may have been removed from the registry
297 if finalizer is not None:
298 sub_debug('calling %s', finalizer)
299 try:
300 finalizer()
301 except Exception:
302 import traceback
303 traceback.print_exc()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000304
305 if minpriority is None:
306 _finalizer_registry.clear()
307
308#
309# Clean up on exit
310#
311
312def is_exiting():
313 '''
314 Returns true if the process is shutting down
315 '''
316 return _exiting or _exiting is None
317
318_exiting = False
319
Alexander Belopolskyf36c49d2012-09-09 13:20:58 -0400320def _exit_function(info=info, debug=debug, _run_finalizers=_run_finalizers,
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100321 active_children=process.active_children,
322 current_process=process.current_process):
Alexander Belopolskyf36c49d2012-09-09 13:20:58 -0400323 # We hold on to references to functions in the arglist due to the
324 # situation described below, where this function is called after this
325 # module's globals are destroyed.
326
Benjamin Petersone711caf2008-06-11 16:44:04 +0000327 global _exiting
328
Richard Oudkerk73d9a292012-06-14 15:30:10 +0100329 if not _exiting:
330 _exiting = True
Benjamin Petersone711caf2008-06-11 16:44:04 +0000331
Richard Oudkerk73d9a292012-06-14 15:30:10 +0100332 info('process shutting down')
333 debug('running all "atexit" finalizers with priority >= 0')
334 _run_finalizers(0)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000335
Alexander Belopolskyf36c49d2012-09-09 13:20:58 -0400336 if current_process() is not None:
337 # We check if the current process is None here because if
Andrew Svetlov5b898402012-12-18 21:26:36 +0200338 # it's None, any call to ``active_children()`` will raise
Richard Oudkerke8cd6bb2012-09-13 17:27:15 +0100339 # an AttributeError (active_children winds up trying to
340 # get attributes from util._current_process). One
341 # situation where this can happen is if someone has
342 # manipulated sys.modules, causing this module to be
343 # garbage collected. The destructor for the module type
344 # then replaces all values in the module dict with None.
345 # For instance, after setuptools runs a test it replaces
346 # sys.modules with a copy created earlier. See issues
347 # #9775 and #15881. Also related: #4106, #9205, and
348 # #9207.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000349
Alexander Belopolskyf36c49d2012-09-09 13:20:58 -0400350 for p in active_children():
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100351 if p.daemon:
Alexander Belopolskyf36c49d2012-09-09 13:20:58 -0400352 info('calling terminate() for daemon %s', p.name)
353 p._popen.terminate()
354
355 for p in active_children():
356 info('calling join() for process %s', p.name)
357 p.join()
Richard Oudkerk73d9a292012-06-14 15:30:10 +0100358
359 debug('running the remaining "atexit" finalizers')
360 _run_finalizers()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000361
362atexit.register(_exit_function)
363
364#
365# Some fork aware types
366#
367
368class ForkAwareThreadLock(object):
369 def __init__(self):
370 self._lock = threading.Lock()
371 self.acquire = self._lock.acquire
372 self.release = self._lock.release
Dong-hee Nae1945302020-04-14 22:15:52 +0900373 register_after_fork(self, ForkAwareThreadLock._at_fork_reinit)
374
375 def _at_fork_reinit(self):
376 self._lock._at_fork_reinit()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000377
Charles-François Natalia924fc72014-05-25 14:12:12 +0100378 def __enter__(self):
379 return self._lock.__enter__()
380
381 def __exit__(self, *args):
382 return self._lock.__exit__(*args)
383
384
Benjamin Petersone711caf2008-06-11 16:44:04 +0000385class ForkAwareLocal(threading.local):
386 def __init__(self):
387 register_after_fork(self, lambda obj : obj.__dict__.clear())
388 def __reduce__(self):
389 return type(self), ()
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100390
391#
392# Close fds except those specified
393#
394
395try:
396 MAXFD = os.sysconf("SC_OPEN_MAX")
397except Exception:
398 MAXFD = 256
399
400def close_all_fds_except(fds):
401 fds = list(fds) + [-1, MAXFD]
402 fds.sort()
403 assert fds[-1] == MAXFD, 'fd too large'
404 for i in range(len(fds) - 1):
405 os.closerange(fds[i]+1, fds[i+1])
Victor Stinnera6d865c2016-03-25 09:29:50 +0100406#
407# Close sys.stdin and replace stdin with os.devnull
408#
409
410def _close_stdin():
411 if sys.stdin is None:
412 return
413
414 try:
415 sys.stdin.close()
416 except (OSError, ValueError):
417 pass
418
419 try:
420 fd = os.open(os.devnull, os.O_RDONLY)
421 try:
Inada Naoki80017752021-04-02 09:01:57 +0900422 sys.stdin = open(fd, encoding="utf-8", closefd=False)
Victor Stinnera6d865c2016-03-25 09:29:50 +0100423 except:
424 os.close(fd)
425 raise
426 except (OSError, ValueError):
427 pass
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100428
429#
Antoine Pitroue756f662018-03-11 19:21:38 +0100430# Flush standard streams, if any
431#
432
433def _flush_std_streams():
434 try:
435 sys.stdout.flush()
436 except (AttributeError, ValueError):
437 pass
438 try:
439 sys.stderr.flush()
440 except (AttributeError, ValueError):
441 pass
442
443#
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100444# Start a program with only specified fds kept open
445#
446
447def spawnv_passfds(path, args, passfds):
Victor Stinner67973c02013-08-28 12:21:47 +0200448 import _posixsubprocess
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300449 passfds = tuple(sorted(map(int, passfds)))
Victor Stinnerdaf45552013-08-28 00:53:59 +0200450 errpipe_read, errpipe_write = os.pipe()
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100451 try:
452 return _posixsubprocess.fork_exec(
453 args, [os.fsencode(path)], True, passfds, None, None,
454 -1, -1, -1, -1, -1, -1, errpipe_read, errpipe_write,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700455 False, False, None, None, None, -1, None)
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100456 finally:
457 os.close(errpipe_read)
458 os.close(errpipe_write)
Thomas Moreauc09a9f52019-05-20 21:37:05 +0200459
460
461def close_fds(*fds):
462 """Close each file descriptor given as an argument"""
463 for fd in fds:
464 os.close(fd)
Victor Stinner9707e8e2019-12-17 18:37:26 +0100465
466
467def _cleanup_tests():
468 """Cleanup multiprocessing resources when multiprocessing tests
469 completed."""
470
471 from test import support
472
473 # cleanup multiprocessing
474 process._cleanup()
475
476 # Stop the ForkServer process if it's running
477 from multiprocessing import forkserver
478 forkserver._forkserver._stop()
479
480 # Stop the ResourceTracker process if it's running
481 from multiprocessing import resource_tracker
482 resource_tracker._resource_tracker._stop()
483
484 # bpo-37421: Explicitly call _run_finalizers() to remove immediately
485 # temporary directories created by multiprocessing.util.get_temp_dir().
486 _run_finalizers()
487 support.gc_collect()
488
489 support.reap_children()