Skip to content

fix: make port allocation conflict-safe and overflow-free - #245

Closed
OzzyHelper wants to merge 1 commit into
FilOzone:mainfrom
OzzyHelper:koan/fix-port-allocation-conflicts
Closed

OzzyHelper wants to merge 1 commit into
FilOzone:mainfrom
OzzyHelper:koan/fix-port-allocation-conflicts

Conversation

@OzzyHelper

@OzzyHelper OzzyHelper commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

Port allocation is now conflict-safe and free of u16 range-validation overflow.

Why

Two failure modes in the startup port pool:

  1. Overflow panic. PortAllocator stored a count and every consumer computed start + count. For a valid range ending at 65535 (e.g. 65000 + 536), that addition overflows u16 — a panic in debug builds and a wrapped value that turns allocate() into an infinite/incorrect loop in release builds. Config never rejected such ranges either.
  2. Handing out busy ports. allocate() only checked its own set; a port taken by another process after the initial pre-flight check was silently handed to a container, which then failed to start with a Docker port-binding error.

How

  • PortAllocator stores the inclusive end port, so start + count is never computed. new() rejects count == 0, start == 0, and overflow.
  • allocate() probes the host and skips (reserving) ports that are already bound; the exhaustion error lists which ports were busy.
  • is_port_available probes 0.0.0.0 (what Docker publishes on) as well as loopback, and reports port 0 unavailable.
  • One shared build_port_allocator helper replaces the duplicated (and divergent) range checks in the sequential and parallel executors. This also fixes the parallel path erroring out when the Portainer port fell outside the range.
  • Config::validate reuses the allocator constructor to reject invalid ranges early.
  • Curio re-checks its four published ports immediately before docker create, since they are allocated minutes earlier in pre-execute.
  • Unit tests cover max-port ranges, port 0, overflow, and skipping a port held by a real listener.

Testing

  • cargo test --lib: 60 passed; the 2 failures (test_print_running_status, test_print_uptime) are pre-existing and need a Docker CLI.
  • cargo clippy --all-targets -- -D warnings and cargo fmt --check clean.

Quality Report

Changes: 7 files changed, 282 insertions(+), 146 deletions(-)

Code scan: clean

Tests: skipped

Branch hygiene: 1 issue(s)

  • Branch is not pushed to remote

- PortAllocator stores the inclusive end port instead of a count, so no
  code path computes start + count (which overflowed u16 for ranges
  ending at 65535 and panicked in debug builds).
- allocate() now probes the host and skips ports that are already bound,
  instead of handing out ports another process owns.
- is_port_available probes the wildcard address Docker binds and treats
  port 0 as unavailable.
- Range setup is one shared helper; the parallel executor no longer
  fails when the Portainer port is outside the configured range.
- Config::validate rejects empty, port-0, and overflowing ranges.
- Curio re-checks its published ports right before container create.
@FilOzzy FilOzzy added this to FOC Nov 19, 2026
@github-project-automation github-project-automation Bot moved this to 📌 Triage in FOC Nov 19, 2026
@Kubuxu

Kubuxu commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

not worth it

@Kubuxu Kubuxu closed this Nov 19, 2026
@github-project-automation github-project-automation Bot moved this from 📌 Triage to 🎉 Done in FOC Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🎉 Done

Development

Successfully merging this pull request may close these issues.

2 participants