diff -r f9a73831f9e7 Lib/asyncio/base_events.py --- a/Lib/asyncio/base_events.py Fri Apr 04 17:17:36 2014 +0200 +++ b/Lib/asyncio/base_events.py Fri Apr 04 18:50:30 2014 +0200 @@ -512,6 +512,10 @@ class BaseEventLoop(events.AbstractEvent if sock is not None: sock.close() exceptions.append(exc) + except: + if sock is not None: + sock.close() + raise else: break else: diff -r f9a73831f9e7 Lib/asyncio/unix_events.py --- a/Lib/asyncio/unix_events.py Fri Apr 04 17:17:36 2014 +0200 +++ b/Lib/asyncio/unix_events.py Fri Apr 04 18:50:30 2014 +0200 @@ -219,6 +223,9 @@ class _UnixSelectorEventLoop(selector_ev raise OSError(errno.EADDRINUSE, msg) from None else: raise + except: + sock.close() + raise else: if sock is None: raise ValueError( diff -r f9a73831f9e7 Lib/asyncio/windows_utils.py --- a/Lib/asyncio/windows_utils.py Fri Apr 04 17:17:36 2014 +0200 +++ b/Lib/asyncio/windows_utils.py Fri Apr 04 18:50:30 2014 +0200 @@ -51,23 +51,23 @@ def socketpair(family=socket.AF_INET, ty # We create a connected TCP socket. Note the trick with setblocking(0) # that prevents us from having to create a thread. lsock = socket.socket(family, type, proto) - lsock.bind((host, 0)) - lsock.listen(1) - # On IPv6, ignore flow_info and scope_id - addr, port = lsock.getsockname()[:2] - csock = socket.socket(family, type, proto) - csock.setblocking(False) - try: - csock.connect((addr, port)) - except (BlockingIOError, InterruptedError): - pass - except Exception: - lsock.close() - csock.close() - raise - ssock, _ = lsock.accept() - csock.setblocking(True) - lsock.close() + with lsock: + lsock.bind((host, 0)) + lsock.listen(1) + # On IPv6, ignore flow_info and scope_id + addr, port = lsock.getsockname()[:2] + csock = socket.socket(family, type, proto) + try: + csock.setblocking(False) + try: + csock.connect((addr, port)) + except (BlockingIOError, InterruptedError): + pass + ssock, _ = lsock.accept() + csock.setblocking(True) + except: + csock.close() + raise return (ssock, csock)