Skip to content

macOS: Bun runtime gets ECONNREFUSED to LAN gateway IPs (custom provider baseURL) while Node/curl work; HTTP_PROXY works around it #37432

Closed as not planned

Description

@pzx521521

Summary

OpenCode cannot call a custom OpenAI-compatible provider whose baseURL points at a LAN IP (e.g. http://192.168.1.1:8317/v1). The embedded Bun runtime fails at the TCP layer with ECONNREFUSED / FailedToOpenSocket, while Node.js, system curl, and other tools on the same machine connect successfully.

This is not a model/auth/config-schema issue. Setting HTTP_PROXY=http://127.0.0.1:7897 makes OpenCode work immediately (traffic goes to localhost proxy, which then reaches the LAN host).

Related: #28418, #2667, #10961, #26807

Environment

Item Value
OpenCode 1.18.3
OS macOS 15.7.3 (Darwin 24.6.0), x86_64
Binary opencode-darwin-x64 (codesign Identifier=bun, Team 7FRXF46ZSN, Bun v1.3.14)
Proxy / VPN Clash Verge Rev, system HTTP/HTTPS/SOCKS proxy 127.0.0.1:7897, TUN enabled (stack: gvisor, auto-route: true), DNS fake-ip
System proxy exceptions includes 192.168.0.0/16, 10.0.0.0/8, 127.0.0.1, localhost
Provider Custom baseURL to CLIProxyAPI / OpenAI-compatible gateway on LAN

Config (minimal)

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "xai": {
      "name": "xAI (CPA)",
      "options": {
        "baseURL": "http://192.168.1.1:8317/v1",
        "apiKey": "sk-opencode"
      },
      "models": {
        "grok-4.5": { "name": "grok-4.5" }
      }
    },
    "openai": {
      "options": {
        "baseURL": "http://192.168.1.1:8317/v1",
        "apiKey": "sk-opencode"
      },
      "models": {
        "gpt-5.5": { "name": "gpt-5.5" }
      }
    }
  },
  "model": "xai/grok-4.5"
}

Both xai/* and openai/* fail the same way when proxy env is unset.

Error (OpenCode log)

ERROR message="stream error" providerID=xai modelID=grok-4.5
error.error="AI_APICallError: Cannot connect to API: Was there a typo in the url or port?
  (cause: Error: Was there a typo in the url or port?)"

Isolating with the embedded Bun binary (BUN_BE_BUN=1 on the OpenCode binary):

FailedToOpenSocket
path: "http://192.168.1.1:8317/v1/models"
errno: 0
code: "FailedToOpenSocket"
message: "Was there a typo in the url or port?"

Raw TCP:

bun net.connect(192.168.1.1:8317) → ECONNREFUSED
node net.connect(192.168.1.1:8317) → connected

Reproduction

  1. Run an OpenAI-compatible API on a LAN host (not localhost), e.g. http://192.168.x.x:8317/v1.
  2. Point OpenCode provider.*.options.baseURL at that URL.
  3. Ensure no HTTP_PROXY / HTTPS_PROXY is set for the OpenCode process.
  4. Run:
opencode run -m xai/grok-4.5 "hi"
# or
opencode run -m openai/gpt-5.5 "hi"
  1. Observe connect failure.

Same-machine controls

Client Target Result
system /usr/bin/curl http://192.168.1.1:8317/v1/models 200
Node.js fetch / net.connect same OK
Python urllib / raw socket same OK
OpenCode / embedded Bun fetch same FailedToOpenSocket
Embedded Bun net.connect 192.168.1.1:8317 ECONNREFUSED
Embedded Bun net.connect 127.0.0.1:7897 OK
Embedded Bun net.connect 192.168.1.5:<local-server> (self LAN IP) OK when something listens
Embedded Bun fetch with HTTP_PROXY=http://127.0.0.1:7897 http://192.168.1.1:8317/v1/models 200
OpenCode with HTTP_PROXY/HTTPS_PROXY full opencode run works (model responds)

Port scan on the gateway IP (illustrative):

Port on 192.168.1.1 Bun Node
80 ECONNREFUSED OK (if open)
8317 ECONNREFUSED OK
53 ECONNREFUSED OK
22 / 443 (closed) ECONNREFUSED ECONNREFUSED

So Bun cannot open TCP to other LAN hosts (here the gateway), while Node can. Loopback works for Bun.

Minimal Bun reproduction (using OpenCode binary as Bun)

OC="$(node -p "require('path').join(require('path').dirname(require('fs').realpathSync(process.execPath)),'..','lib/node_modules/opencode-ai/node_modules/opencode-darwin-x64/bin/opencode")"
# or absolute path to opencode-darwin-*/bin/opencode

# FAIL — direct
BUN_BE_BUN=1 "$OC" -e '
import { connect } from "net";
const s = connect(8317, "192.168.1.1");
s.on("connect", () => { console.log("connected"); s.end(); });
s.on("error", e => console.log(e.code, e.message));
'

# OK — via local HTTP proxy
HTTP_PROXY=http://127.0.0.1:7897 BUN_BE_BUN=1 "$OC" -e '
const r = await fetch("http://192.168.1.1:8317/v1/models", {
  headers: { Authorization: "Bearer sk-opencode" },
});
console.log(r.status);
'

Expected behavior

OpenCode should connect to a custom provider baseURL on a LAN IP whenever the host OS can reach that IP (as proven by curl/Node), without requiring a manual HTTP_PROXY workaround.

Actual behavior

Embedded Bun fails with ECONNREFUSED / FailedToOpenSocket before any HTTP status is returned. User-facing error is the generic “Was there a typo in the url or port?”.

Workarounds

  1. Export proxy env when launching OpenCode (works reliably on this setup):
export HTTP_PROXY=http://127.0.0.1:7897
export HTTPS_PROXY=http://127.0.0.1:7897
opencode
  1. Run the API gateway on localhost / SSH port-forward and set baseURL to http://127.0.0.1:....

  2. Note: top-level opencode.json has no env field for process-wide HTTP_PROXY (unlike Claude Code settings.json), so users cannot declare the workaround in config alone.

Analysis / likely root cause

  1. Failure is at TCP connect in the embedded Bun runtime used by the OpenCode binary, not in provider JSON or API key handling.
  2. Node and system curl succeed to the same host:port at the same time → host firewall / server not simply “down”.
  3. HTTP_PROXY to 127.0.0.1 succeeds → OpenCode’s higher-level HTTP client is fine once Bun can open a socket to a reachable target.
  4. Environment also runs Clash TUN (gVisor). System proxy is enabled with LAN CIDRs in ExceptionsList, so “direct” LAN paths are expected; Bun’s direct path still gets ECONNREFUSED for the gateway IP.
  5. OpenCode CLI is not a .app (Info.plist=not bound, codesign id bun), so it does not appear under System Settings → Privacy & Security → Local Network, and no Local Network prompt is shown. Even if macOS Local Network privacy is involved for some CLIs, users cannot grant it via the UI for this binary.
  6. Overlaps with @ai-sdk/openai-compatible provider fails to connect to LAN addresses — FailedToOpenSocket errno:0 #28418 (LAN FailedToOpenSocket with embedded Bun). This report adds: raw net.connect matrix, gateway-wide refusal, Clash TUN/system-proxy context, and confirmed HTTP_PROXY workaround for full opencode run.

Feature / fix suggestions

  • Document that LAN baseURL can fail under VPN/TUN/proxy setups and document HTTP_PROXY workaround.
  • Consider supporting process env in opencode.json (or provider-level proxy options) so users can set proxy without shell aliases.
  • Investigate Bun embedding / networking on macOS with Network Extensions / TUN (happy eyeballs, interface selection, comparison with standalone Bun).
  • Clearer error message when FailedToOpenSocket hits a private IP (hint: proxy, localhost forward, VPN/TUN).

Additional notes

  • Codex CLI and Claude Code on the same machine can use the same LAN gateway (Claude injects HTTP_PROXY in settings; Codex’s network stack reaches the host).
  • Custom OpenSSL-based curl builds may also fail where system SecureTransport curl succeeds — another sign of stack-specific path issues, not a dead server.

Happy to provide more logs (opencode run --print-logs --log-level DEBUG) or test a patch build.

Activity

  1. github-actions commented on Jul 17, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

  2. github-actions commented on Sep 16, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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

Metadata

Metadata

Assignees

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