tests: resolve TOCTOU port race conditions for streamable-HTTP/SSE tests#2705
Open
Ar-maan05 wants to merge 1 commit into
Open
tests: resolve TOCTOU port race conditions for streamable-HTTP/SSE tests#2705Ar-maan05 wants to merge 1 commit into
Ar-maan05 wants to merge 1 commit into
Conversation
2 tasks
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.
This PR resolves intermittent test failures and flakiness in the CI matrix (specifically under
pytest -n autoparallelism) caused by a time-of-check/time-of-use (TOCTOU) port race when allocating ports for test servers.Closes #2704
Root Cause
Previously, test server port fixtures bound to 127.0.0.1:0 to find a free port, then closed the socket immediately to return the port number. Between this allocation and the server starting in a separate multiprocessing.Process to bind that port, another parallel worker could be assigned the same port. This caused two primary issues:
OSError: [Errno 98] Address already in use(failing to start the server)Solution
I converted the server processes to bind their socket ephemerally (
127.0.0.1:0) and calledlisten()inside the child process itself. The socket remains open and in a listening state throughout the process lifecycle. The child process reports its dynamically allocated port back to the parent process using a unidirectionalmultiprocessing.Pipe.• This fully closes the TOCTOU gap because the socket is never closed between check and use.
• It removes the need for
wait_for_serverpolling (since the socket is already listening before the fixture yields).• It remains fully cross-platform compatible as it does not require inheriting or pickling socket descriptors across process boundaries (only the pipe connection and integers are sent across process boundaries).
Affected Files Migrated
• tests/shared/test_streamable_http.py
• tests/shared/test_sse.py
• tests/server/test_sse_security.py
• tests/server/test_streamable_http_security.py
• tests/client/test_http_unicode.py
Other Cleanups
• Added a reusable
running_servercontext manager intests/test_helpers.py.• Removed the unused
wait_for_serverhelper fromtests/test_helpers.pyto maintain 100% test coverage.• Updated affected test cases to consume these fixtures directly as URL strings, avoiding unnecessary server startups.
Verification
Automated Tests
• Result:
1271 passed(1172 passed, 98 skipped, 1 xfailed).Linting & Formatting
• Result:
0 errors, 0 warnings.Test Coverage
• Result:
100.00% Coverage(19,675 statements, 0 missed).