Skip to content

Fix incorrect port availability check in get_free_port#504

Open
mohityadav8 wants to merge 1 commit into
adeyosemanputra:v3.0.0-prefrom
mohityadav8:fix-port-check-utility-v2
Open

Fix incorrect port availability check in get_free_port#504
mohityadav8 wants to merge 1 commit into
adeyosemanputra:v3.0.0-prefrom
mohityadav8:fix-port-check-utility-v2

Conversation

@mohityadav8

@mohityadav8 mohityadav8 commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

🐞 Fix for Issue #503
Close #503

Problem

The function get_free_port in challenge/utility.py incorrectly checks port availability using:

if result != 1:

However, socket.connect_ex() returns:

  • 0 when the connection succeeds (port is in use)
  • non-zero when the connection fails (port is available)

This incorrect condition can lead to incorrect detection of free ports.

Solution

Updated the condition to:

if result != 0:

This correctly identifies ports that are not currently in use and available for binding.

Changes

  • Updated port availability check in challenge/utility.py
  • Ensures get_free_port returns a truly free port

Testing

  • Verified that the function now correctly returns an available port within the specified range.

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.

1 participant