Repository navigation
Description
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:184restore_snapshot():load_snapshot()-> spawn
every session -> return. No lock, no atomic rename, no claim file.src/cli/commands/restart.rs:123clears the snapshot after the launches.- Auto-restore narrows but does not close the window:
restart.rs:95reads
snapshot.auto_restore_on_next_start, writesset_auto_restore_on_next_start(false), then
restores. Two processes starting together can both readtruebefore either writesfalse.
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_idBoth 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
fix(restart): claim the reboot snapshot atomically so one restore wins (
KooshaPari commentedon Nov 28, 2026
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 viatimeout -k 2 25 script -qec "$KC" /dev/null, withKCODE_HOME=/tmp/k389-homeholding onerestart-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
0ae839431claim fix)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)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.