GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Pinned sessions can be lost when multiple TUI instances write session.json · Issue #44736 · anomalyco/opencode

Pinned sessions can be lost when multiple TUI instances write session.json #44736

New issue
Copy link
New issue
Copy link
Closed as not planned
#45117
Closed as not planned
Pinned sessions can be lost when multiple TUI instances write session.json#44736
#45117
Copy link
Assignees
kommander

Description

@n2k3
n2k3
opened on Aug 24, 2026
Issue body actions

Description

Each TUI reads the global session.json pin list once and keeps a private in-memory copy. Pinning, unpinning, or handling session.deleted rewrites the entire file without locking, rereading, merging, or serializing writes. A stale TUI can therefore erase pins added by another TUI.

Reproduction

  1. Start TUI A and TUI B with the same pin list.
  2. In B, pin a session. B writes the updated list.
  3. In stale A, pin/unpin another session or delete a session.
  4. Restart OpenCode.

The pin added in B is gone because A replaced the complete file with its stale snapshot.

Expected

Concurrent TUI instances should not erase unrelated pin updates.

Cause

createSession() loads the file once and each save() passes the full in-memory array to a fire-and-forget writeJsonAtomic() call:

  • opencode/packages/tui/src/context/local.tsx

    Lines 411 to 493 in 826d9ad

    function createSession() {
    const [sessionStore, setSessionStore] = createStore<{
    ready: boolean
    pinned: string[]
    }>({
    ready: false,
    pinned: [],
    })
    const filePath = path.join(paths.state, "session.json")
    const state = {
    pending: false,
    }
    function save() {
    if (!sessionStore.ready) {
    state.pending = true
    return
    }
    state.pending = false
    void writeJsonAtomic(filePath, {
    pinned: sessionStore.pinned,
    })
    }
    readJson<unknown>(filePath)
    .then((x) => {
    if (!x || typeof x !== "object") return
    const pinned = (x as Record<string, unknown>).pinned
    if (Array.isArray(pinned))
    setSessionStore(
    "pinned",
    pinned.filter((item): item is string => typeof item === "string"),
    )
    })
    .catch(() => {})
    .finally(() => {
    setSessionStore("ready", true)
    if (state.pending) save()
    })
    const slots = createMemo(() => {
    const existing = new Set(sync.data.session.filter((x) => x.parentID === undefined).map((x) => x.id))
    return sessionStore.pinned.filter((id) => existing.has(id)).slice(0, 9)
    })
    function prune(sessionID: string) {
    batch(() => {
    if (sessionStore.pinned.includes(sessionID)) {
    setSessionStore(
    "pinned",
    sessionStore.pinned.filter((x) => x !== sessionID),
    )
    }
    save()
    })
    }
    event.on("session.deleted", (evt) => {
    prune(evt.properties.info.id)
    })
    return {
    get ready() {
    return sessionStore.ready
    },
    pinned() {
    return sessionStore.pinned
    },
    slots,
    isPinned(sessionID: string) {
    return sessionStore.pinned.includes(sessionID)
    },
    togglePin(sessionID: string) {
    batch(() => {
    const exists = sessionStore.pinned.includes(sessionID)
    const next = exists
    ? sessionStore.pinned.filter((x) => x !== sessionID)
    : [...sessionStore.pinned, sessionID]
    setSessionStore("pinned", next)
    save()
    })
    },
  • opencode/packages/tui/src/util/persistence.ts

    Lines 22 to 33 in 826d9ad

    export async function writeJsonAtomic(filePath: string, value: unknown) {
    await mkdir(path.dirname(filePath), { recursive: true })
    const temporary = `${filePath}.${process.pid}.${crypto.randomUUID()}.tmp`
    await Bun.write(temporary, JSON.stringify(value)).catch(async (error) => {
    await rm(temporary, { force: true }).catch(() => undefined)
    throw error
    })
    await rename(temporary, filePath).catch(async (error) => {
    await rm(temporary, { force: true }).catch(() => undefined)
    throw error
    })
    }

Atomic rename prevents partial files but does not prevent stale writers. There is also no file watcher or close-time merge. Closing a TUI alone does not save; the last completed save wins.

I verified that the relevant persistence code is identical in v1.18.18, v1.18.19, v1.18.20, v1.18.21, and current dev.

Environment

  • macOS
  • Multiple OpenCode TUIs in WebStorm and system terminals
  • OpenCode v1.18.18 through v1.18.21

Activity

  1. github-actions
    assigned
    kommander
    on Aug 24, 2026
  2. rareboe commented on Aug 26, 2026

    @rareboe
    rareboe
    on Aug 26, 2026
    More actions

    I'd like to take this one.

    Confirmed the analysis: createSession() in packages/tui/src/context/local.tsx reads session.json once into sessionStore.pinned, and save() hands the whole in-memory array to a fire-and-forget writeJsonAtomic(). Nothing rereads, locks, or serializes, so a stale instance replaces the file wholesale.

    I reproduced the loss directly against the current save() semantics — two writers starting from an empty snapshot, one pinning a and one pinning b, leaves the file as ["b"].

    Planned fix, following the pattern context/kv.tsx already uses for kv.json:

    • express each change as an intent (sessionID + pin/unpin) instead of a snapshot, and apply it to the file's current contents inside Flock.withLock
    • chain writes off the initial read so ordering is guaranteed and the pending flag goes away
    • keep the optimistic in-memory update so the UI still responds immediately, then reconcile the store with whatever the merged file ended up containing

    Worth noting kv.tsx locks but still writes its own snapshot, so kv.json has the same stale-writer shape. I'm leaving that alone here to keep the change focused — happy to follow up if you want it covered too.

    PR shortly, with regression tests for the concurrent case.

  3. github-actions commented on Oct 25, 2026

    @github-actions
    github-actionsBot
    on Oct 25, 2026 – with GitHub Actions
    Contributor
    More actions

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

  4. github-actions
    closed this as not plannedon Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @kommander
    kommander

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

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.