Skip to content

ai-opencode: RUN_FINISHED never arrives when an adapter teardown step does not settle #1638

Closed

Description

@jakubriedl

chat() holds RUN_FINISHED until the adapter's chatStream generator returns. In @tanstack/ai-opencode 0.4.14, the finally block of OpencodeTextAdapter.chatStream awaits handle.dispose(), server.dispose(), and bridge.close() in sequence. If one of them does not settle, the text streams but the run never ends, and the HTTP response stays open.

Repro

  1. Run chat() with opencodeText(...), withSandbox(...), and at least one tool, so that a tool bridge opens.
  2. Wrap the sandbox provider so that process.kill() on a spawned process returns a promise that never settles (as a sandbox API call that never returns does). Or use an opencode on PATH that ignores SIGTERM and keeps a request open on the tool bridge.
  3. Observe: TEXT_MESSAGE_END arrives, but RUN_FINISHED does not.

Proposed fix

Give each teardown step a time limit, and log a step that fails:

const bounded = (step) => Promise.race([
  Promise.resolve().then(step).catch((error) => logger.errors("opencode teardown failed", { error, source: "opencode.chatStream" })),
  new Promise((resolve) => setTimeout(resolve, 1000).unref()),
])
await bounded(() => handle?.dispose())
await bounded(() => server?.dispose())
await bounded(() => bridge?.close())

Suggestion (separate, @tanstack/ai-sandbox-local-process)

spawn uses detached: true, so a spawned process group gets no signal when its owner process dies, and the server continues to run with parent 1. One option: start one watchdog per owner with a stdin pipe that only the owner holds, register each spawned group with it, and kill the registered groups when the pipe closes.

Activity

  1. added
    waiting-on: maintainer
    on Oct 6, 2026
  2. jakubriedl commented on Oct 7, 2026

    @jakubriedl
    Author

    While this issue was generated by claude, it's something we have experienced and verified in real world and have an pnpm patch in our repo. If you have any questions about it please let me know.

  3. added a commit that references this issue on Oct 14, 2026
    b6224c9
  4. added a commit that references this issue on Oct 14, 2026
    6a9f0b1
  5. added 3 commits that reference this issue on Oct 15, 2026
    ea75cbb
    39c1c79
    a5c2af3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

waiting-on: maintainer

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions