[Python-checkins] cpython (3.4): Issue #21119: asyncio now closes sockets on errors

victor.stinner python-checkins at python.org
Wed Jun 4 00:16:54 CEST 2014


http://hg.python.org/cpython/rev/d0dd3eb5b5ef
changeset:   91000:d0dd3eb5b5ef
branch:      3.4
parent:      90997:27e1b4a9de07
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Jun 04 00:11:52 2014 +0200
summary:
  Issue #21119: asyncio now closes sockets on errors

Fix ResourceWarning: create_connection(), create_datagram_endpoint() and
create_unix_server() methods of event loop now close the newly created socket
on error.

files:
  Lib/asyncio/base_events.py                |   8 ++++
  Lib/asyncio/unix_events.py                |   3 +
  Lib/test/test_asyncio/test_base_events.py |  21 +++++++++++
  Lib/test/test_asyncio/test_unix_events.py |  18 +++++++++
  4 files changed, 50 insertions(+), 0 deletions(-)


diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -412,6 +412,10 @@
                     if sock is not None:
                         sock.close()
                     exceptions.append(exc)
+                except:
+                    if sock is not None:
+                        sock.close()
+                    raise
                 else:
                     break
             else:
@@ -512,6 +516,10 @@
                 if sock is not None:
                     sock.close()
                 exceptions.append(exc)
+            except:
+                if sock is not None:
+                    sock.close()
+                raise
             else:
                 break
         else:
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -223,6 +223,9 @@
                     raise OSError(errno.EADDRINUSE, msg) from None
                 else:
                     raise
+            except:
+                sock.close()
+                raise
         else:
             if sock is None:
                 raise ValueError(
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -583,6 +583,27 @@
 
         self.assertEqual(str(cm.exception), 'Multiple exceptions: err1, err2')
 
+    @mock.patch('asyncio.base_events.socket')
+    def test_create_connection_timeout(self, m_socket):
+        # Ensure that the socket is closed on timeout
+        sock = mock.Mock()
+        m_socket.socket.return_value = sock
+
+        def getaddrinfo(*args, **kw):
+            fut = asyncio.Future(loop=self.loop)
+            addr = (socket.AF_INET, socket.SOCK_STREAM, 0, '',
+                    ('127.0.0.1', 80))
+            fut.set_result([addr])
+            return fut
+        self.loop.getaddrinfo = getaddrinfo
+
+        with mock.patch.object(self.loop, 'sock_connect',
+                               side_effect=asyncio.TimeoutError):
+            coro = self.loop.create_connection(MyProto, '127.0.0.1', 80)
+            with self.assertRaises(asyncio.TimeoutError) as cm:
+                self.loop.run_until_complete(coro)
+            self.assertTrue(sock.close.called)
+
     def test_create_connection_host_port_sock(self):
         coro = self.loop.create_connection(
             MyProto, 'example.com', 80, sock=object())
diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py
--- a/Lib/test/test_asyncio/test_unix_events.py
+++ b/Lib/test/test_asyncio/test_unix_events.py
@@ -256,6 +256,24 @@
                                         'A UNIX Domain Socket was expected'):
                 self.loop.run_until_complete(coro)
 
+    @mock.patch('asyncio.unix_events.socket')
+    def test_create_unix_server_bind_error(self, m_socket):
+        # Ensure that the socket is closed on any bind error
+        sock = mock.Mock()
+        m_socket.socket.return_value = sock
+
+        sock.bind.side_effect = OSError
+        coro = self.loop.create_unix_server(lambda: None, path="/test")
+        with self.assertRaises(OSError):
+            self.loop.run_until_complete(coro)
+        self.assertTrue(sock.close.called)
+
+        sock.bind.side_effect = MemoryError
+        coro = self.loop.create_unix_server(lambda: None, path="/test")
+        with self.assertRaises(MemoryError):
+            self.loop.run_until_complete(coro)
+        self.assertTrue(sock.close.called)
+
     def test_create_unix_connection_path_sock(self):
         coro = self.loop.create_unix_connection(
             lambda: None, '/dev/null', sock=object())

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list