Skip to content

opencode run --format json --session hangs after final answer is persisted #32506

Closed as not planned

Description

@cw75

Description

Summary

opencode run --format json --session <session_id> can hang even after the model has completed and the final assistant answer has been persisted to OpenCode’s session DB.

In our repro, the process stayed alive, stdout remained empty, but the SQLite session DB contained both the completed tool result and final answer.

What We Tried

We tested PR #31505, which flushes missed parts after the stream loop finishes:

await finish()
if (!args.attach) await flushParts(result.data?.parts)

That did not fix our case because finish() itself can hang waiting for the event stream to observe session.status: idle. Since flushParts() runs after finish(), the fallback is never reached.

Custom Patch

We changed JSON mode to flush returned prompt/command parts before waiting for stream completion:

async function finishBestEffort() {
  if (args.format !== "json") {
    await finish()
    return
  }

  await Promise.race([
    finish(),
    new Promise<void>((resolve) => setTimeout(resolve, 1000)),
  ])
}

Then changed both session.command and session.prompt paths from:

await finish()
if (!args.attach) await flushParts(result.data?.parts)

to:

if (!args.attach) await flushParts(result.data?.parts)
await finishBestEffort()

Why This Works

Once client.session.prompt() or client.session.command() returns, result.data.parts contains the completed assistant parts. For --format json, emitting those parts immediately avoids depending on the event stream’s idle signal.

The stream is still given a short best-effort drain window, but output delivery no longer blocks forever on it.

Plugins

No response

OpenCode version

0.17.7

Steps to reproduce

No response

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Activity

  1. github-actions commented on Jun 16, 2026

    @github-actions
    Contributor

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

    Issue #32506 is more specific to the --format json --session path where finish() blocks on session.status: idle, which may be a distinct root cause or a manifestation of the same underlying problem. It may be worth cross-referencing these when investigating a fix.

  2. taagarwa-rh commented on Jun 18, 2026

    @taagarwa-rh

    I'm encountering the same issue on v1.17.8

  3. github-actions commented on Aug 18, 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