Issue #23243, asyncio: Emit a ResourceWarning when an event loop or a transport

is not explicitly closed. Close also explicitly transports in test_sslproto.
This commit is contained in:
Victor Stinner 2015-01-29 17:50:58 +01:00
parent 3c0cf05901
commit 978a9afc6a
10 changed files with 104 additions and 10 deletions

View file

@ -1,5 +1,7 @@
import collections
import subprocess
import sys
import warnings
from . import protocols
from . import transports
@ -13,6 +15,7 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
stdin, stdout, stderr, bufsize,
extra=None, **kwargs):
super().__init__(extra)
self._closed = False
self._protocol = protocol
self._loop = loop
self._pid = None
@ -40,7 +43,10 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
program, self._pid)
def __repr__(self):
info = [self.__class__.__name__, 'pid=%s' % self._pid]
info = [self.__class__.__name__]
if self._closed:
info.append('closed')
info.append('pid=%s' % self._pid)
if self._returncode is not None:
info.append('returncode=%s' % self._returncode)
@ -70,6 +76,7 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
raise NotImplementedError
def close(self):
self._closed = True
for proto in self._pipes.values():
if proto is None:
continue
@ -77,6 +84,15 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
if self._returncode is None:
self.terminate()
# On Python 3.3 and older, objects with a destructor part of a reference
# cycle are never destroyed. It's not more the case on Python 3.4 thanks
# to the PEP 442.
if sys.version_info >= (3, 4):
def __del__(self):
if not self._closed:
warnings.warn("unclosed transport %r" % self, ResourceWarning)
self.close()
def get_pid(self):
return self._pid
@ -104,6 +120,7 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
Function called when an exception is raised during the creation
of a subprocess.
"""
self._closed = True
if self._loop.get_debug():
logger.warning('Exception during subprocess creation, '
'kill the subprocess %r',