Fix flaky moto_server fixture by using an OS-assigned ephemeral port - #3759
Open
puchengy wants to merge 1 commit into
Open
Fix flaky moto_server fixture by using an OS-assigned ephemeral port#3759puchengy wants to merge 1 commit into
puchengy wants to merge 1 commit into
Conversation
The `moto_server` session fixture hardcoded port 5001 and pre-bound a socket to it purely to detect conflicts (added in apache#292). This is still flaky: when tests run in parallel (e.g. multiple CI jobs on a shared runner) or when a previous run leaves the port in TIME_WAIT, the pre-bind raises `OSError: [Errno 98] Address already in use`, failing the whole test session at fixture setup. Bind to port 0 instead so the OS assigns a free ephemeral port, and read the actual bound port back via moto's `get_host_and_port()`. This removes the collision and the now-unnecessary pre-bind check (and the `socket` import). The fixture return annotation is corrected to `Generator[...]` since it yields. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
The
moto_serversession fixture intests/conftest.pyhardcodes port5001and pre-binds a socket to it purely to detect conflicts (added in #292). This is still flaky: when the test session runs on a shared/parallel CI runner, or when a previous run left the port inTIME_WAIT, the pre-bind raises and fails the entire test session at fixture setup:Bumping the port (as #292 did, 5000 → 5001) and failing fast doesn't remove the collision — it only surfaces it sooner.
This change binds to port
0so the OS assigns a free ephemeral port, then reads the actual bound port back via moto'sget_host_and_port(). That eliminates the collision entirely and removes the now-unnecessary pre-bind check (and thesocketimport). The fixture's return annotation is also corrected toGenerator[...]since ityields.Are these changes tested?
Yes — existing moto-backed tests continue to pass against the new fixture (e.g.
tests/catalog/test_glue.pyS3 tests, which consumemoto_endpoint_url). Verified locally thatThreadedMotoServer(port=0)starts,get_host_and_port()returns a real ephemeral port, serves requests, and stops cleanly.ruff check tests/conftest.pypasses.Are there any user-facing changes?
No. Test-infrastructure only.