Skip to content

Several stacks on one machine collide on the same ports #387

Description

@p-i-p

Problem

A developer with several projects (different repositories, each with its own
devstack.toml) often has them declare the same ports (5000, 4200, 5432 …).
Started in parallel, the second stack fails at a service's start with
"port N is already in use by another process".

Nothing in devstack compares ports across stacks today:

  • The stack registry (crates/devstack-daemon/src/registry.rs, ADR-0021
    point 2) holds {id, name, file, repo}, no ports.
  • Lanes::free_number (crates/devstack-daemon/src/lanes.rs) avoids only
    the stack's own ports and lanes; other stacks enter only through the OS
    probe at lane start, and only if they hold the port at that moment. A lane
    range of stack A can land on stack B's main ports.
  • Main against main: only the bind failure at service start
    (supervisor/lifecycle.rs, start_locked), or Docker's own error for
    containers without main_ports.

Lanes already solve this inside one repository with an offset
([lanes] port_step). This issue proposes the same for a whole stack's main
tree, chosen per machine.

Proposal: a machine-local port shift per stack

Draft review card (ADR-0070). Not approved yet: the points are approved
in the conversation with the owner and then go into the description of the
pull request that builds this.

# Category Point Beats Reversing costs
1 concept A stack's main tree can run with a port shift: every port a lane offset moves (services, Compose host ports, announced ports, urls) moves by it, while the stack keeps its Compose project, volumes and data. Editing every repository's ports by hand, which every developer must repeat and which fights the other machines' choices. Medium: the shift becomes one more state of a main tree that promote --lane, main_ports and the dashboard must know.
2 contract The shift is machine-local state in the user's state directory (the stack's registry entry), never in the stack file. [stack] port_offset in devstack.toml: committed, the same on every machine, so it only moves the collision. Low: an entry field with a serde default; old entries read as shift 0.
3 contract Every daemon writes the ports it claims (main ports after its shift, plus its lanes' ranges) into its registry entry, and every allocation (shift, lane number) avoids the claims of all other registered stacks, not only ports held at that moment. Today's OS probe only, which misses a stopped stack and lets lane ranges of A land on B's main ports. Low to medium: the registry format grows; the hub and older daemons ignore the field.
4 term Nobody moves silently: when up finds its main ports claimed by another running stack, it starts nothing and offers the shift (devstack up --shift auto / --shift <n>, a button in the dashboard); the stack that was first keeps its ports. Shifting the second stack automatically, which breaks bookmarks and ports hard-coded in app config without a word. Low: making it automatic later is additive; a new CLI flag and button are user-visible.
5 concept A chosen shift sticks to the stack on this machine (until devstack up --shift none), so its addresses stay stable across restarts. Recomputing per start, which gives a project a different port each day. Low.

Decided by the agent alone (not asked): the shift is a multiple of 1000
(auto takes the lowest one whose ports, lanes up to the third included, are
free of every claim); error text and dashboard wording; devstack doctor
lists cross-stack overlaps; a shifted stack's lanes offset from the shifted
ports; ports hard-coded outside devstack's placeholders do not follow
(documented, not detected); no stack-file change, so no requires bump and
the compat files stay untouched.

Implementation outline

  1. Registry: shift and claims on the entry (serde default), written by
    serve (crates/devstack-daemon/src/lib.rs) and when lanes are numbered.
  2. Applying the shift: reuse the DEVSTACK_PORT_OFFSET path
    (daemon/src/lib.rs, lanes::lane_stack). For the main tree's containers,
    a shift-only Compose overlay: factor the host-port and announced-port parts
    out of overlay() in crates/devstack-compose/src/lib.rs (without the
    lane renames), written next to write_main_overlay in
    supervisor/infra.rs and merged with main_ports.
  3. Cross-stack claims: Lanes::free_number adds the other registered
    stacks' claims to taken.
  4. up pre-check and --shift auto|<n>|none in the CLI and the MCP up
    tool.
  5. Hub and dashboard: shift and collision in the stack list, an endpoint
    and a button for "start with shift"; mock hub updated.
  6. devstack doctor: overlaps with other registered stacks.
  7. Docs: docs/dashboard-api.md, a section "Several stacks on one
    machine", skills/devstack, changelog; the ADR after the merge.

Verification

  • Unit tests: free_number with foreign claims; the shift overlay (host and
    announced ports moved, names unchanged); registry round-trip with and
    without the new fields.
  • End-to-end on native Windows: two copies of one example stack in two
    folders; up in the second refuses and names the first; up --shift auto
    runs it on +1000 (services, containers, dashboard links); a lane in each
    lands on non-overlapping ports; a restart keeps the shift; --shift none
    returns to the fixed ports.
  • cargo fmt, cargo clippy, cargo test; compat tests in
    crates/devstack-config/tests/compat/ unchanged; macos-14 CI green.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions