Skip to content

Fix flaky HttpListener tests by using dynamic port allocation#10939

Merged
jonathanpeppers merged 3 commits intomainfrom
copilot/prevent-flaky-test-failure
Mar 13, 2026
Merged

Fix flaky HttpListener tests by using dynamic port allocation#10939
jonathanpeppers merged 3 commits intomainfrom
copilot/prevent-flaky-test-failure

Conversation

Copy link
Contributor

Copilot AI commented Mar 13, 2026

Summary

Fixes the flaky HttpContentStreamIsRewoundAfterCancellation test failure caused by System.Net.HttpListenerException: Address already in use when hardcoded port 47664 is unavailable.

Root Cause

The tests DoesNotDisposeContentStream and HttpContentStreamIsRewoundAfterCancellation used hardcoded ports (47663 and 47664) for their HttpListener instances. When these ports are already in use by another process or a previous test run that didn't clean up properly, the tests fail with "Address already in use".

Changes

  • Added GetAvailablePort() helper method that uses TcpListener with port 0 to dynamically allocate an available port from the OS
  • Updated HttpContentStreamIsRewoundAfterCancellation to use dynamic port instead of hardcoded 47664
  • Updated DoesNotDisposeContentStream to use dynamic port instead of hardcoded 47663 (same vulnerability)
  • Added System.Net.Sockets using directive

Security Summary

No security vulnerabilities introduced or discovered. The change only replaces hardcoded port numbers with dynamically allocated ones in test code.

Copilot AI and others added 2 commits March 13, 2026 18:16
… of hardcoded ports

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes flakiness in AndroidMessageHandlerTests caused by HttpListenerException: Address already in use by removing hardcoded listener ports and allocating ports dynamically at runtime.

Changes:

  • Added a GetAvailablePort() helper that uses TcpListener on port 0 to obtain an OS-assigned free port.
  • Updated DoesNotDisposeContentStream and HttpContentStreamIsRewoundAfterCancellation to use dynamically selected ports instead of hardcoded 47663 / 47664.
  • Added using System.Net.Sockets; for TcpListener.

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

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

🤖 AI Review Summary

Verdict: ✅ LGTM

Found 1 suggestion (0 errors, 0 warnings).

  • 💡 Resource management: TcpListener could use using for defense-in-depth (AndroidMessageHandlerTests.cs:106)

👍 Clean, focused fix. The GetAvailablePort() pattern (bind port 0, read assigned port, release) is the standard way to avoid hardcoded-port flakiness in tests. The TOCTOU window between releasing and rebinding is inherent to this technique but negligible in practice.

CI note: Xamarin.Android-PR is failing but this is pre-existing — all recent runs on that pipeline are failing with batchedCI reason. The public dotnet-android Linux build passed; Mac/Windows builds are still in progress.


Review generated by android-reviewer from review guidelines.

- Changed IPAddress.Loopback to IPAddress.Any to match the http://+:{port}/
  prefix which binds all interfaces
- Added using statement for defense-in-depth resource cleanup

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers merged commit 18aae21 into main Mar 13, 2026
6 checks passed
@jonathanpeppers jonathanpeppers deleted the copilot/prevent-flaky-test-failure branch March 13, 2026 23:21
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