Skip to content

cli: proxy env routes localhost service discovery through proxy, concurrent spawns fail with "port already in use" #57020

Closed as duplicate

Description

@maoshengyzx

Description

Summary

When a system proxy is active (WSL2 networkingMode=Mirrored auto-syncs HTTP_PROXY/no_proxy into every WSL process), service discovery can transiently fail and multiple concurrently starting CLI instances each spawn serve --service; the losers die with Managed service port 49375 ... already in use.

Two related defects:

  1. Without NO_PROXY, requests to 127.0.0.1 are routed through the proxy — loopback should be excluded unconditionally.
  2. Concurrent service spawns are not mutually exclusive, so a transient discovery failure surfaces as a confusing "port already in use" error.

Actual Behavior

Log timeline (2026-10-19, ~/.local/share/opencode/log/opencode.log; 49 occurrences between 10-12 and 10-19 on versions 2.0.29–2.0.32):

06:53:13       event stream disconnected (socket closed unexpectedly)
06:53:18/23/36 three serve --service spawned within 18s
06:53:31       background service starting reason=missing
06:53:34~51    all three die: "Managed service port 49375 on 127.0.0.1 is already in
                use by another process ... (cause: ServeError: Failed to start server.
                Is port 49375 in use?)"
06:54~07:04    external fetches (models.opencode.ai) fail while proxy restarts

Second occurrence the same day:

07:43:48 background service starting reason=missing → spawn #1 (succeeds)
07:43:53 spawn #2 five seconds later
07:44:09 spawn #2 dies: "Managed service port 49375 ... already in use"

Expected Behavior

  • Health/discovery requests to 127.0.0.1 should never go through a proxy, even when NO_PROXY is absent or malformed.
  • Concurrent service starts should be mutually exclusive (per-port spawn lock); losing starters should wait and attach to the already-running service instead of erroring.

Probe experiment

With a deliberately dead proxy (HTTP_PROXY=http://127.0.0.1:1), running opencode api get /api/info:

env result
no NO_PROXY ❌ fails — localhost request is sent to the proxy
NO_PROXY=127.0.0.1 ✅ direct
NO_PROXY=127.*,localhost,<local> (Windows-style, as synced by WSL) ✅ direct

Additional Context

  • Environment: WSL2 with networkingMode=Mirrored; Windows Clash system proxy at 127.0.0.1:7890; the registry ProxyOverride bypass list only affects WinINET consumers, not Node/Bun programs reading HTTP_PROXY.
  • Workarounds: pin NO_PROXY=127.0.0.1,localhost,::1 in the shell profile; avoid toggling the proxy while starting opencode; avoid launching multiple instances simultaneously.
  • Suggested fix: unconditionally exclude loopback in the HTTP client used for service discovery, and guard spawn with a per-port lock so losers poll-attach.

Plugins

none

OpenCode version

2.0.32 (also reproduced on 2.0.29–2.0.31)

Steps to reproduce

  1. Enable a system HTTP proxy on Windows (e.g. Clash at 127.0.0.1:7890); WSL2 mirrored networking syncs HTTP_PROXY/no_proxy into every WSL process.
  2. Toggle/restart the proxy (a few seconds of connectivity loss), and/or start several opencode instances at once during that window.
  3. A CLI logs background service starting reason=missing.
  4. Multiple serve --service processes race; the first binds port 49375, the rest fail with EADDRINUSE.

Screenshot

No response

Operating System

Linux 6.6.87.2-microsoft-standard-WSL2 (WSL2, networkingMode=Mirrored), x86_64 — Windows host

Terminal

Windows Terminal / xterm-256color, shell /bin/bash

Activity

  1. opencode-agent commented on Oct 19, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed timeline and the probe experiment, they make the cause clear.

    I think this is a duplicate of #52054. When HTTP_PROXY is set and NO_PROXY doesn't exempt loopback, the client sends its health and discovery requests to its own background service on 127.0.0.1 through the proxy. When that request fails, the client decides the service is missing and starts another serve --service, which then fails with "Managed service port ... already in use". Your table shows the same thing: no NO_PROXY fails, NO_PROXY=127.0.0.1 works.

    Possible duplicates and related issues, ordered by likelihood:

    Until this is fixed, your workaround of exporting NO_PROXY=127.0.0.1,localhost,::1 (and no_proxy too) in your shell profile is the most reliable option.

    The bot will close this issue as a duplicate in 1 day unless you reply explaining how your problem is different.

  2. opencode-agent commented on Oct 20, 2026

    @opencode-agent
    Contributor

    Closing this as a duplicate of #52054, since we haven't heard otherwise in the last day. Both are about the client sending requests to its own background service on 127.0.0.1 through HTTP_PROXY. Please follow #52054 for progress on the proxy problem, and #54267 for the second point: a new client starting another service instead of attaching to the one already running.

    Until it's fixed, exporting NO_PROXY=127.0.0.1,localhost,::1 (and no_proxy) in your shell profile should avoid the problem. If your case turns out to be different, reply here and we can reopen it. Thanks again for the detailed timeline and probe results.

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