Skip to content

cli: run hangs before creating a session when the initial SSE event is buffered #62681

Open

Description

@zawawa-art

Summary

opencode run can hang indefinitely before creating a session when the client's connection buffers the initial server-sent event on GET /api/event. The CLI opens the event stream, never receives server.connected, and sends no subsequent request (no session creation, no prompt). It exits only when the caller times out or aborts; there is no diagnostic output.

Environment

  • opencode version: 2.2.6 (client and server)
  • OS (server): Darwin 27.0.0 arm64 (macOS)
  • OS (client): Ubuntu 24.04.5 LTS (x86_64), cloud VM
  • Terminal: non-interactive / no TTY (scripted invocation)
  • Shell: /bin/bash (GNU bash 5.2.21)
  • Install/channel: server via Homebrew (latest); client via the official v2 installer (https://opencode.ai/v2/install), npm package @opencode/cli-linux-x64 installed to ~/.opencode/bin
  • Active plugins: none

Reproduction

  1. On a server, run opencode serve --hostname 0.0.0.0 --port 4096 with OPENCODE_SERVER_PASSWORD set.
  2. From a client host whose egress path buffers SSE responses (in this case a cloud VM behind a Tailscale tunnel proxy), set OPENCODE_PASSWORD and run opencode run --auto --server http://<server>:4096 "say pong" from a working directory that exists on the server.
  3. Observe the CLI: no output, no session created, until the command is killed.

Expected Behavior

opencode run should either complete the turn, or fail with a clear error or timeout that identifies the stalled event subscription.

Actual Behavior

The CLI hangs indefinitely. No session is created on the server, and the only request recorded server-side is the event stream subscription:

GET /api/event 200 span=14283   (14.3s)
GET /api/event 200 span=45137   (45.1s)
GET /api/event 200 span=93069   (93.1s, matches a 90s caller timeout)
GET /api/event 200 span=59537   (59.5s)

--auto does not change the behavior, so this is not a wait on a permission prompt.

Additional Context

  • The server sends an initial event immediately on subscribe (96 bytes): data: {"type":"server.connected", ...} followed by : heartbeat.
  • Through the buffering proxy, an idle subscription received only response headers (~352 bytes in 12s) and no event data; events arrived only once another session produced traffic (~4.2KB in 18s).
  • Against the same server from a client on the same host, opencode run completes in ~1.6s, and the /api/event request lives exactly that long.
  • opencode models and session listing work when the client cwd exists on the server, so name resolution, authentication (Basic opencode:<password>), and model fetching are all fine.
  • Workaround: create the session and submit the prompt through POST /api/session/<id>/prompt and poll the session instead of using opencode run.
  • Suggestion: log the startup phase (server resolved, subscribing to events, session created) at debug level, and add a timeout for the initial server.connected event.

Activity

  1. opencodejr commented on Nov 8, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I was able to reproduce this on 2.2.6, and it still happens on the latest dev (5a47777).

    What I did:

    • Ran opencode serve with a password, plus a small reverse proxy in front of it. The proxy passes everything through, except that for GET /api/event it sends the response headers and holds back the event data, like a buffering proxy would.
    • Ran opencode run --auto --server <proxy> "say pong". The CLI requested /api/info and /api/location, created a session, opened /api/event, and then did nothing: no prompt was sent, nothing was printed, and it ran until it was killed. Exiting then took another 10 seconds.
    • The same command run directly against the server sends the prompt within about a second.

    One small difference: here the session is created before the event subscription. The hang itself matches what you described.

    It looks like the CLI waits for the initial server.connected event with no timeout and no diagnostic output. The script below reproduces it.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Repro for #62681: `opencode run --server` hangs silently when the initial SSE event is buffered.
    # Usage: OPENCODE="opencode" bash repro.sh
    set -u
    OPENCODE=${OPENCODE:-opencode}
    export OPENCODE_SERVER_PASSWORD=secret OPENCODE_PASSWORD=secret
    PROXY_TS=$(mktemp --suffix .ts)
    cat > "$PROXY_TS" <<'TS'
    // Reverse proxy that forwards everything to the opencode server, but for
    // GET /api/event it sends the response headers and then holds back the body
    // (as a buffering proxy does with small SSE payloads).
    const target = process.env.TARGET ?? "http://127.0.0.1:4096"
    const port = Number(process.env.PORT ?? 4097)
    Bun.serve({
      port,
      idleTimeout: 0,
      async fetch(req) {
        const url = new URL(req.url)
        console.log(new Date().toISOString(), req.method, url.pathname)
        const upstream = await fetch(target + url.pathname + url.search, {
          method: req.method,
          headers: req.headers,
          body: req.method === "GET" || req.method === "HEAD" ? undefined : await req.arrayBuffer(),
          redirect: "manual",
        })
        if (url.pathname.endsWith("/event")) {
          // Drain upstream but never forward body bytes (buffered).
          const reader = upstream.body!.getReader()
          ;(async () => { while (!(await reader.read()).done) {} })().catch(() => {})
          return new Response(new ReadableStream({ start() {} }), { status: upstream.status, headers: upstream.headers })
        }
        return new Response(upstream.body, { status: upstream.status, headers: upstream.headers })
      },
    })
    console.log("proxy on", port)
    TS
    PROJ=$(mktemp -d); cd "$PROJ"; git init -q
    $OPENCODE serve --hostname 127.0.0.1 --port 4096 >/tmp/oc-serve.log 2>&1 & SERVE=$!
    PORT=4097 bun "$PROXY_TS" >/tmp/oc-proxy.log 2>&1 & PROXY=$!
    sleep 6
    echo "== direct (no proxy), 20s limit =="
    start=$(date +%s); timeout 20 $OPENCODE run --auto --server http://127.0.0.1:4096 "say pong" </dev/null; echo "exit=$? after $(( $(date +%s)-start ))s"
    echo "== via buffering proxy, 30s limit =="
    start=$(date +%s); timeout 30 $OPENCODE run --auto --server http://127.0.0.1:4097 "say pong" </dev/null; echo "exit=$? after $(( $(date +%s)-start ))s (124 = killed by timeout)"
    echo "== requests seen by proxy =="; cat /tmp/oc-proxy.log
    kill $SERVE $PROXY 2>/dev/null

    Run it: bash repro.sh

  2. r266-tech commented on Nov 8, 2026

    @r266-tech
    Contributor

    I prepared a focused CLI fix at 7682f5a4ca. It bounds the initial event wait to 10 seconds, closes the subscription on timeout or cancellation, and removes the deadline after the first event arrives.

    Real CLI subprocesses against local HTTP fixtures on macOS confirmed:

    • With response headers delivered but the SSE body withheld, 2.2.6 kept waiting; the patched CLI exited 1 with a diagnostic about /api/event buffering.
    • SIGTERM during that wait exited 143 without admitting a prompt.
    • A connected run stayed active past 12 seconds and cancelled normally.

    Validation: 390 CLI tests passed, 9 skipped; the 25 non-interactive tests passed 10 repetitions; repository lint and 33 typecheck tasks passed. The cancellation regression fails on unmodified dev. No model requests were made; the reported tunnel and Windows were not tested.

    I have three open PRs here and previously hit the per-user submission limit, so I am sharing the tested branch in the meantime.

    AI-assisted fix and validation.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions