fix(desktop): close CloseTab-Snapshot/DeleteSession resurrection race#4406
Merged
esengine merged 1 commit intoJun 15, 2026
Merged
Conversation
…esengine#4384) CloseTab released a.mu before Snapshot() wrote the session file, so a concurrent DeleteSession could trash the file and the deferred Snapshot rewrote it — the deleted session 'resurrected.' Two vectors: 1. The explicit Snapshot() after delete(a.tabs). 2. The background tabSnapshotLoop holding a raw *WorkspaceTab pointer. Fix: after the final snapshot (and skipping the detach path), clear the controller's session path so future snapshots no-op, and drain any in-flight autosave loop via a closing flag + sync.Cond before CloseTab returns. Disk I/O stays outside a.mu. Adds regression tests proving no resurrection and that survivor tabs keep autosaving. Fixes esengine#4384.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4384
Problem
After deleting a session from the history panel, the session sometimes reappears. CloseTab removes the tab from a.tabs and releases the lock before Snapshot() writes the session file. A concurrent DeleteSession checks a.tabs, doesn't see the tab, moves the file to trash — then Snapshot() recreates it at the original path.
Two resurrection vectors:
Why not just move Snapshot() inside the lock
PR #4400 takes this approach. It works for Vector 1 but has two problems:
What this fix does
Three changes, all in desktop/tabs.go:
Clear the controller's session path after the final snapshot (SetSessionPath). This makes all future Snapshot() calls no-ops. Verified safe: snapshot() at controller.go:2000 captures path under lock, returns early when path is empty.
Drain any in-flight autosave goroutine via a closing flag and sync.Cond (quiesceTabAutosave). CloseTab sets closing=true and waits until the loop finishes its current write. After quiesceTabAutosave returns, no background goroutine can call Snapshot again.
scheduleTabSnapshot checks the closing flag before launching new work, so no new autosave loops start after CloseTab begins teardown.
The detach path (hasActiveRuntimeWork and detachSessionRuntime) is exempt — detached runtimes keep running and must keep saving.
Disk I/O stays outside a.mu. The lock is not held during any file write.
Files Changed
Verification
cd desktop and go build ./... and go vet ./...
cd desktop and go test -run TestCloseTab -v -count=1
cd desktop and go test -race -run TestCloseTab -v -count=1
cd desktop and go test -run TestTurnDone -v -count=1