Skip to content

fix: race condition when assigning device kit a random port#204

Merged
gmegidish merged 1 commit intomainfrom
fix-multiple-simulators
Apr 27, 2026
Merged

fix: race condition when assigning device kit a random port#204
gmegidish merged 1 commit intomainfrom
fix-multiple-simulators

Conversation

@gmegidish
Copy link
Copy Markdown
Member

No description provided.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

Walkthrough

The FindAvailablePortInRange function in port-manager.go has been modified to randomize the port selection process. Instead of scanning ports sequentially from startPort, the function now selects a random starting offset within the specified range using rand.Intn(rangeSize) and iterates through ports in a wraparound sequence. The availability checking logic and error handling behavior remain unchanged.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive No pull request description was provided by the author, making it impossible to assess relevance to the changeset. Add a description explaining the race condition being fixed, the impact of randomized port selection, and any testing performed.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change—introducing randomized port selection to fix a race condition when assigning ports to device kits.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-multiple-simulators

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
utils/port-manager.go (1)

26-33: ⚠️ Potential issue | 🟡 Minor

Add input validation for invalid port range.

The function will panic with a negative or zero rangeSize if startPort > endPort. Add a validation check at the start of FindAvailablePortInRange to return an error for invalid input ranges:

 func FindAvailablePortInRange(startPort, endPort int) (int, error) {
+	if startPort > endPort {
+		return 0, fmt.Errorf("invalid port range: startPort %d > endPort %d", startPort, endPort)
+	}
 	rangeSize := endPort - startPort + 1
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@utils/port-manager.go` around lines 26 - 33, The function
FindAvailablePortInRange can panic when startPort > endPort because rangeSize
becomes non-positive; add an early input validation at the start of
FindAvailablePortInRange that checks that startPort <= endPort (and optionally
that both are within 1..65535) and return a clear error (e.g.,
fmt.Errorf("invalid port range: %d-%d", startPort, endPort)) instead of
proceeding to compute rangeSize and call rand.Intn; keep the existing loop and
IsPortAvailable("localhost", port) logic unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@utils/port-manager.go`:
- Around line 26-33: The function FindAvailablePortInRange can panic when
startPort > endPort because rangeSize becomes non-positive; add an early input
validation at the start of FindAvailablePortInRange that checks that startPort
<= endPort (and optionally that both are within 1..65535) and return a clear
error (e.g., fmt.Errorf("invalid port range: %d-%d", startPort, endPort))
instead of proceeding to compute rangeSize and call rand.Intn; keep the existing
loop and IsPortAvailable("localhost", port) logic unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a3cc1d65-f0ea-4241-96e9-bebd54fc0b79

📥 Commits

Reviewing files that changed from the base of the PR and between 079dfc7 and 2b6d672.

📒 Files selected for processing (1)
  • utils/port-manager.go

@gmegidish gmegidish merged commit b76aaeb into main Apr 27, 2026
15 checks passed
@gmegidish gmegidish deleted the fix-multiple-simulators branch April 27, 2026 16:03
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