Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,32 @@ def client():
# let it close
self.loop.run_until_complete(asyncio.sleep(0.1))

def test_create_connection_sock_cancel_detaches(self):
async def client(addr):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(False)
try:
sock.connect(addr)
except BlockingIOError:
pass
await asyncio.sleep(0.01)

task = asyncio.ensure_future(
self.loop.create_connection(asyncio.Protocol, sock=sock))
await asyncio.sleep(0)
task.cancel()
with self.assertRaises(asyncio.CancelledError):
await task

# After cancellation the socket must be detached (fd == -1)
# so that its __del__ won't close a recycled fd.
self.assertEqual(sock.fileno(), -1)

with self.tcp_server(lambda sock: sock.recv_all(1),
max_clients=1,
backlog=1) as srv:
self.loop.run_until_complete(client(srv.addr))

@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
def test_create_connection_wrong_sock(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Expand Down
2 changes: 2 additions & 0 deletions uvloop/loop.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,7 @@ cdef class Loop:
# up in `Transport._call_connection_made()`, and calling
# `_close()` before it is fine.
tr._close()
sock.detach()
raise

tr._attach_fileobj(sock)
Expand Down Expand Up @@ -2307,6 +2308,7 @@ cdef class Loop:
raise
except BaseException:
tr._close()
sock.detach()
raise

tr._attach_fileobj(sock)
Expand Down
Loading