Skip to content

Fix get_free_port checking port 65535#2044

Open
nightcityblade wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
nightcityblade:fix-get-free-port-65535
Open

Fix get_free_port checking port 65535#2044
nightcityblade wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
nightcityblade:fix-get-free-port-65535

Conversation

@nightcityblade
Copy link
Copy Markdown
Contributor

Description

Fixes the inclusive upper bound in get_free_port() so port 65535 is actually checked before raising.

Closes #2023.

Usage

from nemo_curator.core.utils import get_free_port

port = get_free_port(65534)

Checklist

  • I am familiar with the Contributing Guide.
  • New or Existing tests cover these changes.
  • The documentation is up to date with these changes.

Tests:

  • uv run ruff check nemo_curator/core/utils.py tests/core/test_utils.py
  • uv run python - <<'PY' ... custom focused get_free_port(65534) == 65535 harness

Note: uv run pytest tests/core/test_utils.py -q could not run on this macOS host because NeMo-Curator raises on non-Linux systems during import.

Signed-off-by: nightcityblade <nightcityblade@gmail.com>
@nightcityblade nightcityblade requested a review from a team as a code owner June 2, 2026 03:12
@nightcityblade nightcityblade requested review from meatybobby and removed request for a team June 2, 2026 03:12
@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented Jun 2, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Jun 2, 2026

Greptile Summary

This PR fixes an off-by-one bug in get_free_port where range(start_port, 65535) excluded port 65535, the highest valid TCP port. The range is corrected to range(start_port, 65536) so port 65535 is now reachable.

  • nemo_curator/core/utils.py: Single-character fix changing the range upper bound from 65535 to 65536, ensuring port 65535 is included in the scan.
  • tests/core/test_utils.py: Adds a regression test using a monkeypatched FakeSocket that rejects all ports except 65535, verifying get_free_port(65534) now correctly returns 65535.

Confidence Score: 5/5

Safe to merge — the change is a minimal, well-tested one-line fix with no side effects on the rest of the port-scanning logic.

The fix is a single-character range correction with clear intent, a matching regression test, and no impact on any other code path. Ray worker port exclusion (10002–19999) is unaffected, and the error message already says '65535' so it remains consistent with the new behaviour.

No files require special attention.

Important Files Changed

Filename Overview
nemo_curator/core/utils.py One-line off-by-one fix: range upper bound changed from 65535 to 65536, correctly including port 65535 in the search.
tests/core/test_utils.py Adds import of get_free_port and a focused regression test using a monkeypatched FakeSocket that rejects all ports except 65535, confirming the fixed range reaches port 65535.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["get_free_port(start_port)"] --> B["for port in range(start_port, 65536)"]
    B --> C{"port in Ray\nworker range?\n(10002–19999)"}
    C -- Yes --> B
    C -- No --> D["Try bind(localhost, port)"]
    D -- "OSError\n(port busy)" --> E{"get_next_free_port\nAND port==start_port?"}
    E -- No --> B
    E -- Yes --> F["raise RuntimeError\n'Port already in use'"]
    D -- Success --> G["return port"]
    B -- "exhausted" --> H["raise RuntimeError\n'No free port found'"]
Loading

Reviews (1): Last reviewed commit: "Fix get_free_port checking port 65535" | Re-trigger Greptile

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: get_free_port() off-by-one — port 65535 never checked despite appearing in error message

2 participants