test: Eliminate race conditions in event manager WebSocket tests#801
Merged
test: Eliminate race conditions in event manager WebSocket tests#801
Conversation
The `test_event_handling_on_platform` test was flaky on Windows CI due to two issues in the WebSocket test setup: 1. Dual-stack binding with different ports: `serve(host='localhost')` on Windows creates two sockets — IPv6 and IPv4 — with different random ports. The test read the port from `sockets[0]` (IPv6) but the client could resolve `localhost` to IPv4, hitting the wrong port. 2. Race condition: After `ApifyEventManager.__aenter__` returned, the test broadcast to `connected_ws_clients` without yielding to the event loop, so the server handler that populates the set may not have run yet. Fix: Extract a shared `_platform_ws_server` helper that binds to `127.0.0.1` (single socket) and uses `asyncio.Event` to synchronize the server handler registration before any messages are sent. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #801 +/- ##
=======================================
Coverage 85.50% 85.50%
=======================================
Files 46 46
Lines 2691 2691
=======================================
Hits 2301 2301
Misses 390 390
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
Author
|
or we can remove the test :) |
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.
Summary
test_event_handling_on_platformtest that failed intermittently on Windows CIlocalhostbinding created two sockets with different ports on Windows, and a race condition between server handler registration and message broadcasting_platform_ws_serverhelper binding to127.0.0.1withasyncio.Eventsynchronization — applied to all 6 WebSocket-based testsExample failure: https://github.com/apify/apify-sdk-python/actions/runs/22132559177/job/63976018507?pr=782
Test plan
🤖 Generated with Claude Code