Stop the Claude Code daemon on switch so auth takes effect
Recent Claude Code releases keep a background daemon that caches each account's auth in memory, so a profile switch didn't take effect until the daemon exited. Every switch now stops the daemon supervisor first via `claude daemon stop --any --keep-workers`, letting the next session re-read the restored credentials while detached background sessions keep running. Best-effort: an older Claude Code, a missing binary, or no running daemon never blocks the switch. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
6 files changed
+84
-2
No files matched your search
@@ -8,6 +8,19 @@ All notable changes to ccswitch are documented here. The format follows
|
||||
|
||||
_Nothing yet._
|
||||
|
||||
## [0.1.1] — 2026-07-08
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Daemon-aware switching** — recent Claude Code releases keep a background
|
||||
daemon running that caches each account's auth in memory, so a profile switch
|
||||
did not take effect until the daemon exited. Every switch (`use` and the bare
|
||||
`ccswitch <name>` form) now stops the daemon's supervisor first via `claude
|
||||
daemon stop --any --keep-workers`, so the next session re-reads the restored
|
||||
credentials while any detached background sessions keep running. It is
|
||||
best-effort: an older Claude Code, a missing binary, or no running daemon
|
||||
never blocks the switch (a warning is printed and the switch proceeds).
|
||||
|
||||
## [0.1.0] — 2026-07-08
|
||||
|
||||
First release of the Rust CLI, superseding the original fish plugin (now under
|
||||
|
||||
Generated
+1
-1
@@ -60,7 +60,7 @@ checksum = "2a4385e2e34eb35d6b3efe798b9eb88096925d87726c0798709bf56d9ed84af3"
|
||||
|
||||
[[package]]
|
||||
name = "ccswitch"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ccswitch"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
description = "Switch between multiple Claude Code accounts"
|
||||
license = "MIT"
|
||||
|
||||
@@ -327,6 +327,12 @@ plaintext tokens to a shared or public repo.
|
||||
- Quit any running `claude` session before switching — an open session can
|
||||
rewrite `~/.claude.json` on exit and clobber the swap. `ccswitch` warns when
|
||||
it detects one.
|
||||
- Recent Claude Code releases keep a background **daemon** that caches each
|
||||
account's auth in memory. On every switch `ccswitch` stops its supervisor
|
||||
(`claude daemon stop --any --keep-workers`) so the next session re-reads the
|
||||
restored credentials, while any detached background sessions keep running.
|
||||
This is best-effort: on an older Claude Code, or when no daemon is running, it
|
||||
is a no-op and the switch proceeds regardless.
|
||||
- Access tokens expire, but the refresh token is restored too, so Claude Code
|
||||
re-refreshes automatically after a switch.
|
||||
- OAuth refresh tokens rotate on every use. To keep snapshots valid, `ccswitch`
|
||||
|
||||
+52
@@ -248,6 +248,15 @@ pub trait System {
|
||||
/// Whether a `claude` process is currently running.
|
||||
fn claude_is_running(&self) -> bool;
|
||||
|
||||
/// Stop the Claude Code daemon's supervisor so the next session re-reads the
|
||||
/// restored credentials, while leaving detached background sessions running
|
||||
/// (`claude daemon stop --any --keep-workers`). Best-effort.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the daemon control command could not be run.
|
||||
fn stop_daemon(&self) -> Result<()>;
|
||||
|
||||
/// Create (or replace) a symlink at `link` pointing to `target`.
|
||||
///
|
||||
/// # Errors
|
||||
@@ -536,6 +545,12 @@ impl<'a> App<'a> {
|
||||
"warning: a running 'claude' may overwrite ~/.claude.json on exit; quit it first"
|
||||
)?;
|
||||
}
|
||||
// A running daemon caches the account's auth; stop its supervisor so the
|
||||
// next session picks up the restored credentials (detached background
|
||||
// sessions keep running). Best-effort — never block the switch on it.
|
||||
if let Err(e) = self.system.stop_daemon() {
|
||||
writeln!(io.err, "warning: could not stop the claude daemon: {e}")?;
|
||||
}
|
||||
let account = self.switcher().activate(name)?;
|
||||
writeln!(io.out, "switched to '{name}' ({})", email_of(&account))?;
|
||||
Ok(())
|
||||
@@ -929,9 +944,11 @@ mod tests {
|
||||
pick_err: bool,
|
||||
symlink_err: bool,
|
||||
exec_err: bool,
|
||||
daemon_stop_err: bool,
|
||||
links: RefCell<Vec<(PathBuf, PathBuf)>>,
|
||||
execs: RefCell<Vec<ExecCall>>,
|
||||
sessions_scope: RefCell<Vec<String>>,
|
||||
daemon_stops: RefCell<u32>,
|
||||
}
|
||||
|
||||
impl System for FakeSystem {
|
||||
@@ -947,6 +964,13 @@ mod tests {
|
||||
fn claude_is_running(&self) -> bool {
|
||||
self.running
|
||||
}
|
||||
fn stop_daemon(&self) -> Result<()> {
|
||||
*self.daemon_stops.borrow_mut() += 1;
|
||||
if self.daemon_stop_err {
|
||||
return Err(Error::Invalid("daemon".to_string()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn make_symlink(&self, target: &Path, link: &Path) -> Result<()> {
|
||||
if self.symlink_err {
|
||||
return Err(Error::Invalid("symlink".to_string()));
|
||||
@@ -1665,6 +1689,34 @@ mod tests {
|
||||
.unwrap();
|
||||
assert!(bufs.err().contains("warning: a running 'claude'"));
|
||||
assert!(bufs.out().contains("switched to 'dev' (a@example.com)"));
|
||||
// The daemon supervisor is stopped as part of the switch.
|
||||
assert_eq!(*system.daemon_stops.borrow(), 1);
|
||||
fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn use_warns_but_still_switches_when_daemon_stop_fails() {
|
||||
let dir = temp_dir();
|
||||
write_config(&dir, &config_json("B", "OB", "b@x", "Org B"));
|
||||
let store = Store::new(dir.join("accounts"));
|
||||
seed_profile(&store, "dev", "A", "OA", "a@example.com");
|
||||
let creds = FakeCreds::with(Some("LIVE"), Some("attr"));
|
||||
let system = FakeSystem {
|
||||
daemon_stop_err: true,
|
||||
..Default::default()
|
||||
};
|
||||
let app = app_env(&system, &creds, &store, &dir);
|
||||
let mut bufs = Bufs::new();
|
||||
app.dispatch(
|
||||
&mut bufs.io(),
|
||||
Command::Use {
|
||||
name: "dev".to_string(),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(bufs.err().contains("could not stop the claude daemon"));
|
||||
assert!(bufs.out().contains("switched to 'dev' (a@example.com)"));
|
||||
assert_eq!(*system.daemon_stops.borrow(), 1);
|
||||
fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,17 @@ impl System for RealSystem {
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn stop_daemon(&self) -> Result<()> {
|
||||
// Best-effort: an old Claude Code without the daemon, a missing binary,
|
||||
// or no daemon running are all fine — never fail the switch on this.
|
||||
let _ = Proc::new("claude")
|
||||
.args(["daemon", "stop", "--any", "--keep-workers"])
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn make_symlink(&self, target: &Path, link: &Path) -> Result<()> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user