Skip to content

Cannot connect to API: The socket connection was closed unexpectedly #23123

@kyler505

Description

@kyler505

Description

When using OpenCode, API connections intermittently fail with:

Cannot connect to API: The socket connection was closed unexpectedly. For more information, pass verbose: true in the second argument to fetch()

This error surfaces raw fetch() internals to the user rather than providing a meaningful error message or automatic recovery. While similar issues exist (#21643 for Zen models on QEMU, #22803 for SGLang ECONNRESET during streaming), this issue covers the general case where the underlying connection drops for any provider — including standard OpenAI/Anthropic endpoints on stable networks.

The core problem is that OpenCode does not gracefully handle transient socket closures. Expected behavior would be:

  1. Automatic retry with backoff (not infinite loops like Streaming ECONNRESET / "socket connection was closed unexpectedly" with SGLang OpenAI-compatible provider during tool-using agent runs (Qwen3.5 + reasoning content) #22803)
  2. A user-facing error message that explains the failure without exposing Bun/fetch internals
  3. A way to configure timeout and retry behavior

Plugins

none

OpenCode version

1.4.6

Steps to reproduce

  1. Use OpenCode with any provider (OpenAI, Anthropic, or custom OpenAI-compatible)
  2. Run an extended session that involves multiple tool calls or streaming
  3. Intermittently the socket drops and the raw error message appears

This is difficult to reproduce deterministically as it depends on network conditions, but occurs regularly in practice.

Screenshot and/or share link

No response

Operating System

Linux (various distributions)

Terminal

various

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions