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
use std::sync::mpsc::{Sender, channel};
use crate::jobs::{Start, Stream, StreamPoll};
use crate::remote::AdoptedMaster;
#[derive(Debug)]
pub enum InstallEvent {
Log(String),
/// A croft is already installed on the remote, so the user can be
/// dropped into it now while the (re)install continues in the
/// background. Fired at most once, before `Done`.
CanLaunch,
/// The fast cross-build path is unavailable and the installer wants to
/// compile on the remote box instead (slow). The UI must answer via
/// `respond_fallback`: `true` runs the remote compile, `false` aborts
/// the install so the user can run `croft setup-cross` and reconnect
/// for the fast path. Fired only while the install still blocks the
/// connect (never after `CanLaunch`).
FallbackPrompt {
reason: String,
},
Done(Result<(), String>),
}
/// What an install whose thread crashed before it finished says, in the
/// connect dialog and on the status line.
pub const CRASHED_DETAIL: &str = "the install's thread crashed";
pub struct InstallSession {
pub host: String,
pub path: Option<String>,
pub adopted: Option<AdoptedMaster>,
/// The install's progress, until it ends or its thread dies.
events: Option<Stream<InstallEvent>>,
fallback_answer_tx: Sender<bool>,
}
/// The install itself: [`crate::remote::install_only_streaming`], or a
/// test's stand-in. It sends its log lines and its "can launch" signal on the
/// two senders, and asks the fallback question through the callback.
type Install = fn(
AdoptedMaster,
Sender<String>,
Sender<()>,
&mut dyn FnMut(&str) -> bool,
) -> anyhow::Result<AdoptedMaster>;
impl InstallSession {
/// Run the install as the `remote-install` stream. Fails only when no
/// thread can be started, and then closes the control master it was
/// handed, which nothing else will use.
pub fn start(
adopted: AdoptedMaster,
host: String,
path: Option<String>,
) -> std::io::Result<Self> {
Self::start_with(adopted, host, path, crate::remote::install_only_streaming)
}
fn start_with(
adopted: AdoptedMaster,
host: String,
path: Option<String>,
install: Install,
) -> std::io::Result<Self> {
let adopted_for_thread = adopted.clone();
let (fallback_answer_tx, fallback_answer_rx) = channel::<bool>();
let spawned = Stream::spawn("remote-install", Start::Now, move |events| {
let (log_tx_for_install, log_rx) = channel::<String>();
let log_forwarder = {
let events = events.clone();
std::thread::spawn(move || {
while let Ok(line) = log_rx.recv() {
if !events.send(InstallEvent::Log(line)) {
break;
}
}
})
};
let (can_launch_tx, can_launch_rx) = channel::<()>();
let can_launch_forwarder = {
let events = events.clone();
std::thread::spawn(move || {
if can_launch_rx.recv().is_ok() {
events.send(InstallEvent::CanLaunch);
}
})
};
let mut confirm_fallback = move |reason: &str| {
if !events.send(InstallEvent::FallbackPrompt {
reason: reason.to_string(),
}) {
return false;
}
// Blocks until the dialog answers. A torn-down dialog drops
// the answer sender, which reads as a decline.
fallback_answer_rx.recv().unwrap_or(false)
};
let result = install(
adopted_for_thread,
log_tx_for_install,
can_launch_tx,
&mut confirm_fallback,
);
// The install has let go of both senders, so the forwarders end
// once they have passed on what it sent: its last log lines and
// its "can launch" always arrive before its outcome (#2605).
let _ = log_forwarder.join();
let _ = can_launch_forwarder.join();
let payload = match result {
Ok(_) => Ok(()),
Err(e) => Err(format!("{e:#}")),
};
events.send(InstallEvent::Done(payload));
});
let events = match spawned {
Ok(events) => events,
Err(e) => {
crate::remote::close_master(adopted);
return Err(e);
}
};
Ok(Self {
host,
path,
adopted: Some(adopted),
events: Some(events),
fallback_answer_tx,
})
}
/// The events sent since the last call. An install whose thread crashed
/// before it finished ends with a failed `Done`, for [`CRASHED_DETAIL`],
/// so the connect dialog never waits on it for good.
pub fn drain_events(&mut self) -> Vec<InstallEvent> {
let mut out = Vec::new();
while let Some(events) = self.events.as_mut() {
match events.poll() {
StreamPoll::Update(event) => out.push(event),
StreamPoll::Pending => break,
StreamPoll::Ended => self.events = None,
StreamPoll::Died => {
self.events = None;
out.push(InstallEvent::Done(Err(String::from(CRASHED_DETAIL))));
}
}
}
out
}
/// Answer a pending `FallbackPrompt`: `true` proceeds with the slow
/// remote compile, `false` aborts the install.
pub fn respond_fallback(&self, proceed: bool) {
let _ = self.fallback_answer_tx.send(proceed);
}
/// Hand the adopted control master to the caller, leaving `None`
/// behind. Taken once the TUI has surrendered the alt-screen and is
/// ready to launch through the existing connection.
pub fn take_adopted(&mut self) -> Option<AdoptedMaster> {
self.adopted.take()
}
}
#[cfg(test)]
impl InstallSession {
/// A session whose installer has reported `events` and is still running,
/// so a test can drive the App's handling of them without ssh.
pub fn with_events(
host: &str,
path: Option<String>,
adopted: Option<AdoptedMaster>,
events: Vec<InstallEvent>,
) -> Self {
let (fallback_answer_tx, _) = channel();
Self {
host: host.to_string(),
path,
adopted,
events: Some(Stream::running(events)),
fallback_answer_tx,
}
}
/// A session whose installer logs a line and then crashes while a
/// helper of its own, like a build's output reader, still holds its log
/// sender (#2605).
pub fn crashing(host: &str) -> Self {
fn install(
_: AdoptedMaster,
log_tx: Sender<String>,
_: Sender<()>,
_: &mut dyn FnMut(&str) -> bool,
) -> anyhow::Result<AdoptedMaster> {
let _ = log_tx.send(String::from("Probing the remote"));
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(60));
drop(log_tx);
});
panic!("the install crashed (on purpose, in a test)")
}
Self::start_with(test_master(host), host.to_string(), None, install).unwrap()
}
}
/// A control master for a test's install session, at paths that do not
/// exist: nothing connects through it.
#[cfg(test)]
fn test_master(host: &str) -> AdoptedMaster {
AdoptedMaster {
host: host.to_string(),
socket_dir: std::path::PathBuf::from("/nonexistent/croft-test-ctl"),
socket_path: std::path::PathBuf::from("/nonexistent/croft-test-ctl/ctl"),
}
}
#[cfg(test)]
mod tests {
use super::*;
/// #2605 review: an install that says croft can launch and then fails
/// reports the launch before its outcome, so the user is dropped into the
/// croft already there instead of being told the connect failed.
#[test]
fn an_install_that_could_launch_and_then_failed_reports_the_launch_first() {
fn install(
_: AdoptedMaster,
log_tx: Sender<String>,
can_launch_tx: Sender<()>,
_: &mut dyn FnMut(&str) -> bool,
) -> anyhow::Result<AdoptedMaster> {
let _ = log_tx.send(String::from("croft is already installed"));
let _ = can_launch_tx.send(());
anyhow::bail!("the reinstall failed")
}
for _ in 0..20 {
let mut session =
InstallSession::start_with(test_master("devbox"), "devbox".into(), None, install)
.unwrap();
let mut events = Vec::new();
crate::test_budget::await_spawned(
std::time::Duration::from_secs(1),
"the install to end",
|| {
events.extend(session.drain_events());
events.iter().any(|e| matches!(e, InstallEvent::Done(_)))
},
);
let done = events
.iter()
.position(|e| matches!(e, InstallEvent::Done(_)))
.unwrap();
assert!(
events[..done]
.iter()
.any(|e| matches!(e, InstallEvent::CanLaunch)),
"{events:?}"
);
assert!(
events[..done]
.iter()
.any(|e| matches!(e, InstallEvent::Log(_))),
"{events:?}"
);
assert!(
matches!(&events[done], InstallEvent::Done(Err(e)) if e == "the reinstall failed"),
"{events:?}"
);
}
}
}