Skip to content

Fix random port selection failing in sandboxed environments (Windows/Codex CLI)#52

Merged
pardeike merged 2 commits intomainfrom
copilot/fix-2a95827c-aa27-4725-827e-6af587f13ecf
Sep 16, 2025
Merged

Fix random port selection failing in sandboxed environments (Windows/Codex CLI)#52
pardeike merged 2 commits intomainfrom
copilot/fix-2a95827c-aa27-4725-827e-6af587f13ecf

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Sep 16, 2025

Problem

The current random port selection code fails when GABS is used as an MCP server in sandboxed environments, particularly on Windows with Codex CLI. The random port probing approach using crypto/rand can be restricted or interfered with by sandbox security policies, causing port allocation failures.

Solution

Refactored the port selection mechanism from random probing to a deterministic sequential approach that avoids sandbox restrictions while maintaining reliable concurrent operation.

Key Changes

Before:

// Try up to 100 random ports to avoid infinite loops
for attempts := 0; attempts < 100; attempts++ {
    port := minPort + (randomInt() % (maxPort - minPort + 1))
    if isPortAvailable(port) {
        return port, nil
    }
}

After:

// Sequential search with small offset for concurrent collision avoidance
offset := portOffset % 10  // Deterministic but rotating
for i := 0; i < rangeSize; i++ {
    port := minPort + (offset + i) % rangeSize
    if isPortAvailable(port) {
        return port, nil
    }
}

Benefits

  • Sandbox Compatible: Eliminates complex random generation that may be blocked in restricted environments
  • Predictable: Port selection is deterministic and repeatable, avoiding sandbox interference issues
  • Concurrent Safe: Small rotating offset (0-9) prevents multiple games from competing for the same initial ports
  • Performance: Faster port discovery by eliminating random retry loops
  • Backward Compatible: All existing port range configurations and fallback logic preserved

Testing

  • Added comprehensive tests for deterministic port selection behavior
  • Validated concurrent bridge creation still works correctly (10 unique ports allocated simultaneously)
  • All existing tests pass (16/16 test suites)
  • No regressions in other components

This change specifically addresses the Windows sandbox restrictions reported with Codex CLI while maintaining the robust port allocation behavior needed for concurrent game launches.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…sues

Co-authored-by: pardeike <853584+pardeike@users.noreply.github.com>
Copilot AI changed the title [WIP] The current random port selection code section that determines which port the GABP server should start listening on seems to fail on my local Windows computer when I run Codex CLI (AI) that uses GABS as an MCP server. Maybe a sandbox interferes with th... Fix random port selection failing in sandboxed environments (Windows/Codex CLI) Sep 16, 2025
Copilot AI requested a review from pardeike September 16, 2025 22:05
@pardeike pardeike marked this pull request as ready for review September 16, 2025 22:15
@pardeike pardeike merged commit d704f49 into main Sep 16, 2025
6 checks passed
@pardeike pardeike deleted the copilot/fix-2a95827c-aa27-4725-827e-6af587f13ecf branch September 16, 2025 22:16
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.

2 participants