Standard: Backend locks and threads
Applies to
- All Rust code in
src-tauri/srcthat takes the app state lock (app::Shared, aMutex<AppData>) or any other mutex:- the overlay's internal lock
- the CLI sessions
- each Claude Code session's stderr tail
- the chat CLIs' private folder (
chat/cli/) - the tray's last-built menu (
SHOWNintray.rs) - the update that was found (
update::Found) - the last state file written (
WRITTENinapp.rs) - the main window's size limit and normal place (
SIZE_LIMIT,NORMAL_RECTinwindow.rs) - the chat CLIs' process groups (
GROUPSinchat/cli/launch.rs)
- That code lives in the commands the page calls (
commands.rs), chat (chat/), the agent bridge (agent/), the overlay thread (desktop/overlay.rs), the tray and windows (tray.rs,window.rs) and auto-update (update.rs). - Not covered:
- The app's CLI mode (
runinagent/cli.rs): a separate process with no app state. - Test code.
- The app's CLI mode (
Rules
Taking a lock
- Take the state with
app::lock, every other mutex withapp::hold. - Both accept a poisoned lock as is. The data stays whole (every change is made in place), so one panic does not fail every later request.
- Never write
.lock().unwrap()outside tests.
Never under Shared
- No runtime call that waits on the main thread:
- the monitor list (
available_monitors,primary_monitor) - the cursor position (
cursor_position) - menu and tray text changes (
set_text,set_tooltip,set_title) - window state queries (
is_visible,inner_size)
- the monitor list (
- No slow work: keychain reads and writes, network calls, file dialogs, waiting for an AI reply, waiting for a process to exit (
Child::wait). - No write of
state.json. A save is taken under the lock and written after it is let go:commitandapp::show_and_save(which takes the guard) show the change, let the lock go, then write.- A caller with more to do under the lock (a chat turn) calls
app::show_for_saveand writes theUnsavedit gets after dropping the guard. - A small change that comes often (a pat, time together, a scene picked, something moved) uses
app::show_and_save_soonorapp::commit_soon: shown at once, written byapp::flushon its own thread within 250 ms, together with any others, and at exit.- A change no page shows (
app::persist_soon) waits up to 2 s; a 250 ms ask meanwhile starts its own timer and writes both. - A failed write is tried again after 2 s, then twice as long each time up to a minute. The pages show
store.heldonce, and it goes once a write goes through. app::flushreturns only once no write is under way (it takesWRITTENlast), so the exit and an update's restart never cut a write short.
- A change no page shows (
- After you release the lock and take it again, check what may have changed in between: whether the friend is still there, whether a reply is already pending, whether the AI connection is the same. Example:
chat::still_readyin chat.
Lock order
- Holding both: take
Sharedfirst, the overlay's internal lock second. - Never take
Sharedwhile holding the overlay's internal lock. - The other locks are held briefly, and none is held when
Sharedis taken:- CLI sessions (
CliManager.sessionsinchat/cli/mod.rs):- Taken after
Sharedwhen a chat is reset or cancelled (chat::cancelholds the state). - Taken alone by the idle reaper and a chat turn.
- A turn takes its session out of the map before asking the CLI and puts it back afterwards, so the lock is never held during the wait for a reply.
- A dropped session kills its process and reaps it on its own thread.
- Taken after
- A Claude Code session's stderr tail (
StderrTailinchat/cli/claude.rs), taken alone:- by its reader thread, for each piece it reads
- by a turn that reads why the CLI quit; the turn waits on the tail for at most a second, with no other lock held
- The chat CLIs' private folder (
DIRinprivate_dir,chat/cli/launch.rs): taken alone and briefly, to return the folder or recreate it if it was removed. - The tray's
SHOWN:- Taken on the main thread while the menu is built from a snapshot (
tray::apply, queued withrun_on_main_thread). - Taken briefly by
rebuildand the e2e check. - The snapshot is read from
Sharedbefore, never under it.
- Taken on the main thread while the menu is built from a snapshot (
WRITTEN(the number of the last state file written):- Held while
state.jsonis written, so two writes never share the temporary file and an older save never replaces a newer one. - Never taken under
Shared. Every save (commit,show_and_save,show_for_save, a delayed save inapp::flush) reads the state underShared, releases it, then takesWRITTENto write.
- Held while
SAVE_LATER(app.rs, the save put off): taken briefly to ask for a save or take it, underSharedor alone; no other lock is taken while it is held.BOOK(problems.rs, the problems kept for a report): taken briefly to note or read one, alone, underWRITTEN(a failed write) or underShared; no other lock is taken while it is held.SIZE_LIMITandNORMAL_RECT(window.rs): read or written briefly on the main thread; never held across a window call.GROUPS(chat/cli/launch.rs): taken alone and briefly, to add, remove or stop the process groups of the CLIs the app started.- The update's
Found:- Taken only to store, read or take the update.
- The download and install run after the guard is dropped.
- A failed install puts the update back.
- CLI sessions (
Overlay and commands
- The overlay reads monitors from the cache (
current_monitors, refreshed outside the lock every 2 seconds). - Functions that can be called inside the lock, such as
layout(), do not call the runtime. commands::callruns the commands the page calls on a worker thread (spawn_blocking).- Inside a command, do not add a wait that blocks the main thread.
- A menu item that saves (the desktop, wander and sound switches, taking a friend in from the desktop) or waits on the main loop (
alwaysOnTop,fit) runs on a worker thread (spawn_blocking), not in the menu callback on the main thread.
Recommendations
Follow these when you can. When you cannot, say why in the PR.
- Inside the locked section, only read and write state. Build and send results after releasing the lock.
Exceptions
| When | How |
|---|---|
Code that already runs on the main thread (tray menu handling, window events) takes Shared briefly | Allowed. No deadlock as long as no other thread waits on the main thread while holding Shared; these rules keep that true |
| A runtime call cannot be moved outside the lock | Explain in the PR description why, and why there is no deadlock; a maintainer approves it |
Rationale
- The main thread also takes
Shared: sprite requests used to run on it, and the tray menu still does. - If another thread waits on the main thread while holding
Shared, the two wait on each other forever. - A performance review confirmed that state-change notifications took this path.
- The same cause made the keychain prompt freeze the whole app.
- Based on: decision “Never wait on the main thread while holding the app state lock”
Automated checks
- None. The compiler and clippy cannot tell whether a call runs inside a lock.
- Review PRs that change code taking locks against the rules above.
- E2E (
pnpm run test:e2e) only confirms that runs finish without deadlock; it does not catch rare overlapping timing.
Change history
- 2026-10-01 First version
- 2026-10-10
app::lockfor the state andapp::holdfor every other mutex, both recovering from a poisoned lock; where the CLI sessions, the tray, the mini window and the update lock sit relative toShared; the Claude Code stderr tail and the CLIs' private folder - 2026-11-14 No state write under
Shared:save_and_showgave way toshow_and_saveandshow_for_save;WRITTENis never taken underShared; the menu switches that save run on a worker thread; small frequent changes are written together within 250 ms - 2026-11-14 A failed put-off write is tried again less often and shown once;
app::flushwaits for a write under way;SAVE_LATER - 2026-11-29
BOOK; a command's save that failed is wanted again like a put-off one; the bridge file is written withoutShared