1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//! What installing rho does about the daemon that is running (#1666).
//!
//! `rho update` leaves the daemon on the new binary without cutting work
//! short. Where the installer wrote over the daemon's executable, the daemon
//! notices the new file itself and re-executes it once nothing would be cut
//! short (#1713), and the update waits to say which of the two happened.
//! Where the new version went to another path, or the daemon holds its file
//! against replacement, an idle daemon is stopped instead, so the next
//! client starts the new one; a busy daemon is left alone, with what to run
//! once it is done. `rho uninstall` stops the daemon before it removes
//! anything.
//!
//! Whether a running daemon's file can be replaced comes from the daemon's
//! platform boundary ([`daemon::REPLACEABLE_WHILE_RUNNING`]); nothing here
//! branches on the operating system.
use std::{io::Write, time::Duration};
use rho_protocol::{methods, ClientError, PendingUpgrade, StatusParams};
use super::daemon_cli;
use crate::daemon::{
self, ConnectAs, ConnectError, DaemonPaths, Executable, LockHolder, DAEMON_RESTART_TIMEOUT,
};
/// How long `rho update` waits for the daemon to notice the executable it
/// just installed.
//
// Tripwire. The daemon looks at its executable every `UPGRADE_CHECK_INTERVAL`
// (1 s, in `daemon_host::upgrade`), runs the new one with `--version`, and
// restarts. Measured with the debug binary, all of `rho update` with a
// stand-in installer and an idle daemon one full interval from its next
// check: 1.02 s median (1.03 s max) over 20 runs idle, and 1.06 s median
// (1.08 s max) over 8 runs with all 12 cores saturated. A daemon that says
// nothing for 5 s was not installed over.
const UPGRADE_NOTICE_TIMEOUT: Duration = Duration::from_secs(5);
/// Interval between `rho/daemon/status` requests while the update waits.
/// Each is one request on a local endpoint.
const STATUS_POLL_INTERVAL: Duration = Duration::from_millis(50);
/// Where an installer put the new rho.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Installed {
/// At the path the old one had, as the install script, Cargo, and
/// pacman do.
OverTheExecutable,
/// In a directory of its own, as mise and Scoop keep each version.
AtAnotherPath,
}
/// The daemon as `rho update` found it before installing, to follow once
/// the installer ran.
pub(crate) struct Watched {
/// The rho that runs the update, by the path an installer writes over.
updating: Option<Executable>,
daemon: Before,
}
/// What ran before the installer did.
enum Before {
/// No daemon, or one already dealt with.
Nothing,
/// A connection to the daemon, held while the installer runs: the
/// daemon closes it as it restarts, however long the installer goes on
/// after writing the new binary.
Running(Box<daemon::Connection>),
/// A daemon of another protocol, which answers no request.
OtherProtocol,
}
/// Finds the running daemon before `rho update` installs. Where the daemon
/// would keep the installer from replacing the executable, an idle one is
/// stopped, and `output` says how to stop a busy one.
pub(crate) async fn before_update(output: &mut impl Write) -> anyhow::Result<Watched> {
let paths = DaemonPaths::of_this_process()?;
let daemon = if daemon::REPLACEABLE_WHILE_RUNNING {
match daemon::connect(&paths, ConnectAs::Command.hello()).await {
Ok(connection) => Before::Running(Box::new(connection)),
Err(ConnectError::NotRunning) => Before::Nothing,
Err(ConnectError::Handshake(_)) => Before::OtherProtocol,
Err(error) => return Err(error.into()),
}
} else {
make_way(&paths, output).await?;
Before::Nothing
};
Ok(Watched {
updating: Executable::of_this_process().ok(),
daemon,
})
}
/// Stops an idle daemon so an installer can replace the executable it
/// holds; says how to stop one that is busy.
async fn make_way(paths: &DaemonPaths, output: &mut impl Write) -> anyhow::Result<()> {
match stop_if_idle(paths).await? {
Retired::NotRunning => {}
Retired::Stopped => writeln!(
output,
"stopped the idle rho daemon so the installer can replace the rho executable; the next rho command starts it again"
)?,
Retired::Busy { pid, in_progress, .. } => writeln!(
output,
"the rho daemon (pid {pid}) keeps the rho executable open and has {in_progress} {IN_PROGRESS}; once that finished, run `rho daemon stop` before the command below"
)?,
Retired::OtherProtocol => writeln!(
output,
"a rho daemon of another version is running and keeps the rho executable open; run `rho daemon stop` before the command below"
)?,
}
Ok(())
}
/// What a daemon's count of work in progress counts, after the number.
const IN_PROGRESS: &str = "job(s), login(s), or session task(s) in progress";
impl Watched {
/// Moves the daemon to the rho the update `installed`, or says when it
/// moves. Nothing is interrupted: a daemon with work in progress is
/// left to finish it.
pub(crate) async fn follow(
self,
installed: Installed,
output: &mut impl Write,
) -> anyhow::Result<()> {
// Nothing was installed over a daemon that holds its executable.
if !daemon::REPLACEABLE_WHILE_RUNNING {
return Ok(());
}
let paths = DaemonPaths::of_this_process()?;
match installed {
Installed::OverTheExecutable => {}
Installed::AtAnotherPath => {
drop(self.daemon);
return retire(&paths, output).await;
}
}
// An installer that found nothing newer leaves this rho's file as
// it was, and the daemon as it is.
if self
.updating
.is_some_and(|updating| updating.replacement().is_none())
{
return Ok(());
}
let daemon::Connection {
client,
mut notifications,
hello,
} = match self.daemon {
Before::Nothing => return Ok(()),
Before::OtherProtocol => {
writeln!(
output,
"a rho daemon of another version is still running; run `rho daemon restart` to move it to the updated rho, which interrupts the work in progress"
)?;
return Ok(());
}
Before::Running(connection) => *connection,
};
let rho_protocol::DaemonInfo { pid, version, .. } = hello.daemon;
match noticed(&client, &mut notifications).await {
Noticed::Restarting => {
drop(client);
restarted(&paths, pid, output).await
}
Noticed::Waiting(PendingUpgrade {
version,
waiting_for,
}) => {
writeln!(
output,
"rho daemon (pid {pid}) restarts onto rho {version} once its {waiting_for} {IN_PROGRESS} finish; nothing is interrupted"
)?;
Ok(())
}
Noticed::Nothing => {
writeln!(
output,
"rho daemon (pid {pid}, version {version}) did not restart within UPGRADE_NOTICE_TIMEOUT ({UPGRADE_NOTICE_TIMEOUT:?}): it runs another rho executable than the one updated. Run `rho daemon restart` from the updated rho to move it over, which interrupts the work in progress"
)?;
Ok(())
}
}
}
}
/// Stops an idle daemon that cannot re-execute the updated rho, so the next
/// client starts it; says what to run for one that is busy.
async fn retire(paths: &DaemonPaths, output: &mut impl Write) -> anyhow::Result<()> {
match stop_if_idle(paths).await? {
Retired::NotRunning => {}
Retired::Stopped => writeln!(
output,
"stopped the idle rho daemon; the next rho command starts it from the updated rho"
)?,
Retired::Busy {
pid,
version,
in_progress,
} => writeln!(
output,
"rho daemon (pid {pid}) still runs rho {version} and has {in_progress} {IN_PROGRESS}; once that finished, run `rho daemon restart` to move it to the updated rho"
)?,
Retired::OtherProtocol => writeln!(
output,
"a rho daemon of another version is still running; run `rho daemon restart` to move it to the updated rho, which interrupts the work in progress"
)?,
}
Ok(())
}
/// What [`stop_if_idle`] found and did.
enum Retired {
NotRunning,
/// Nothing was in progress, and it was stopped.
Stopped,
/// It was left to its work.
Busy {
pid: u32,
version: String,
in_progress: u64,
},
/// It speaks a protocol this rho cannot, so it could not say what it
/// runs and was left alone.
OtherProtocol,
}
/// Stops the daemon unless a stop would interrupt something: what its
/// upgrade in place waits for too. Work that starts between the check and
/// the stop is recorded interrupted, as by any stop.
async fn stop_if_idle(paths: &DaemonPaths) -> anyhow::Result<Retired> {
let connection = match daemon::connect(paths, ConnectAs::Command.hello()).await {
Ok(connection) => connection,
Err(ConnectError::NotRunning) => return Ok(Retired::NotRunning),
Err(ConnectError::Handshake(
ClientError::VersionMismatch(_) | ClientError::UnreadableHello(_),
)) => return Ok(Retired::OtherProtocol),
Err(error) => return Err(error.into()),
};
let in_progress = connection
.client
.request::<methods::Status>(&StatusParams {})
.await?
.in_progress;
let daemon = connection.hello.daemon;
drop(connection.client);
if in_progress > 0 {
return Ok(Retired::Busy {
pid: daemon.pid,
version: daemon.version,
in_progress,
});
}
daemon_cli::stop(paths).await?;
Ok(Retired::Stopped)
}
/// What the daemon did about its replaced executable.
enum Noticed {
/// It closed this connection to restart.
Restarting,
/// It waits for work in progress, then restarts.
Waiting(PendingUpgrade),
/// It said nothing in time: its executable is not the one installed
/// over.
Nothing,
}
/// Waits until the daemon restarts or reports the upgrade waiting for work,
/// for at most [`UPGRADE_NOTICE_TIMEOUT`].
async fn noticed(
client: &rho_protocol::Client,
notifications: &mut rho_protocol::Notifications,
) -> Noticed {
let deadline = tokio::time::Instant::now() + UPGRADE_NOTICE_TIMEOUT;
let mut polls = tokio::time::interval(STATUS_POLL_INTERVAL);
loop {
tokio::select! {
notification = notifications.recv() => {
if notification.is_none() {
return Noticed::Restarting;
}
}
_ = polls.tick() => {
// A request the restart cut short fails; the closed
// connection then says so above.
let Ok(status) = client.request::<methods::Status>(&StatusParams {}).await else {
continue;
};
// An upgrade waiting for nothing restarts at its next
// check, so keep watching for that.
if let Some(upgrade) = status.upgrade.filter(|upgrade| upgrade.waiting_for > 0) {
return Noticed::Waiting(upgrade);
}
}
() = tokio::time::sleep_until(deadline) => return Noticed::Nothing,
}
}
}
/// Waits for the daemon that began restarting to answer, and reports it.
async fn restarted(paths: &DaemonPaths, pid: u32, output: &mut impl Write) -> anyhow::Result<()> {
let restarted = tokio::time::timeout(
DAEMON_RESTART_TIMEOUT,
daemon::connect_restarting(paths, ConnectAs::Command.hello()),
)
.await;
match restarted {
Ok(Ok(connection)) => {
let daemon = connection.hello.daemon;
writeln!(
output,
"rho daemon restarted onto the updated rho (pid {}, version {})",
daemon.pid, daemon.version
)?;
Ok(())
}
// This is still the rho that was replaced, so a daemon of the new
// one may speak a protocol it cannot: it restarted all the same.
Ok(Err(ConnectError::Handshake(ClientError::VersionMismatch(mismatch)))) => {
writeln!(
output,
"rho daemon restarted onto the updated rho (protocol {})",
mismatch.daemon
)?;
Ok(())
}
Ok(Err(error)) => Err(anyhow::Error::new(error).context(format!(
"rho daemon (pid {pid}) did not come back after restarting; see {}",
paths.log_file.display()
))),
Err(_) => anyhow::bail!(
"rho daemon (pid {pid}) did not answer within DAEMON_RESTART_TIMEOUT ({DAEMON_RESTART_TIMEOUT:?}) of restarting; see {}",
paths.log_file.display()
),
}
}
/// The daemon `rho uninstall` has to stop.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct RunningDaemon {
/// Unknown while the daemon is starting or already stopping.
pub(crate) pid: Option<u32>,
}
/// This rho home's running daemon, as its lock shows it: no request is
/// sent, so a daemon of any protocol is found. A rho home that cannot be
/// resolved has none this process could stop.
pub(crate) fn running_daemon() -> Option<RunningDaemon> {
let paths = DaemonPaths::of_this_process().ok()?;
match daemon::lock_holder(&paths) {
LockHolder::Nobody => None,
LockHolder::Unrecorded => Some(RunningDaemon { pid: None }),
LockHolder::Daemon(record) => Some(RunningDaemon {
pid: Some(record.pid),
}),
}
}
/// Stops this rho home's daemon as `rho daemon stop` does and waits for it
/// to exit. One that exited meanwhile counts as stopped.
pub(crate) async fn stop_for_uninstall() -> anyhow::Result<()> {
let paths = DaemonPaths::of_this_process()?;
match daemon_cli::stop(&paths).await {
Err(_) if daemon::lock_holder(&paths) == LockHolder::Nobody => Ok(()),
stopped => stopped,
}
}