Skip to content

Conversation

@maxisbey
Copy link
Contributor

@maxisbey maxisbey commented Oct 29, 2025

This PR completes the work started in #1527 by fixing the remaining test fixtures that were still using manual socket polling loops to wait for server startup.

Motivation and Context

PR #1527 introduced the wait_for_server() helper and fixed most test files, but missed 4 test fixtures that were still using manual polling loops with arbitrary time.sleep() calls. These remaining fixtures could still cause flaky test failures in CI due to race conditions.

Changes Made

Fixed 4 test fixtures that still used manual polling:

  1. tests/server/fastmcp/test_integration.py - server_transport fixture
  2. tests/client/test_notification_response.py - non_sdk_server fixture
  3. tests/client/test_http_unicode.py - running_unicode_server fixture
  4. tests/shared/test_sse.py - mounted_server and context_server fixtures

Before (Manual Polling):

max_attempts = 20
attempt = 0
while attempt < max_attempts:
    try:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect(("127.0.0.1", server_port))
            break
    except ConnectionRefusedError:
        time.sleep(0.1)  # Arbitrary delay
        attempt += 1
else:
    raise RuntimeError(f"Server failed to start after {max_attempts} attempts")

After (Active Polling):

wait_for_server(server_port)  # Returns immediately when ready

How Has This Been Tested?

All modified test files have been verified:

  • pytest tests/server/fastmcp/test_integration.py - 20 tests pass
  • pytest tests/client/test_notification_response.py - 1 test passes
  • pytest tests/client/test_http_unicode.py - 2 tests pass
  • pytest tests/shared/test_sse.py::test_sse_client_basic_connection_mounted_app - 1 test passes

Breaking Changes

None - this is purely an internal test infrastructure improvement.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

This change:

  • Removes 49 lines of duplicate manual polling code
  • Makes tests more deterministic and reliable
  • Reduces wasted time from fixed delays (tests return as soon as server is ready)

All server startup waiting uses the centralized wait_for_server() helper.

This change completes the work started in PR #1527 by fixing the
remaining test fixtures that were still using manual socket polling
loops to wait for server startup.

## Changes

Fixed 4 test fixtures that were missed in the previous PR:
- tests/server/fastmcp/test_integration.py: server_transport fixture
- tests/client/test_notification_response.py: non_sdk_server fixture
- tests/client/test_http_unicode.py: running_unicode_server fixture
- tests/shared/test_sse.py: mounted_server and context_server fixtures

All now use the centralized wait_for_server() helper which:
- Actively polls for TCP connection readiness
- Returns immediately when server is ready
- Has clear timeout behavior (5s default)
- Eliminates race conditions from arbitrary sleeps

## Impact

- Removes 49 lines of duplicate manual polling code
- Makes tests more deterministic and reliable
- Reduces wasted time from fixed delays (tests return as soon as ready)
- Follows the patterns documented in FLAKY_TESTS.md
@maxisbey maxisbey enabled auto-merge (squash) October 29, 2025 00:13
Copy link
Contributor

@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

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

🧑‍🍳 👌

@maxisbey maxisbey merged commit db9e451 into main Oct 29, 2025
21 checks passed
@maxisbey maxisbey deleted the fix-remaining-flaky-timeout-tests branch October 29, 2025 11:27
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