cua icon indicating copy to clipboard operation
cua copied to clipboard

[Bug]: macOS cua-driver mcp starts a standard-mode daemon while the LaunchAgent reloads

Open FredAmartey opened this issue 2 months ago • 4 comments

Primary area

Cua Driver

Summary

On macOS, cua-driver mcp starts its own daemon with open -n -g -a CuaDriver --args serve whenever the socket is down, even when the LaunchAgent from the operate guide manages the daemon. An MCP client that starts during the guide's unload and load step gets a daemon in standard mode, and the agent's own daemon then exits with "already running" on every respawn. On Linux and Windows the same path tries the managed service first.

Reproduction

  1. Install the LaunchAgent from the operate guide with mode flags after serve (here --permission-mode unrestricted --dangerously-bypass-approvals).
  2. launchctl bootout gui/$UID/com.trycua.cua-driver (the guide's unload).
  3. Start any MCP client of the stock binary; here, one initialize request to cua-driver mcp.
  4. launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.trycua.cua-driver.plist (the guide's load).
  5. cua-driver status.

1 of 1 deliberate runs. I first hit it by accident on 0.28.2 while reloading the agent.

Expected behavior

The pinned mode survives: the client restarts the managed agent, as the systemd and scheduled task paths try to, or refuses and says to start it. The operate guide says "A bad configuration fails startup instead of falling back to standard."

Actual behavior

 0.0 s  status: pid 30110, permission mode: unrestricted (trusted_startup_configuration)
 2.1 s  after bootout: Cua Driver daemon is not running
 2.6 s  cua-driver mcp answers initialize
        status: pid 28513, permission mode: standard (built_in_default)
        ps: 28513, ppid 1, /Applications/CuaDriver.app/Contents/MacOS/cua-driver serve
 7.7 s  after bootstrap: still pid 28513, standard (built_in_default)
        launchctl print: state = spawn scheduled, last exit code = 1
        agent stderr: Cua Driver daemon is already running on .../cua-driver.sock (pid 28513)

The agent keeps respawning and exiting until the other daemon is stopped.

Environment

Cua Driver 0.33.3 release app. LaunchAgent com.trycua.cua-driver as in the operate guide, plus mode flags. macOS 27.0 (26A428), Apple silicon.

Evidence

The macOS launch_daemon_with_state_and_wait ignores _allow_managed_restart and always opens the app with a bare serve: https://github.com/trycua/cua/blob/2b3e4e548c6d27f918f5367f84c094151aa10c8f/libs/cua-driver/rust/crates/cua-driver/src/cli.rs#L1313-L1319

The Linux and Windows version tries restart_managed_daemon_if_present first: https://github.com/trycua/cua/blob/2b3e4e548c6d27f918f5367f84c094151aa10c8f/libs/cua-driver/rust/crates/cua-driver/src/cli.rs#L1421-L1437

Workaround and additional context

cua-driver stop, then launchctl kickstart -k gui/$UID/com.trycua.cua-driver.

One open question for a fix is the label: the operate guide's agent is com.trycua.cua-driver, while #2196 and #2990 use com.trycua.driver.

Submission checks

  • [x] I searched for duplicate issues and active pull requests.
  • [x] This issue describes a single problem.
  • [x] I removed credentials, private data, sensitive screenshots, and vulnerability details from this public report.

FredAmartey avatar Oct 05 '26 03:10 FredAmartey

Thanks for the report, @FredAmartey. I've labelled it and assigned @injaneity to take a look.

f-trycua avatar Oct 06 '26 13:10 f-trycua

Another data point on the same mechanism, and what it costs when the LaunchAgent has KeepAlive.

Setup: cua-driver 0.25.0, macOS 27.0.1 (arm64). ~/Library/LaunchAgents/com.trycua.cua-driver.plist has ProgramArguments cua-driver serve, RunAtLoad and KeepAlive both true.

What happened: shortly after login, a daemon was started through LaunchServices. runningboardd logged an LS launch com.trycua.driver, and the job showed up as application.com.trycua.driver.*, not under the LaunchAgent's label. We couldn't tell from the logs what issued that launch. A few minutes later, the LaunchAgent's own serve was bootstrapped and exited 1:

Cua Driver daemon is already running ... (pid 5640). Run `cua-driver stop` first.

Because of KeepAlive, launchd respawned it about every 10 s (its throttle interval). ~/.cua-driver/serve.err.log ended up with 5,966 of those lines, all naming the same pid, over about 21 hours, until the job was unloaded. The daemon that was already running kept working the whole time, so nothing looked broken. The only symptom was the extra load.

Workaround on our side: the script that starts the agent now skips the bootstrap when cua-driver serve is already running under any label, and boots out the agent's job if it keeps respawning.

Possible fix upstream: when serve finds a live daemon, it could exit 0 instead of 1. launchd's KeepAlive would still restart it, though, so it may be better to make KeepAlive conditional (SuccessfulExit = false) or to have serve wait on the existing daemon instead of exiting. Either way, an "already running" exit under the shipped LaunchAgent turns into a respawn loop that nobody notices.

djbclark avatar Oct 19 '26 01:10 djbclark

Follow-up from the same machine: after a clean reboot the workaround held. Our boot-time launcher now skips the LaunchAgent when a cua-driver serve is already running under another label, and the "already running" count in serve.err.log stayed flat (5971 before and after the reboot, versus ~5,966 respawns over ~21 h earlier). So the loop is avoidable from the launcher side, but the underlying cause in this issue (cua-driver mcp starting its own daemon via open -n -g -a CuaDriver --args serve while the LaunchAgent has KeepAlive) is still there.

djbclark avatar Oct 19 '26 14:10 djbclark

#5926 fixes this. When the LaunchAgent is installed, cua-driver mcp starts it through launchd instead of opening a second daemon, and if the agent is installed but not loaded it errors with the launchctl load command to run.

FredAmartey avatar Nov 22 '26 09:11 FredAmartey