Skip to content

gh-162606: Avoid find_unused_port() races in asyncio tests - #162607

Merged
asvetlov merged 4 commits into
python:mainfrom
asvetlov:claude/cpython-ci-job-failure-2d136b
Nov 25, 2026
Merged

asvetlov merged 4 commits into
python:mainfrom
asvetlov:claude/cpython-ci-job-failure-2d136b

Conversation

@asvetlov

@asvetlov asvetlov commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Several asyncio tests picked a port with find_unused_port() and used it later, so a parallel test could take it in between.

  • Tests that expect a refused connection keep a bound, non-listening socket open for the whole test.
  • Servers bind to port 0 instead of a pre-picked port.
  • test_create_connection_local_addr retries on EADDRINUSE.

🤖 Generated with Claude Code

asvetlov and others added 2 commits November 25, 2026 07:03
test_create_connection_local_addr_skip_different_family and
test_sock_client_fail expected a connect to an "unused" port to be
refused, but the port was not reserved, so a parallel test could start
listening on it and the connect would succeed.  Keep a bound,
non-listening socket open for the duration of each test instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bind servers to port 0 instead of picking a port first and binding it
later, retry test_create_connection_local_addr on EADDRINUSE, and keep
the port of test_open_connection_happy_eyeball_refcycles reserved on
both loopback addresses so the connection is reliably refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@asvetlov asvetlov added type-bug An unexpected behavior, bug, or error topic-asyncio skip news needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 25, 2026
@asvetlov asvetlov linked an issue Nov 25, 2026 that may be closed by this pull request
macOS and iOS fail a connect to a bound but non-listening port with
ETIMEDOUT immediately instead of ECONNREFUSED.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@asvetlov
asvetlov merged commit 4b66b87 into python:main Nov 25, 2026
53 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @asvetlov for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Nov 25, 2026

Copy link
Copy Markdown

GH-162619 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 25, 2026
@bedevere-app

bedevere-app Bot commented Nov 25, 2026

Copy link
Copy Markdown

GH-162620 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 25, 2026
asvetlov added a commit that referenced this pull request Nov 25, 2026
…-162607) (#162620)

gh-162606: Avoid find_unused_port() races in asyncio tests (GH-162607)

* gh-162606: Fix port races in asyncio connection-refused tests

test_create_connection_local_addr_skip_different_family and
test_sock_client_fail expected a connect to an "unused" port to be
refused, but the port was not reserved, so a parallel test could start
listening on it and the connect would succeed.  Keep a bound,
non-listening socket open for the duration of each test instead.



* gh-162606: Avoid find_unused_port() races in more asyncio tests

Bind servers to port 0 instead of picking a port first and binding it
later, retry test_create_connection_local_addr on EADDRINUSE, and keep
the port of test_open_connection_happy_eyeball_refcycles reserved on
both loopback addresses so the connection is reliably refused.



* gh-162606: Remove unused import in test_proactor_events



* gh-162606: Accept TimeoutError for non-listening ports on macOS

macOS and iOS fail a connect to a bound but non-listening port with
ETIMEDOUT immediately instead of ECONNREFUSED.



---------
(cherry picked from commit 4b66b87)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
asvetlov added a commit that referenced this pull request Nov 25, 2026
…-162607) (#162619)

gh-162606: Avoid find_unused_port() races in asyncio tests (GH-162607)

* gh-162606: Fix port races in asyncio connection-refused tests

test_create_connection_local_addr_skip_different_family and
test_sock_client_fail expected a connect to an "unused" port to be
refused, but the port was not reserved, so a parallel test could start
listening on it and the connect would succeed.  Keep a bound,
non-listening socket open for the duration of each test instead.



* gh-162606: Avoid find_unused_port() races in more asyncio tests

Bind servers to port 0 instead of picking a port first and binding it
later, retry test_create_connection_local_addr on EADDRINUSE, and keep
the port of test_open_connection_happy_eyeball_refcycles reserved on
both loopback addresses so the connection is reliably refused.



* gh-162606: Remove unused import in test_proactor_events



* gh-162606: Accept TimeoutError for non-listening ports on macOS

macOS and iOS fail a connect to a bound but non-listening port with
ETIMEDOUT immediately instead of ECONNREFUSED.



---------
(cherry picked from commit 4b66b87)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news tests Tests in the Lib/test dir topic-asyncio type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get rid of find_unused_port() in asyncio tests

1 participant