Skip to content

restore: concurrent 'restart restore' duplicates windows (snapshot has no claim/lock) #389

Done

Description

@KooshaPari

Summary

restart restore has no claim on the reboot snapshot, so two concurrent invocations both restore the
same snapshot and each spawns its own windows. Measured on Linux, deterministic: +2 terminals from
1 snapshot, 3/3 trials
.

Cause (source, not inference)

  • crates/jcode-app-core/src/restart_snapshot.rs:184 restore_snapshot(): load_snapshot() -> spawn
    every session -> return. No lock, no atomic rename, no claim file.
  • src/cli/commands/restart.rs:123 clears the snapshot after the launches.
  • Auto-restore narrows but does not close the window: restart.rs:95 reads
    snapshot.auto_restore_on_next_start, writes set_auto_restore_on_next_start(false), then
    restores. Two processes starting together can both read true before either writes false.

Repro (Linux)

With a probe KCODE_HOME holding a restart-snapshot.json whose auto_restore_on_next_start is true
and one session in sessions[]:

timeout -k 2 20 script -qec "$KCODE" /dev/null &   # first client
timeout -k 2 20 script -qec "$KCODE" /dev/null &   # second client
wait
hc ls --all                                        # TWO terminals for the same session_id

Both processes print:

Found a reboot snapshot with auto-restore enabled. Restoring 1 jcode window(s)...
Restored 1 jcode window(s).
Cleared reboot snapshot after successful restore.

Expected

One restore wins. The second sees no snapshot and starts a normal session, or exits quietly.

Actual

Both restore. Duplicate windows for the same session, and both report success.

Why it matters

A reboot that starts more than one client (desktop autostart plus a fleet pane, or two windows racing
one launcher) can produce duplicate windows of every saved session while the log says the restore
succeeded.

Suggested fix

Make the claim atomic before spawning: rename(restart-snapshot.json, restart-snapshot.claiming.<pid>)
or O_EXCL on a claim file, and restore from the renamed file, so exactly one process can win and the
loser starts normally. Apply it to both the explicit command and the auto-restore branch, because the
flag write is also check-then-act.

Evidence in hand

Repro logs, the three trial deltas, and the hc get record of a restored terminal (argv
kcode --fresh-spawn --resume <session>, identity: ghostty, reported: true, parent: null,
die_with_parent: false) are available on request; probes ran in an isolated KCODE_HOME with
production config untouched.

Activity

  1. added
    bug
    kilo-triaged
    on Nov 28, 2026
  2. KooshaPari commented on Nov 28, 2026

    @KooshaPari
    OwnerAuthor

    Linux repro re-run against the fix, plus a negative control. Same box, same script, same isolated KCODE_HOME.

    Setup

    [nixos] [email protected], Linux 7.3.4. Two concurrent PTY clients via timeout -k 2 25 script -qec "$KC" /dev/null, with KCODE_HOME=/tmp/k389-home holding one restart-snapshot.json (auto_restore_on_next_start: true, one session), HOME/XDG redirected to /tmp/k389-userhome.

    Metric: how many clients print Restored 1 jcode window(s). across both.

    Result: fixed build (master 0ae839431 claim fix)

    === client1 ===
    restoring_lines=0   restored_lines=0   cleared_lines=0
    === client2 ===
    restoring_lines=1   restored_lines=1   cleared_lines=1
    TOTAL_restored=1
    snapshot_absent
    no_claim_file
    

    One restore wins, the loser never prints the restore banner and starts normally, the claim file is released.

    Negative control: pre-fix binary (kcode v0.93.0-k0.1.0-dev (398d44f), verified NOT an ancestor of the fix)

    === client1 ===
    restoring_lines=1   restored_lines=1   cleared_lines=1
    === client2 ===
    restoring_lines=1   restored_lines=1   cleared_lines=1
    TOTAL_restored=2
    

    Two restores for one snapshot, exactly as reported in this issue. So the script can fail, and it fails on the code this issue was filed against.

    Also

    Focused unit suite on the same host: cargo test -p jcode-app-core restart_snapshot -> test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 1539 filtered out; finished in 0.69s.

    Closing: the duplicate-restore path no longer reproduces on Linux.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugkilo-triaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions