Skip to content

Cross-process session concurrency: two processes can run loops on the same session simultaneously #49367

Closed as not planned

Description

@Duong-Truong-Anh

Bug

\packages/opencode/src/session/run-state.ts\ keeps its per-session Runner map in \InstanceState\ — in-memory, per-process. When two opencode processes share the same session database (e.g., a TUI and a headless \opencode serve, or two TUI windows), both can start agent loops on the SAME session concurrently, producing interleaved parts from two different message streams.

Reproduction

  1. Open a TUI attached to session X
  2. Start \opencode serve --port N\ on the same project
  3. POST to /session/X/message on the serve port while the TUI is mid-turn
  4. Both processes run loops on session X simultaneously — interleaved step-start/step-finish parts from different runs

Evidence

From opencode.log, two concurrent run IDs (3f40dbfe and 20413a5b) produced interleaved loop/process/stream lines on the same session for 16 minutes.

Proposed fix

Persist the runner state (a DB-level lease or advisory lock keyed by session ID) so only one process may run a loop on a session at a time. The second process should either join the running loop or queue until it finishes.

Impact

Interleaved parts from concurrent loops create confusing output, phantom assistant messages responding to partial state, and data corruption risk when both loops write to the same message rows.

Activity

  1. github-actions commented on Sep 16, 2026

    @github-actions
    Contributor

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

    If your issue is distinct from these (e.g. specific to the opencode serve + TUI combination or the interleaved parts symptom), please clarify the difference.

  2. added 2 commits that reference this issue on Sep 23, 2026
    d970983
    7b776f3
  3. opencodejr commented on Oct 28, 2026

    @opencodejr

    Thanks for the detailed report and the log evidence.

    The code you point to (packages/opencode/src/session/run-state.ts) is OpenCode v1 (opencode-ai, versions 0.x/1.x). v1 now only gets fixes for critical bugs such as data loss, security problems or crashes with no workaround. Running two processes on the same session gives confusing interleaved output, but you can avoid it by sending prompts to a session from only one process at a time. So this is unlikely to be fixed in v1. That is also why your PR #50968 was closed.

    We'd encourage you to upgrade to v2 (@opencode/cli). Note that v2 has a similar cross-process problem, already tracked here:

    If you're seeing this on v2, please add your details to #43648. If you think the v1 behaviour is critical, for example because it actually loses data rather than just interleaving output, reply here and explain.

  4. opencodejr commented on Oct 29, 2026

    @opencodejr

    We haven't heard back, so I'm closing this as not planned. The report is about v1 (packages/opencode/src/session/run-state.ts), and v1 now only gets fixes for critical bugs. Here you can avoid the problem by sending prompts to a session from only one process at a time.

    If you see the same thing on v2 (@opencode/cli), please follow #43648, which tracks overlapping runs on one session across processes sharing a database, and add your details there. Related: #53146 and #51411. If you think the v1 behaviour causes real data loss, reply here and explain.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions