Skip to content

Commit 42ceba4

Browse files
committed
Clean up usage.
1 parent 4d4db08 commit 42ceba4

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
- Connecting to the daemon and starting it are now a single operation, so a command that needs the
3434
daemon will start one itself if the background pre-warm hasn't yet, instead of silently running
3535
without it. Concurrent starts still coordinate so only one daemon is spawned.
36-
- When connecting, the client now checks the running daemon's moon and protocol version against its
37-
own and, on a mismatch, restarts it — so a daemon left over from before a `moon upgrade` is
36+
- When connecting, the client now checks the running daemon's moon and protocol version against
37+
its own and, on a mismatch, restarts it — so a daemon left over from before a `moon upgrade` is
3838
replaced instead of serving the old binary indefinitely.
3939
- **Processes**
4040
- Improved our "stream and capture output" child process handling to operate on bytes instead of

crates/app/src/session.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct MoonSession {
5353

5454
// Lazy components
5555
pub(crate) cache_engine: OnceLock<Arc<CacheEngine>>,
56-
// pub(crate) daemon_client: OnceCell<Option<DaemonClient>>,
56+
pub(crate) daemon_client: OnceLock<DaemonClient>,
5757
pub(crate) extension_registry: OnceCell<Arc<ExtensionRegistry>>,
5858
pub(crate) project_graph: OnceLock<Arc<ProjectGraph>>,
5959
pub(crate) task_graph: OnceLock<Arc<TaskGraph>>,
@@ -83,7 +83,7 @@ impl MoonSession {
8383
config_dir: PathBuf::new(),
8484
config_loader: ConfigLoader::default(),
8585
console: Console::new(cli.quiet || is_formatted_output()),
86-
// daemon_client: OnceCell::new(),
86+
daemon_client: OnceLock::new(),
8787
extensions_config: Arc::new(ExtensionsConfig::default()),
8888
extension_registry: OnceCell::new(),
8989
moon_env: Arc::new(MoonEnvironment::default()),
@@ -139,16 +139,19 @@ impl MoonSession {
139139
return Ok(None);
140140
}
141141

142-
// let client = self
143-
// .daemon_client
144-
// .get_or_try_init(async move || self.get_daemon_connector()?.acquire().await)
145-
// .await?;
142+
if let Some(client) = self.daemon_client.get() {
143+
return Ok(Some(client.to_owned()));
144+
}
145+
146+
let client = self.get_daemon_connector()?.acquire().await?;
146147

147-
// Ok(client.clone())
148+
// Only cache the client if we successfully connected to a daemon.
149+
// If we failed to connect, we don't want to cache so that we try again.
150+
if let Some(client) = &client {
151+
let _ = self.daemon_client.set(client.to_owned());
152+
}
148153

149-
// `acquire` connects to a running daemon or starts one, degrading to
150-
// `None` on failure, so there's nothing to handle here.
151-
self.get_daemon_connector()?.acquire().await
154+
Ok(client)
152155
}
153156

154157
pub async fn create_workspace_graph_context(&self) -> miette::Result<WorkspaceBuilderContext> {
@@ -476,7 +479,9 @@ impl AppSession for MoonSession {
476479
// `connect_to_daemon`, so the two coordinate and at most one spawns —
477480
// and a spawn failure degrades to `None` instead of failing the run.
478481
if self.is_daemon_allowed() {
479-
self.get_daemon_connector()?.acquire().await?;
482+
if let Some(client) = self.get_daemon_connector()?.acquire().await? {
483+
let _ = self.daemon_client.set(client);
484+
}
480485
}
481486

482487
Ok(None)

crates/daemon-client/src/daemon_client.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,26 @@ impl DaemonClient {
103103
debug!(endpoint = endpoint, "Connecting to daemon");
104104

105105
let channel = match timeout(CONNECT_TIMEOUT, connect_channel(&endpoint)).await {
106-
Ok(Ok(channel)) => channel,
106+
Ok(Ok(channel)) => {
107+
debug!(endpoint = endpoint, "Connected to daemon");
108+
109+
channel
110+
}
107111
Ok(Err(error)) => {
112+
debug!(
113+
endpoint = endpoint,
114+
error = error.to_string(),
115+
"Failed to connect to daemon"
116+
);
117+
108118
return Err(DaemonClientError::ConnectFailed {
109119
endpoint,
110120
error: Box::new(error),
111121
});
112122
}
113123
Err(_) => {
124+
debug!(endpoint = endpoint, "Timed out connecting to daemon");
125+
114126
return Err(DaemonClientError::ConnectTimedOut {
115127
endpoint,
116128
timeout_secs: CONNECT_TIMEOUT.as_secs(),
@@ -147,6 +159,8 @@ impl DaemonClient {
147159
/// A failure to read status keeps the daemon rather than churning on a
148160
/// transient error — a real RPC will surface any genuine problem.
149161
pub async fn handshake(&mut self, client_version: &str) -> HandshakeOutcome {
162+
debug!("Initiating handshake with daemon");
163+
150164
match self.status().await {
151165
Ok(status) => {
152166
let outcome = if status.protocol_version == PROTOCOL_VERSION
@@ -159,9 +173,9 @@ impl DaemonClient {
159173

160174
if outcome == HandshakeOutcome::Restart {
161175
debug!(
162-
daemon_version = status.moon_version,
176+
server_version = status.moon_version,
163177
client_version,
164-
daemon_protocol = status.protocol_version,
178+
server_protocol = status.protocol_version,
165179
client_protocol = PROTOCOL_VERSION,
166180
"Daemon version handshake mismatch"
167181
);

0 commit comments

Comments
 (0)