Skip to content

Commit eb25938

Browse files
miss-islingtonasvetlovclaude
authored
[3.15] gh-162931: Fix slow asyncio connection-refused tests on macOS (GH-162990) (#162995)
gh-162931: Fix slow asyncio connection-refused tests on macOS (GH-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. (cherry picked from commit cd023a7) Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cc6be88 commit eb25938

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
@@ -815,11 +815,14 @@ def test_create_connection_local_addr(self):
815815
def test_create_connection_local_addr_skip_different_family(self):
816816
# See https://github.com/python/cpython/issues/86508
817817
# A bound but non-listening socket reserves a port that refuses
818-
# connections (macOS times them out immediately instead);
819-
# find_unused_port() could be grabbed by another test.
818+
# connections; find_unused_port() could be grabbed by another test.
820819
with socket.socket() as sock:
821820
sock.bind(('127.0.0.1', 0))
822821
port = sock.getsockname()[1]
822+
if support.is_apple:
823+
# Apple platforms drop connections to a bound but
824+
# non-listening port, so connect() would only time out.
825+
sock.close()
823826
getaddrinfo_orig = self.loop.getaddrinfo
824827

825828
async def getaddrinfo(host, port, *args, **kwargs):
@@ -834,7 +837,7 @@ async def getaddrinfo(host, port, *args, **kwargs):
834837
lambda: MyProto(loop=self.loop),
835838
'127.0.0.1', port, local_addr=('localhost', 0))
836839

837-
with self.assertRaises((ConnectionRefusedError, TimeoutError)):
840+
with self.assertRaises(ConnectionRefusedError):
838841
self.loop.run_until_complete(f)
839842

840843
@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
@@ -1362,6 +1362,11 @@ def test_open_connection_happy_eyeball_refcycles(self):
13621362
sock6.bind((socket_helper.HOSTv6, port))
13631363
except OSError:
13641364
self.skipTest(f'cannot bind {socket_helper.HOSTv6} port {port}')
1365+
if support.is_apple:
1366+
# Apple platforms drop connections to a bound but non-listening
1367+
# port, so connect() would only time out.
1368+
for s in reserved:
1369+
s.close()
13651370

13661371
async def main():
13671372
exc = None

0 commit comments

Comments
 (0)