Repository navigation
fix(daemon/macos): take over stale daemon lock after logout/login - #2749
Merged
AucaCoyan merged 1 commit intoJul 24, 2026
Merged
Conversation
On macOS, a daemon from a previous login session can survive a logout/login (or fast user switching), holding the daemon lock while no longer serving the current session. A new daemon then failed to acquire the lock and exited with "daemon is already running!", so espanso appeared running but expansions were dead until the stale process was killed by hand. The daemon now takes over on macOS: it asks the existing daemon to exit via IPC and waits for the lock to release; if the holder is wedged and won't release, it clears the stale lock file and acquires a fresh one. The existing worker-termination path then cleans up the old worker, so no orphaned processes accumulate across relogins. Closes espanso#1492.
Collaborator
|
Thank you! |
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.
Problem
As reported in #1492.
On macOS, espanso can appear to be running after login, but expansions don't work. I assume it would apply to fast user switch as well, but did not verify this myself.
espanso statusreports a running instance, but nothing expands until the processis manually killed and restarted.
A
daemonprocess from the previous login session seems to survive into the new session. It holds the daemon lock, but isn't serving the current session. When the new session starts espanso, the new daemon can't acquire the lock, logsdaemon is already running!, and exits. Because the stale daemon is still there, repeated logins leave several dead espanso processes behind as noted in the issue.Root cause
The daemon lock (
espanso/src/lock.rs) is an advisoryflockheld by an open file descriptor, so the OS releases it automatically when the holding process dies. A lock that is still held implies a live process is holding it.On macOS, the previous session's
daemonis not reliably torn down on logout, so it stays alive into the next session and keeps holding the lock. Indaemon_main(espanso/src/cli/daemon/mod.rs), the startup path did:So the new session's daemon could never acquire the lock from the stale holder: it returned
DAEMON_ALREADY_RUNNINGand exited. Nothing tried to reclaim the lock or to shut the stale daemon down so dead processes may accumulate.Fix
On macOS, when the daemon lock is already held, the new daemon now tries to take over from the (presumed stale) holder instead of giving up. It:
IPCEvent::Exit) and wait up to 3s for it to release the lock, then acquire it. A live, responsive daemon shuts down cleanly and hands off.terminate_worker_if_already_runningstep then shuts down the old session's worker, so no daemon/worker processes accumulate across relogins.I tried to mirror the existing
terminate_worker_if_already_runningpattern (same 3s / 200ms poll), applied to the daemon instead of the worker.The change is gated behind
#[cfg(target_os = "macos")], so Linux and Windows startup behavior is unchanged. Supporting additions:lock::clear_daemon_lock(removes the stale daemon lock file) and a smalllock_file_pathhelper / name-constant refactor so the path is defined once.ipc::create_ipc_client_to_daemon, mirroring the existingcreate_ipc_client_to_worker.Testing
A unit test in
lock.rscovers the take-over mechanism: an orphan holds the lock, and clearing the stale lock file lets a new lock be acquired while the orphan's now-unlinked file is still held.Manual test on macOS with two real
espanso daemonprocesses against isolated--config_dir/--runtime_dirdirs:daemon lock is held, attempting to take over from a possibly stale daemon...then proceeds. Daemon A exits and its worker self-exits (detected unexpected daemon termination... exiting worker process). Result: exactly one daemon + one worker, no leftovers.SIGSTOPso it can't answer IPC; starting daemon B waits ~3s, logsstale daemon did not release the lock, clearing the stale lock file, then takes over.daemon is already running!.Not exercised: a literal macOS logout/login cycle. The two-process test drives the exact code path a relogin hits (lock held by a live daemon → take over).
On CI: out of an abundance of caution I did not enable GitHub Actions on my fork, so the workflows have not run against these commits. I reproduced the
macosjob locally on Apple Silicon:cargo fmt --all -- --checkcargo test --workspacecargo clippy -- --deny warningsAll passed.
I did not verify other platforms. I'll leave that to CI.
Note
As mentioned, this changes is scoped intentionally to macOS. The bug is macOS-specific (the previous session's daemon surviving a logout/login), reproduction needs a Mac, and gating keeps Linux/Windows startup untouched. I'll be happy to generalize the mechanism if maintainers would prefer a unified, cross-platform fix.
Related: #2725 also touches
daemon/mod.rsand reworks the worker-exit handling. This change appears independent (a different bug, and a different region of the file: lock acquisition + a new take-over helper), so I don't anticipate any conflict ondev, but whichever lands second may need a trivial rebase.While verifying, I noticed a pre-existing, separate quirk: a graceful daemon shutdown over IPC logs
received unexpected exit code from worker 0, exitingat ERROR level even though it exits cleanly. It sits in the same exit-handling code #2725 is reworking, so I left it out of this PR.Closes #1492.