Skip to content

test(cli): sandbox forward tests flake when another test claims the released port #4569

In review

Description

@matthewgrossman

User Story

I'm the OpenShell 0.1.5 release captain. The rustls security fix (#4568) failed Branch Checks because of an unrelated flaky CLI test, which held up the release fix.

Description

openshell-cli::sandbox_create_lifecycle_integration::sandbox_create_keeps_sandbox_with_forwarding failed on aarch64-linux with:

sandbox create with forward should succeed:   × Port 36059 is already in use by another process.

The test reserves a forward port by binding 127.0.0.1:0, reading the port, and dropping the listener. nextest runs tests as concurrent processes, so another test's bind("127.0.0.1:0") can be handed the same ephemeral port before the CLI checks it. Three other forward tests in the file, and the reserve_local_port helper, use the same pattern.

Context

Definition of Done

  • Forward tests in sandbox_create_lifecycle_integration.rs pick ports that concurrent OS-assigned binds can't take
  • The test file passes repeatedly

Activity

  1. added a commit that references this issue on Oct 14, 2026
    4dd6151
  2. TaylorMutch commented on Nov 24, 2026

    @TaylorMutch
    Contributor

    🏗️ build-plan

    Reopened after #5947 failed its ARM64 Linux Rust test job: https://github.com/ai-openshell/openshell/actions/runs/45467433034/job/138705982778 . The PostgreSQL test connect_to_a_refusing_server_fails_unavailable_within_the_acquire_timeout expected connection refusal but received unexpected response from SSLRequest: 0x48; 10,508 of 10,509 tests passed. Its fixture releases an OS-assigned port before connecting, allowing another parallel test to listen there.

    The implementation uses port zero for generic connection failures and retained silent listeners for PostgreSQL handshake/acquisition timeouts. A Linux-only test retains a bound, non-listening socket to verify exact refusal and exclusion of reuse-enabled listeners. This removes the shared connected-pair fixture after self-review reproduced its macOS reuse gap, and removes the added test feature/dependencies. The audit comment records the remaining server/subprocess handoff candidates separately.

  3. TaylorMutch commented on Nov 24, 2026

    @TaylorMutch
    Contributor

    Audit of released-port fixtures (continuation of the #5947 failure):

    The revised patch uses destination 127.0.0.1:0 for generic connection failures: a listener cannot claim destination port zero. PostgreSQL timeout tests instead retain a listener that never answers the handshake. A separate Linux-only test holds a bound, non-listening socket and checks both exact connection refusal and exclusion of a normal Tokio listener (which enables SO_REUSEADDR).

    The connected-pair guard was removed after self-review reproduced a macOS SO_REUSEADDR listener claiming its source port. Removing it also removes all added dev-dependencies and restores the original no-default-features graphs.

    Fixed sites:

    • PostgresStore::unresponsive_url_for_tests and its startup, mutation-lock, and pool-budget callers retain the silent listener through acquisition. The Linux refusal test retains its bound socket.
    • Peer-forward metrics' unavailable owner endpoint.
    • HTTP and gRPC OTLP exporter closed-port tests.
    • GitHub App token transport's connection-failure case.
    • Vault's unavailable-proxy endpoint uses port zero.
    • testkit program's absent-gateway endpoint uses port zero.
    • Sandbox performance harness's worker preparation failure.
    • The supervisor IPv4 readiness bind test now lets the actual listener bind port zero.

    Potential remaining handoff races found by the audit (not covered by the refusal fixture):

    Sites Why they need a separate approach
    CLI auth.rs, callback.rs, and oidc_auth.rs: free_loopback_port A re-executed test binds a configured fixed port later.
    CLI sandbox_create_lifecycle_integration.rs: the two direct spawn_service_forward callers They use reserve_local_port without the SSH forward helper's collision retries.
    Supervisor TCP readiness transition tests They close and reopen a listener at the same address to test readiness.
    Server lib.rs: reserved_loopback_address Health-listener startup and a late PostgreSQL proxy bind the released address later.
    OTLP relay failed_export_does_not_stop_the_relay It intentionally starts the collector later and retries collector-bind collisions, but the earlier failure phase still uses a released port.
    host_server_support::free_port and conformance port forwarding The CLI binds later, after sandbox creation.
    Legacy e2e/rust find_free_port, gateway shell helpers, Python native-endpoint fixture, Kubernetes app-listener port-forward A container or subprocess binds the released port later.
    Network supervisor proxy.rs: dropped UDP socket used as an unreachable DNS server UDP needs its own strategy; holding a UDP socket changes the expected failure into a timeout.

    The original SSH-forward cases already have bounded retries for both preflight port conflicts and fake SSH exit 255 (the latter also tracked in #4797). Unix-domain sockets in temporary directories and fixtures that retain their actual listening sockets were excluded from the TCP race findings.

    Validation of the revised patch: focused macOS ARM64 tests and all 14 testkit program checks pass; the silent PostgreSQL startup fixture passes 12 runs across four concurrent processes. Clippy, formatting, Markdown lint, and the no-default-features graph check pass. The Linux-only refusal/performance tests and Windows runtime behavior were not validated locally.

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