Skip to content

test(apps): hold the port the retry test targets - #800

Open
deanq wants to merge 1 commit into
mainfrom
deanq/SLS-1129-flaky-rest-test
Open

deanq wants to merge 1 commit into
mainfrom
deanq/SLS-1129-flaky-rest-test

Conversation

@deanq

@deanq deanq commented Dec 3, 2026

Copy link
Copy Markdown
Member

Summary

tests/test_api/test_rest.py::test_error_body_fallbacks_do_not_mask_http_failure fails now and then with assert 2 == 1: its test server counts one more request than the test sent.

The extra request comes from tests/test_apps/test_retry.py::test_connection_error_retried. That test sends its GET retries to a port from pytest-asyncio's unused_tcp_port, which binds a port, closes it, and returns the number. The port is free again before the test runs. When another pytest process runs at the same time (parallel worktrees, or a shared CI runner), its test_rest server can bind that port and receive a retry. The test_rest handler counts every request before checking the path, so the stray GET shows up as a second hit.

Change

  • New resetting_tcp_port fixture in tests/test_apps/conftest.py. A listener holds the port for the whole test, and a background thread accepts each connection and closes it with SO_LINGER 0, which sends a TCP reset. The retries still fail at the connection level (aiohttp raises a ClientConnectionError subclass), and no other process can bind the port while the test runs.
  • test_connection_error_retried uses the new fixture. It was the only user of unused_tcp_port.

A socket that is bound but never listens would be simpler, but on macOS connects to it time out instead of being refused (about 8s per attempt), so it is not used.

Only test code changes.

Tests

  • test_resetting_port_resets_connections: a connect to the port gets a reset quickly.
  • test_resetting_port_cannot_be_taken_by_another_server: binding a ThreadingHTTPServer to the port raises OSError.
  • With the old bind, close and return behavior, both new tests fail; with the new fixture they pass.
  • Reproduced the original failure by sending one stray GET to a test_rest server during the test.
  • make quality-check: 4435 passed, 8 skipped.

Copilot AI balanced review requested due to automatic review settings December 3, 2026 15:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The test-port reservation changes have no active review issues.

No open issues 🎉

What changed in this PR

Prevents retry tests from sending stray requests to parallel test servers by keeping the target port reserved throughout each test.

Changes:

  • Adds a fixture that holds a listening port and resets incoming connections.
  • Updates the retry test to use the fixture.
  • Adds checks for connection resets and exclusive port ownership.
File Description
tests/​test_apps/​test_retry.py Uses the reserved port and adds fixture regression tests.
tests/​test_apps/​conftest.py Adds the connection-resetting listener fixture and cleanup.

Review effort: Balanced

@deanq
deanq force-pushed the deanq/SLS-1129-flaky-rest-test branch 5 times, most recently from c9a08b4 to 9280245 Compare December 4, 2026 17:04
test_connection_error_retried sent its GET retries to a port from
unused_tcp_port, which pytest-asyncio releases before the test runs.
Another pytest process running in parallel could bind that port for
its own test server and count the stray request, which made
tests/test_api/test_rest.py fail intermittently with an extra hit.

Add a resetting_tcp_port fixture that keeps the port held by a
listener for the whole test and resets every connection, so the
retries still fail at the connection level and no other server can
take the port. A bound socket that never listens does not work on
macOS, where connects to it time out instead of being refused.
@deanq
deanq force-pushed the deanq/SLS-1129-flaky-rest-test branch from 9280245 to 5cd8e76 Compare December 4, 2026 20:20
@deanq
deanq requested review from a team, KAJdev and justinwlin December 4, 2026 22:02
@justinwlin

justinwlin commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Kimi review posted: #800 (review)

How to ask for a K​imi review: GitHub: request @​justinwlin as a reviewer, or comment @​justinwlin w​atcher, review on the PR. Slack: DM @​justin.lin W​atcher, review <PR link>. New commits get an automatic re-review 30 min after the last push. Stop it: DM W​atcher, stop <PR link>.

@justinwlin justinwlin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kimi review, round 1

Verdict: Needs changes. 0 Critical, 1 High, 2 Medium, 0 Low → not approved: both new socket tests are flaky, and required CI is red.

Next step: deanq fixes findings 2-3 with a deterministic retry test, resolves or reruns the required e2e failure, then re-requests review.

Context: The PR reserves the retry-test port with a listener that resets incoming connections.

Findings

  1. High: Required e2e and validation checks fail. The cold-start test polls port 8199 after flash falls back to 8200; base 791cdfa has the same failure. Fix or rerun the required check before merge.
  2. Medium: RSTs trigger an asyncio transport-setup race. tests/test_apps/test_retry.py:158. A later retry raises RuntimeError instead of ClientConnectionError; reproduced with focused stress. Stub the connection error and assert RETRY_ATTEMPTS calls instead.
  3. Medium: The reset assertion has a send-side race. tests/test_apps/test_retry.py:142. An early RST makes sendall raise BrokenPipeError; reproduced in the 5,000-connection stress run. Assert recv first or accept both reset outcomes.
Other reviewers, PR description, nits, what was run
  • Ran: touched module → 24 passed on the first run; a rerun with global coverage disabled gave 23 passed plus one ResourceWarning error. Repeated new tests failed on run 9/20; focused RST stress reproduced findings 2-3, while the 100-call refused-port control passed.
  • Optional test (3.14) failed during collection on OpenTelemetry deprecation warnings; the other unit jobs were cancelled.
  • Copilot on its PR review: Disagree; both reset-path failures reproduce at the current head. #800 (review)

To get a new review, re-request it or comment 'w​atcher, review'. If you push new commits, we review again by ourselves 30 minutes after the last push.

Kimi (moonshotai/Kimi-K3.1-Pro) · round 1 · 2 review agents (socket correctness, quality/PR claims/tests) · 5 min · automated review, a human owns the final call. New commits are checked every 5 min and get an automatic re-review of the changes since this round. How to ask for a K​imi review: GitHub: request @​justinwlin as a reviewer, or comment @​justinwlin w​atcher, review on the PR. Slack: DM @​justin.lin W​atcher, review <PR link>. New commits get an automatic re-review 30 min after the last push. Stop it: DM W​atcher, stop <PR link>.

url = f"http://127.0.0.1:{resetting_tcp_port}/run"
with patch("runpod.apps.targets.RETRY_BASE_DELAY", 0.01):
with pytest.raises(aiohttp.ClientConnectionError):
await _request_json("GET", url, {}, timeout=5)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: Resetting during asyncio transport setup can leave the old transport registered, so a later retry raises RuntimeError: File descriptor … is used by transport instead of ClientConnectionError; reproduced with the 100-call stress test and ResourceWarning: unclosed transport. Make this a deterministic retry unit test: stub the request to raise ClientConnectionError, assert RETRY_ATTEMPTS calls, and remove the RST fixture.

def test_resetting_port_resets_connections(resetting_tcp_port):
with socket.create_connection(("127.0.0.1", resetting_tcp_port), timeout=2) as client:
with pytest.raises(ConnectionResetError):
client.sendall(b"GET /run HTTP/1.1\r\nHost: x\r\n\r\n")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: If the listener resets before sendall, macOS raises BrokenPipeError, so this new test flakes even though the fixture worked; reproduced in the 5,000-connection stress run. Assert recv before sending, or accept BrokenPipeError as another reset outcome.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants