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
use super::*;
impl RelayClient {
pub async fn connect(spec: &CommandSpec, expected_session_id: &str) -> Result<Self> {
Self::connect_with_timeouts(
spec,
expected_session_id,
RELAY_RPC_TIMEOUT,
RELAY_HANDSHAKE_TIMEOUT,
)
.await
}
#[cfg(all(test, unix))]
pub(super) async fn connect_with_timeout(
spec: &CommandSpec,
expected_session_id: &str,
request_timeout: Duration,
) -> Result<Self> {
Self::connect_with_timeouts(spec, expected_session_id, request_timeout, request_timeout)
.await
}
/// Start a relay proxy and complete its handshake, retrying while the
/// remote `sshd` is turning fresh connections away before authentication.
///
/// The whole daemon reconnects at once after a restart, which is exactly
/// when a host at its `MaxStartups` ceiling drops the surplus. Those
/// rejections say nothing about the worker, so escalating one to worker
/// recovery would destroy a healthy session.
pub(super) async fn connect_with_timeouts(
spec: &CommandSpec,
expected_session_id: &str,
request_timeout: Duration,
handshake_timeout: Duration,
) -> Result<Self> {
for attempt in 1..=SSH_RETRY_ATTEMPTS {
let outcome = Self::connect_attempt(
spec,
expected_session_id,
request_timeout,
handshake_timeout,
)
.await;
let error = match outcome {
Ok(client) => return Ok(client),
Err(ConnectFailure {
error,
transport_rejected,
}) => {
if attempt == SSH_RETRY_ATTEMPTS || !transport_rejected {
return Err(error);
}
error
}
};
let delay = mj_core::targets::ssh_retry_delay(attempt);
tracing::warn!(
session_id = %expected_session_id,
destination = spec.ssh_destination.as_deref().unwrap_or_default(),
purpose = %spec.purpose,
attempt,
attempts = SSH_RETRY_ATTEMPTS,
delay_ms = delay.as_millis() as u64,
error = %error,
"relay proxy was refused by the SSH server before authentication; retrying"
);
tokio::time::sleep(delay).await;
}
unreachable!("the final attempt always returns");
}
/// One proxy launch and handshake.
///
/// An admission permit is taken before the proxy is spawned and released
/// once hello completes: `sshd` counts only unauthenticated connections
/// against `MaxStartups`, so the long-lived relay stops occupying a slot
/// as soon as it is authenticated and talking.
pub(super) async fn connect_attempt(
spec: &CommandSpec,
expected_session_id: &str,
request_timeout: Duration,
handshake_timeout: Duration,
) -> std::result::Result<Self, ConnectFailure> {
let permit = match spec.ssh_destination.clone() {
Some(destination) => {
match tokio::task::spawn_blocking(move || SshAdmission::acquire(&destination)).await
{
Ok(permit) => Some(permit),
Err(error) => {
return Err(ConnectFailure::plain(anyhow!(
"SSH admission for the relay proxy was cancelled: {error}"
)));
}
}
}
None => None,
};
Self::spawn_and_handshake(
spec,
expected_session_id,
request_timeout,
handshake_timeout,
permit,
)
.await
}
pub(super) async fn spawn_and_handshake(
spec: &CommandSpec,
expected_session_id: &str,
request_timeout: Duration,
handshake_timeout: Duration,
permit: Option<SshPermit>,
) -> std::result::Result<Self, ConnectFailure> {
let mut child = Command::new(&spec.program)
.args(&spec.args)
.envs(&spec.env)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
// Never inherit: the controller owns a TUI alternate screen, so a
// child writing to the shared stderr corrupts the display outside
// the renderer's buffer. Drain it into the log instead.
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()
.with_context(|| format!("start session relay proxy for {}", spec.purpose))
.map_err(|error| {
tracing::warn!(
session_id = %expected_session_id,
operation = "connect",
purpose = %spec.purpose,
error = %error,
"could not start relay proxy"
);
error
})?;
let stderr_tail: ProxyStderrTail = Default::default();
let draining = child.stderr.take().map(|errors| {
let purpose = spec.purpose.clone();
let session_id = expected_session_id.to_owned();
let tail = stderr_tail.clone();
tokio::spawn(drain_proxy_stderr(errors, purpose, session_id, tail))
});
let input = child
.stdin
.take()
.context("relay proxy stdin unavailable")
.map_err(|error| {
tracing::warn!(
session_id = %expected_session_id,
operation = "connect",
purpose = %spec.purpose,
error = %error,
"relay proxy did not provide stdin"
);
error
})?;
let output = child
.stdout
.take()
.context("relay proxy stdout unavailable")
.map_err(|error| {
tracing::warn!(
session_id = %expected_session_id,
operation = "connect",
purpose = %spec.purpose,
error = %error,
"relay proxy did not provide stdout"
);
error
})?;
let mut nonce_bytes = [0_u8; 8];
getrandom::fill(&mut nonce_bytes).map_err(|error| {
let error = anyhow!("generate relay request nonce: {error}");
tracing::warn!(
session_id = %expected_session_id,
operation = "connect",
error = %error,
"could not initialize relay request nonce"
);
error
})?;
let mut client = Self {
child: Some(child),
input: Some(input),
output: BufReader::new(output),
request_timeout,
abandoned: None,
next_request: 1,
connection_nonce: u64::from_le_bytes(nonce_bytes),
protocol_version: RELAY_PROTOCOL_VERSION,
// Keep the expected identity from process creation onward so a
// handshake failure and the dropped proxy that follows it remain
// attributable even when Hello never returns a session ID.
session_id: expected_session_id.to_owned(),
relay_version: String::new(),
worker_build: None,
latest_ordinal: 0,
latest_digest: RELAY_EVENT_GENESIS_DIGEST.to_owned(),
};
match client
.complete_handshake(expected_session_id, handshake_timeout)
.await
{
Ok(()) => {
// Hello succeeded, so this connection is past authentication
// and no longer counts against the server's startup budget.
drop(permit);
// The drain task keeps logging for the life of the connection.
Ok(client)
}
Err(error) => {
// Read the proxy's exit status before killing it: a connection
// the server dropped has already exited 255, and that status
// is what separates a refused connection from a broken worker.
let status = match client.child.as_mut() {
Some(child) => {
match tokio::time::timeout(RELAY_PROXY_DETACH_GRACE, child.wait()).await {
Ok(Ok(status)) => status.code(),
// Still running, or unwaitable. Stop the proxy so
// it closes stderr; otherwise a proxy that is
// merely slow would hold the drain task open past
// its grace period and the tail would be lost. The
// child stays in place so dropping `client` reaps
// it as usual.
_ => {
let _ = child.start_kill();
None
}
}
}
None => None,
};
let tail = Self::proxy_stderr_tail(draining, &stderr_tail).await;
let transport_rejected = permit.is_some()
&& status
.is_some_and(|status| is_transport_rejection(status, &tail.join("\n")));
drop(permit);
Err(ConnectFailure {
error: Self::attach_proxy_stderr(error, tail),
transport_rejected,
})
}
}
}
/// Collect the proxy's trailing stderr.
///
/// The caller has already waited for the proxy or killed it, so the drain
/// normally reaches EOF at once. The grace period covers the case it
/// cannot: a grandchild that inherited stderr keeps the pipe open for as
/// long as it lives. Either way the lines already read are returned, since
/// the drain publishes them as it goes.
pub(super) async fn proxy_stderr_tail(
draining: Option<tokio::task::JoinHandle<()>>,
tail: &ProxyStderrTail,
) -> Vec<String> {
if let Some(handle) = draining
&& tokio::time::timeout(RELAY_PROXY_DETACH_GRACE, handle)
.await
.is_err()
{
tracing::debug!("relay proxy stderr is still open; reporting the lines read so far");
}
tail.lock()
.unwrap_or_else(PoisonError::into_inner)
.iter()
.cloned()
.collect()
}
/// Attach the proxy's own stderr tail to a failed connect. The proxy
/// explains failures the controller cannot see any other way, such as a
/// worker socket path longer than `sun_path`.
pub(super) fn attach_proxy_stderr(error: anyhow::Error, lines: Vec<String>) -> anyhow::Error {
if lines.is_empty() {
return error;
}
error.context(format!(
"relay proxy stderr (last {} lines):\n{}",
lines.len(),
lines.join("\n")
))
}
/// Exchange `Hello` and record what the relay negotiated.
pub(super) async fn complete_handshake(
&mut self,
expected_session_id: &str,
handshake_timeout: Duration,
) -> Result<()> {
let response = self
.call_hello(
RelayRequest::Hello {
controller_version: env!("CARGO_PKG_VERSION").to_owned(),
supported: RelayVersionRange::CURRENT,
},
handshake_timeout,
)
.await?;
let RelayResponsePayload::Hello {
negotiated,
relay_version,
session_id,
worker_build,
} = response
else {
let error = anyhow!("relay returned an unexpected hello response");
log_relay_client_failure(self, "hello", "relay-hello", &error);
return Err(error);
};
if session_id != expected_session_id {
let error = anyhow!("relay belongs to session {session_id}, not {expected_session_id}");
log_relay_client_failure(self, "hello", "relay-hello", &error);
return Err(error);
}
if !RelayVersionRange::CURRENT.contains(negotiated) {
let error = anyhow!(
"relay negotiated unsupported protocol {negotiated}; this controller supports {}-{}",
RELAY_MIN_PROTOCOL_VERSION,
RELAY_PROTOCOL_VERSION
);
log_relay_client_failure(self, "hello", "relay-hello", &error);
return Err(error);
}
self.protocol_version = negotiated;
self.session_id = session_id;
self.relay_version = relay_version;
self.worker_build = worker_build;
Ok(())
}
}