summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-07-15 20:12:24 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-07-15 20:12:24 (GMT)
commitc081c0c6a0c917de72b7d7944c5316174717d56d (patch)
tree184657849382db7dec5904be11d3d44895f6a16c /Lib
parentb8298a01e6fefec9b0b64fd6915e57fc246b9a55 (diff)
downloadcpython-c081c0c6a0c917de72b7d7944c5316174717d56d.zip
cpython-c081c0c6a0c917de72b7d7944c5316174717d56d.tar.gz
cpython-c081c0c6a0c917de72b7d7944c5316174717d56d.tar.bz2
Issue #12573: Add resource checks for dangling Thread and Process objects.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/multiprocessing/process.py5
-rwxr-xr-xLib/test/regrtest.py37
-rw-r--r--Lib/threading.py4
3 files changed, 45 insertions, 1 deletions
diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py
index 941893f..5987af9 100644
--- a/Lib/multiprocessing/process.py
+++ b/Lib/multiprocessing/process.py
@@ -42,6 +42,7 @@ import os
import sys
import signal
import itertools
+from _weakrefset import WeakSet
#
#
@@ -105,6 +106,7 @@ class Process(object):
self._kwargs = dict(kwargs)
self._name = name or type(self).__name__ + '-' + \
':'.join(str(i) for i in self._identity)
+ _dangling.add(self)
def run(self):
'''
@@ -328,3 +330,6 @@ _exitcode_to_name = {}
for name, signum in list(signal.__dict__.items()):
if name[:3]=='SIG' and '_' not in name:
_exitcode_to_name[-signum] = name
+
+# For debug and leak testing
+_dangling = WeakSet()
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index 14fa005..8ca7f6c 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -172,6 +172,15 @@ import unittest
import warnings
from inspect import isabstract
+try:
+ import threading
+except ImportError:
+ threading = None
+try:
+ import multiprocessing.process
+except ImportError:
+ multiprocessing = None
+
# Some times __path__ and __file__ are not absolute (e.g. while running from
# Lib/) and, if we change the CWD to run the tests in a temporary dir, some
@@ -864,7 +873,8 @@ class saved_test_environment:
'os.environ', 'sys.path', 'sys.path_hooks', '__import__',
'warnings.filters', 'asyncore.socket_map',
'logging._handlers', 'logging._handlerList',
- 'sys.warnoptions')
+ 'sys.warnoptions', 'threading._dangling',
+ 'multiprocessing.process._dangling')
def get_sys_argv(self):
return id(sys.argv), sys.argv, sys.argv[:]
@@ -952,6 +962,31 @@ class saved_test_environment:
sys.warnoptions = saved_options[1]
sys.warnoptions[:] = saved_options[2]
+ # Controlling dangling references to Thread objects can make it easier
+ # to track reference leaks.
+ def get_threading__dangling(self):
+ if not threading:
+ return None
+ # This copies the weakrefs without making any strong reference
+ return threading._dangling.copy()
+ def restore_threading__dangling(self, saved):
+ if not threading:
+ return
+ threading._dangling.clear()
+ threading._dangling.update(saved)
+
+ # Same for Process objects
+ def get_multiprocessing_process__dangling(self):
+ if not multiprocessing:
+ return None
+ # This copies the weakrefs without making any strong reference
+ return multiprocessing.process._dangling.copy()
+ def restore_multiprocessing_process__dangling(self, saved):
+ if not multiprocessing:
+ return
+ multiprocessing.process._dangling.clear()
+ multiprocessing.process._dangling.update(saved)
+
def resource_info(self):
for name in self.resources:
method_suffix = name.replace('.', '_')
diff --git a/Lib/threading.py b/Lib/threading.py
index fd93f74..d260983 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -6,6 +6,7 @@ import _thread
from time import time as _time, sleep as _sleep
from traceback import format_exc as _format_exc
from collections import deque
+from _weakrefset import WeakSet
# Note regarding PEP 8 compliant names
# This threading model was originally inspired by Java, and inherited
@@ -606,6 +607,8 @@ _active_limbo_lock = _allocate_lock()
_active = {} # maps thread id to Thread object
_limbo = {}
+# For debug and leak testing
+_dangling = WeakSet()
# Main class for threads
@@ -640,6 +643,7 @@ class Thread(_Verbose):
# sys.stderr is not stored in the class like
# sys.exc_info since it can be changed between instances
self._stderr = _sys.stderr
+ _dangling.add(self)
def _reset_internal_locks(self):
# private! Called by _after_fork() to reset our internal locks as