Skip to content

opencode api: output piped to another process is truncated (stdout not drained before process.exit) #60231

Closed as duplicate

Description

@sergiopadure

Description

opencode api loses part of its output when stdout is a pipe. The response body is written with
process.stdout.write(...) (packages/cli/src/commands/handlers/api.ts:44), which is asynchronous for pipes, and the
CLI then calls process.exit(process.exitCode ?? 0) right away (packages/cli/src/index.ts:142). Data that has not been
flushed yet is dropped, and the exit status stays 0, so callers get truncated JSON with no error.

Steps to reproduce

With a running server, on a response larger than 64 KB (for example the OpenAPI document):

opencode api get /openapi.json > full.json; wc -c < full.json        # complete, valid JSON
opencode api get /openapi.json | wc -c                              # 262144
opencode api get /openapi.json | (sleep 2; wc -c)                   # 65536

Expected behavior

The full body reaches the pipe, as it does with a file.

Actual behavior

The output is cut at the pipe buffer size (65536 bytes with a slow reader, 262144 in our runs with a fast one); exit
status 0. Reproducible on every run.

OpenCode version

2.2.1 (source references checked at v2.2.0, same code path)

Operating System

Linux x86_64

Possible fix

Wait for stdout to drain before exiting, for example by passing a callback to the final process.stdout.write and
exiting from it, or by awaiting the drain event when write returns false. Setting process.exitCode and letting
the process end naturally would also avoid it.

Activity

  1. opencodejr commented on Oct 29, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report and the analysis of the cause, @sergiopadure! I think this is a duplicate of #52925, which reports the same truncation of opencode api get output when stdout is a pipe (exit code 0, complete when redirected to a file). Your 2.2.1 report confirms it is still present in the latest release.

    Possible duplicates and closely related issues, ordered from most to least likely:

    The bot will close this issue as a duplicate in 1 day unless you reply explaining how your problem is different. Please add any further findings to #52925.

  2. opencodejr commented on Oct 30, 2026

    @opencodejr
    Contributor

    Closing this as a duplicate of #52925, since there was no reply about how it differs. Thanks again for the report and the analysis, @sergiopadure! Please follow #52925 and its open fix PR #52955. If you have more findings, such as your 2.2.1 results, add them there. If this turns out to be a different problem, reply here and we can reopen it.

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