Skip to content

Handle remote multi-session port conflicts with port-range fallback #437

Closed

Description

@foxytanuki

Summary

Remote mode currently defaults every Plannotator server to the fixed port 19432 so SSH/devcontainer port forwarding stays predictable. That works for a single session, but breaks when multiple Claude Code / OpenCode sessions are started on the same remote machine: the second session and later hit EADDRINUSE and fail.

Today the server only retries the same port a few times; it never searches for another free port.

Environment

  • OpenCode v1.3.6
  • Plannotator v0.16.0
  • Remote mode via SSH (connecting from a local client to a remote dev machine running OpenCode)

Why this happens

  • Local mode uses port 0, so the OS picks a free port.
  • Remote mode uses fixed port 19432 in packages/server/remote.ts.
  • Plan/review/annotate servers retry the same port instead of falling back to another one.
  • PLANNOTATOR_PORT works as a manual escape hatch, but requiring users to manage ports in ~/.zshrc is not ideal.

Proposed solution

Use a small remote port range instead of a single fixed port.

Suggested behavior:

  • Keep 19432 as the default starting port.
  • In remote mode, if 19432 is busy, try 19433, 19434, ... up to 19439.
  • If PLANNOTATOR_PORT is explicitly set, preserve current behavior and only try that exact port.
  • Keep local behavior unchanged (0 => OS-assigned free port).

Why this approach

Pros:

  • Fixes multi-session collisions for the common case.
  • Still keeps remote port forwarding predictable.
  • Requires relatively small backend changes.
  • No frontend changes.
  • No new long-lived daemon/service.

Tradeoffs:

  • Remote users should forward a small range instead of a single port.
  • More than ~8 simultaneous sessions would still fail.

Files likely involved

  • packages/server/remote.ts
  • packages/server/index.ts
  • packages/server/review.ts
  • packages/server/annotate.ts
  • apps/pi-extension/server/network.ts
  • packages/server/remote.test.ts
  • remote/devcontainer docs

Considered alternative: shared reverse proxy

A single stable proxy port with per-session routing is possible, but it looks much heavier than the problem warrants:

  • frontend currently hardcodes many root-relative /api/... calls
  • would require a long-lived proxy/daemon and session registration lifecycle
  • would add more complexity around SSE/streaming and cleanup

For this problem, port-range fallback seems like the better tradeoff.

Activity

  1. foxytanuki commented on Mar 31, 2026

    @foxytanuki
    ContributorAuthor

    Here's the workaround I've been using in ~/.zshrc so far:

    # Find the first available port in the Plannotator range
    _pick_plannotator_port() {
      python3 - <<'PY'
    import socket
    for port in range(19432, 19550):
        with socket.socket() as s:
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            try:
                s.bind(("127.0.0.1", port))
                print(port)
                break
            except OSError:
                pass
    PY
    }
    
    # Wrapper: inject a free port before launching opencode
    oc() {
      local port
      port="$(_pick_plannotator_port)"
      if [[ -z "$port" ]]; then
        print -u2 'oc: no free PLANNOTATOR_PORT found in range 19432–19549'
        return 1
      fi
      PLANNOTATOR_PORT="$port" opencode "$@"
    }

    This works for the most part, but it has a race condition: the port is probed at launch time, but the socket isn't actually bound until Plannotator needs it. If two shells open in quick succession, both may see the same port as free before either has claimed it.

    The port-range fallback proposed here would fix this properly by doing the selection atomically at bind time on the server side.

  2. foxytanuki commented on Mar 31, 2026

    @foxytanuki
    ContributorAuthor

    Closing this as not pursuing upstream in the current form.

    Reference implementations in my fork:

    What the remote port fallback PR does:

    • in remote mode, fall back across 19432-19439 instead of failing immediately on 19432
    • preserve exact-port behavior when PLANNOTATOR_PORT is set
    • fix Bun-side EADDRINUSE detection so the fallback actually works

    Why I’m not upstreaming it as-is:

    • the backend behavior is better, but the UX is still incomplete
    • the missing piece is surfacing which port was actually chosen
    • that is especially problematic in Claude Code, where the PermissionRequest / ExitPlanMode success path does not provide a clean user-visible channel for a custom Plannotator is ready at http://... message

    The OpenCode PR shows one possible workaround, but it relies on session.prompt() rather than a proper status/tool UI surface, so it comes with tradeoffs like queued delivery and session noise.

    So at this point:

    • the backend fallback itself works
    • but the chosen-port notification story is still inconsistent across integrations

    Because of that, I think this is better kept as a fork/reference implementation rather than merged upstream in its current form.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions