Skip to content

Commit cd023a7

Browse files
asvetlovclaude
andauthored
gh-162931: Fix slow asyncio connection-refused tests on macOS (#162990)
Apple platforms drop connections to a bound but non-listening port, so connect() only fails after a long timeout instead of being refused. Release the reserved port before connecting there. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 31146df commit cd023a7

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎Lib/test/test_asyncio/test_events.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,14 @@ def test_create_connection_local_addr(self):
816816
def test_create_connection_local_addr_skip_different_family(self):
817817
# See https://github.com/python/cpython/issues/86508
818818
# A bound but non-listening socket reserves a port that refuses
819-
# connections (macOS times them out immediately instead);
820-
# find_unused_port() could be grabbed by another test.
819+
# connections; find_unused_port() could be grabbed by another test.
821820
with socket.socket() as sock:
822821
sock.bind(('127.0.0.1', 0))
823822
port = sock.getsockname()[1]
823+
if support.is_apple:
824+
# Apple platforms drop connections to a bound but
825+
# non-listening port, so connect() would only time out.
826+
sock.close()
824827
getaddrinfo_orig = self.loop.getaddrinfo
825828

826829
async def getaddrinfo(host, port, *args, **kwargs):
@@ -835,7 +838,7 @@ async def getaddrinfo(host, port, *args, **kwargs):
835838
lambda: MyProto(loop=self.loop),
836839
'127.0.0.1', port, local_addr=('localhost', 0))
837840

838-
with self.assertRaises((ConnectionRefusedError, TimeoutError)):
841+
with self.assertRaises(ConnectionRefusedError):
839842
self.loop.run_until_complete(f)
840843

841844
@socket_helper.skip_if_tcp_blackhole

‎Lib/test/test_asyncio/test_sock_lowlevel.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,15 +531,18 @@ def test_unix_sock_client_ops(self):
531531

532532
def test_sock_client_fail(self):
533533
# Keep the port bound but not listening so that no other process
534-
# can take it and connections to it are refused (macOS times them
535-
# out immediately instead).
534+
# can take it and connections to it are refused.
536535
with socket.socket() as s:
537536
s.bind(('127.0.0.1', 0))
538537
address = s.getsockname()
538+
if support.is_apple:
539+
# Apple platforms drop connections to a bound but
540+
# non-listening port, so connect() would only time out.
541+
s.close()
539542

540543
sock = socket.socket()
541544
sock.setblocking(False)
542-
with self.assertRaises((ConnectionRefusedError, TimeoutError)):
545+
with self.assertRaises(ConnectionRefusedError):
543546
self.loop.run_until_complete(
544547
self.loop.sock_connect(sock, address))
545548
sock.close()

‎Lib/test/test_asyncio/test_streams.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,11 @@ def test_open_connection_happy_eyeball_refcycles(self):
13701370
sock6.bind((socket_helper.HOSTv6, port))
13711371
except OSError:
13721372
self.skipTest(f'cannot bind {socket_helper.HOSTv6} port {port}')
1373+
if support.is_apple:
1374+
# Apple platforms drop connections to a bound but non-listening
1375+
# port, so connect() would only time out.
1376+
for s in reserved:
1377+
s.close()
13731378

13741379
async def main():
13751380
exc = None

0 commit comments

Comments
 (0)