mirror of
https://github.com/python/cpython.git
synced 2025-11-01 06:01:29 +00:00
Issue #21119: asyncio: Make sure that socketpair() close sockets on error
Close the listening socket if sock.bind() raises an exception.
This commit is contained in:
parent
223a624158
commit
a9fa2664ab
2 changed files with 26 additions and 15 deletions
|
|
@ -51,23 +51,25 @@ def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
|
||||||
# We create a connected TCP socket. Note the trick with setblocking(0)
|
# We create a connected TCP socket. Note the trick with setblocking(0)
|
||||||
# that prevents us from having to create a thread.
|
# that prevents us from having to create a thread.
|
||||||
lsock = socket.socket(family, type, proto)
|
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:
|
try:
|
||||||
csock.connect((addr, port))
|
lsock.bind((host, 0))
|
||||||
except (BlockingIOError, InterruptedError):
|
lsock.listen(1)
|
||||||
pass
|
# On IPv6, ignore flow_info and scope_id
|
||||||
except Exception:
|
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
|
||||||
|
finally:
|
||||||
lsock.close()
|
lsock.close()
|
||||||
csock.close()
|
|
||||||
raise
|
|
||||||
ssock, _ = lsock.accept()
|
|
||||||
csock.setblocking(True)
|
|
||||||
lsock.close()
|
|
||||||
return (ssock, csock)
|
return (ssock, csock)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,15 @@ def test_winsocketpair_invalid_args(self):
|
||||||
self.assertRaises(ValueError,
|
self.assertRaises(ValueError,
|
||||||
windows_utils.socketpair, proto=1)
|
windows_utils.socketpair, proto=1)
|
||||||
|
|
||||||
|
@mock.patch('asyncio.windows_utils.socket')
|
||||||
|
def test_winsocketpair_close(self, m_socket):
|
||||||
|
m_socket.AF_INET = socket.AF_INET
|
||||||
|
m_socket.SOCK_STREAM = socket.SOCK_STREAM
|
||||||
|
sock = mock.Mock()
|
||||||
|
m_socket.socket.return_value = sock
|
||||||
|
sock.bind.side_effect = OSError
|
||||||
|
self.assertRaises(OSError, windows_utils.socketpair)
|
||||||
|
self.assertTrue(sock.close.called)
|
||||||
|
|
||||||
|
|
||||||
class PipeTests(unittest.TestCase):
|
class PipeTests(unittest.TestCase):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue