shep-daemon 0.18.2

The supervision engine behind shep: process spawning, restart policy, the log plane, watch and cron restarts, and the RPC server
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! The tasks [`spawn_log_pump`], [`spawn_stdin_pump`] and
//! [`spawn_channel_pumps`] spawn, and the line-draining helpers they share.

use std::io;
use std::pin::Pin;

use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt as _, BufReader};
use tokio::sync::mpsc;
use tokio::sync::mpsc::error::TrySendError;
use tokio::time::{Instant, Sleep, sleep_until, timeout};

use crate::channel::{ChildMessage, ShepherdMessage};
use crate::runner::{LogCtl, LogLine, RunnerError, StdinWrite};

use super::FINAL_DRAIN;
#[cfg(unix)]
use super::READ_BUFFER;
use super::line_reader::LineReader;
use super::log_file::{LogFile, LogFiles, LogSink, PipeFds};

/// What the pump does after handling one line result.
enum AfterLine {
    /// The stream is live; keep reading it.
    KeepReading,
    /// The stream reached EOF or failed; stop reading THIS stream.
    StreamEnded,
    /// The owning sheep task dropped its `logs` receiver; stop entirely.
    LogsClosed,
}

/// Handles one line read from a stream: appends it to that stream's file,
/// forwards it on `logs_tx`, and reports what the pump should do next.
///
/// The wait for room on `logs_tx` keeps serving `ctl_rx`; see
/// [`reserve_slot`] for the cycle that would otherwise close.
async fn deliver_line<O, E>(
    result: io::Result<Option<String>>,
    err: bool,
    files: &mut LogFiles,
    logs_tx: &mpsc::Sender<LogLine>,
    ctl_rx: &mut mpsc::Receiver<LogCtl>,
    streams: &mut Streams<O, E>,
) -> AfterLine
where
    O: AsyncRead + Unpin,
    E: AsyncRead + Unpin,
{
    match result {
        Ok(Some(line)) => {
            files.stream(err).append(&line).await;
            let Some(slot) = reserve_slot(logs_tx, files, ctl_rx, streams).await else {
                return AfterLine::LogsClosed;
            };
            slot.send(LogLine { err, line });
            AfterLine::KeepReading
        }
        Ok(None) => AfterLine::StreamEnded, // normally the child exiting
        Err(error) => {
            tracing::error!(path = ?files.stream(err).path, %error, "log stream read failed");
            AfterLine::StreamEnded
        }
    }
}

/// Waits for room on `logs_tx`, serving control requests and idle flushes
/// while it waits. `None` once the `logs` receiver is gone.
///
/// A pump parked on a full `logs` channel is parked for as long as the sheep
/// task takes, which is unbounded, so without the idle-flush branch the line
/// just appended would sit in the buffer for exactly that long.
///
/// A `select!` handler is not cancellable, so a bare `send().await` inside
/// one stops the pump polling `ctl_rx` for as long as the wait lasts. The
/// party that makes room on `logs` is the sheep task, the same party a
/// reopen's acknowledgement travels back to, so that would close a cycle.
async fn reserve_slot<'tx, O, E>(
    logs_tx: &'tx mpsc::Sender<LogLine>,
    files: &mut LogFiles,
    ctl_rx: &mut mpsc::Receiver<LogCtl>,
    streams: &mut Streams<O, E>,
) -> Option<mpsc::Permit<'tx, LogLine>>
where
    O: AsyncRead + Unpin,
    E: AsyncRead + Unpin,
{
    // The common case skips the `select!`, whose timer is registered and
    // removed per call under a lock every worker thread contends for.
    match logs_tx.try_reserve() {
        Ok(slot) => return Some(slot),
        Err(TrySendError::Closed(())) => return None,
        Err(TrySendError::Full(())) => {}
    }
    loop {
        // Recomputed every iteration from the stored mark, so losing the
        // race never extends the window.
        let flush_at = files.flush_deadline();
        // Every branch is documented cancel-safe, as `select!` requires: a
        // `reserve` that loses the race has taken no slot, a `recv` that
        // loses it has taken no message, and a `sleep_until` that loses it
        // is rebuilt against the same absolute deadline.
        tokio::select! {
            slot = logs_tx.reserve() => return slot.ok(),
            ctl = ctl_rx.recv() => match ctl {
                Some(ctl) => files.serve(ctl, streams).await,
                // The line in hand is still owed to the receiver. Awaited
                // outside the `select!` rather than looping, since a closed
                // receiver is ready on every poll.
                None => return logs_tx.reserve().await.ok(),
            },
            () = sleep_until(flush_at.unwrap_or_else(Instant::now)), if flush_at.is_some() => {
                files.flush_idle().await;
            }
        }
    }
}

/// The next line from an optional stream, or a future that never resolves
/// once there is no stream left to read.
///
/// The pump's `select!` needs a branch it can leave in place after a stream
/// ends: a ready `None` would be re-selected on every poll and spin the
/// loop, while pending forever drops the branch out of contention.
///
/// Cancel-safe, as a `select!` branch must be: see [`LineReader::read_line`].
async fn next_line_ref<R>(lines: &mut Option<LineReader<R>>) -> io::Result<Option<&str>>
where
    R: AsyncRead + Unpin,
{
    match lines {
        Some(lines) => lines.read_line().await,
        None => core::future::pending().await,
    }
}

/// [`next_line_ref`] with the line copied out, for a caller that keeps it
/// past the next read: the pump holds it while it waits for room on `logs`.
/// Cancel-safe for the same reason.
async fn next_line<R>(lines: &mut Option<LineReader<R>>) -> io::Result<Option<String>>
where
    R: AsyncRead + Unpin,
{
    match lines {
        Some(lines) => lines.read_line_owned().await,
        None => core::future::pending().await,
    }
}

/// Points `timer` at `deadline`, touching the timer wheel only when the
/// deadline moved or was retired.
///
/// The pump keeps one timer across lines: building one per `select!`
/// registers and removes it under a lock every worker thread contends for.
fn arm_idle_flush(timer: &mut Option<Pin<Box<Sleep>>>, deadline: Option<Instant>) {
    match (timer.as_mut(), deadline) {
        (_, None) => *timer = None,
        (Some(sleep), Some(at)) if sleep.deadline() == at => {}
        (Some(sleep), Some(at)) => sleep.as_mut().reset(at),
        (None, Some(at)) => *timer = Some(Box::pin(sleep_until(at))),
    }
}

/// Resolves when `timer` fires, and never while it is unarmed.
///
/// Cancel-safe: a `Sleep` that loses a `select!` race stays registered at
/// the same deadline.
async fn idle_flush_due(timer: &mut Option<Pin<Box<Sleep>>>) {
    match timer {
        Some(sleep) => sleep.as_mut().await,
        None => core::future::pending().await,
    }
}

/// One stream's reader, at the capacity a handover reasons about.
///
/// A free function rather than an inline `BufReader::with_capacity` at each
/// call site, so [`drain_ready`]'s bound and the reader's real capacity
/// cannot drift apart.
#[cfg(unix)]
fn with_read_buffer<R: AsyncRead>(reader: R) -> BufReader<R> {
    BufReader::with_capacity(READ_BUFFER, reader)
}

/// See the unix definition above; nothing here reasons about the capacity.
#[cfg(not(unix))]
fn with_read_buffer<R: AsyncRead>(reader: R) -> BufReader<R> {
    BufReader::new(reader)
}

/// A pump's two line readers, held in one place so a control request can
/// reach them.
///
/// [`LogCtl::ReportFds`] is served from two places, the pump's own `select!`
/// and [`reserve_slot`]'s, and a local would be visible to only one.
///
/// `None` per stream is one that has reached EOF or failed. The pump drops
/// the reader at that moment, which closes the descriptor, so a stream with
/// no reader has neither a number nor bytes left to write.
pub(super) struct Streams<O, E> {
    /// The stdout reader, until stdout ends.
    pub(super) out: Option<LineReader<O>>,
    /// The stderr reader, until stderr ends.
    pub(super) err: Option<LineReader<E>>,
}

/// Writes out the whole lines one stream's reader is already holding, and
/// touches the pipe behind it not at all.
///
/// This is what a descriptor report owes the successor: the reader has taken
/// up to [`super::READ_BUFFER`] off the pipe the successor will never see
/// there, and the `execve` destroys it. Reading the pipe here would refill
/// the reader as fast as it empties it; stopping at "no whole line is
/// buffered" makes the buffer strictly shrink, since a `read_line` that
/// finds its delimiter already buffered returns without a syscall.
///
/// The partial line at the end of the buffer is left behind. Nothing drained
/// here goes on `logs_tx`: those bus subscribers go with the image.
#[cfg(unix)]
pub(super) async fn drain_ready<R>(lines: &mut Option<LineReader<R>>, file: &mut LogFile)
where
    R: AsyncRead + Unpin,
{
    let Some(reader) = lines.as_mut() else {
        return;
    };
    while reader.get_ref().buffer().contains(&b'\n') {
        // A delimiter already in the buffer means no `read(2)`, so nothing
        // new arrives to replace what is written.
        let Ok(Some(line)) = reader.read_line().await else {
            return;
        };
        file.append(line).await;
    }
}

/// Writes out what the streams still hold once the sheep task has let go,
/// and stops after [`FINAL_DRAIN`] however much is left.
///
/// Files only: `logs_tx` is closed, which is what brought us here.
///
/// Each stream is retired on EOF or a read failure, so a child that has
/// exited ends this in one poll per stream. The budget covers the other
/// case: a lamb that inherited a write end keeps the pipe open, and without
/// a bound the pump would follow it for as long as it cared to talk.
///
/// A retired stream does not clear its number from `files.pipes` and does
/// not need to: the caller `break`s as soon as this returns.
async fn final_drain<O, E>(files: &mut LogFiles, streams: &mut Streams<O, E>)
where
    O: AsyncRead + Unpin,
    E: AsyncRead + Unpin,
{
    let drained = timeout(FINAL_DRAIN, async {
        while streams.out.is_some() || streams.err.is_some() {
            // Bound before the `match`: the future borrows `streams`, and a
            // scrutinee's temporaries outlive the arms.
            tokio::select! {
                result = next_line_ref(&mut streams.out) => {
                    match result {
                        Ok(Some(line)) => files.stream(false).append(line).await,
                        Ok(None) | Err(_) => streams.out = None,
                    }
                }
                result = next_line_ref(&mut streams.err) => {
                    match result {
                        Ok(Some(line)) => files.stream(true).append(line).await,
                        Ok(None) | Err(_) => streams.err = None,
                    }
                }
            }
        }
    })
    .await;
    if drained.is_err() {
        tracing::debug!("a pump left the pipe still open at its sheep task's exit");
    }
}

/// Pumps a sheep's stdout and stderr to completion, and stays reachable the
/// whole time it does.
///
/// Every line is appended to its stream's file and then forwarded on
/// `logs_tx`. A [`LogCtl`] is served between lines, while no line is flowing
/// at all, and while the pump waits for room on `logs_tx`. One task for both
/// streams, so one [`LogCtl::Reopen`] swaps both files and answers once.
///
/// The file write is issued before the line is forwarded but lands in a
/// [`BufWriter`](tokio::io::BufWriter), so a line seen on `logs_tx` need not
/// be in the file yet. [`LogCtl::Reopen`], [`LogCtl::Flush`] and
/// [`super::IDLE_FLUSH`] are the barriers. A reopen waits behind the pump's
/// own file I/O, with no timeout.
///
/// `stamped` is the sheep's `log_timestamps`, and it holds for both files.
#[allow(
    clippy::too_many_arguments,
    reason = "eight: both streams, both sinks, both channels, the fds, the stamp"
)]
pub(super) fn spawn_log_pump<O, E>(
    stdout: Option<O>,
    stderr: Option<E>,
    out_sink: LogSink,
    err_sink: LogSink,
    stamped: bool,
    logs_tx: mpsc::Sender<LogLine>,
    mut ctl_rx: mpsc::Receiver<LogCtl>,
    pipes: PipeFds,
) where
    O: AsyncRead + Unpin + Send + 'static,
    E: AsyncRead + Unpin + Send + 'static,
{
    tokio::spawn(async move {
        let mut files = LogFiles {
            out: LogFile::from_sink(out_sink, stamped).await,
            err: LogFile::from_sink(err_sink, stamped).await,
            pipes,
            #[cfg(unix)]
            parked: false,
        };
        let mut streams = Streams {
            out: stdout.map(|reader| LineReader::new(with_read_buffer(reader))),
            err: stderr.map(|reader| LineReader::new(with_read_buffer(reader))),
        };

        let mut idle_flush = None;
        while streams.out.is_some() || streams.err.is_some() {
            // Recomputed every iteration from the stored mark;
            // `reserve_slot` carries the same branch.
            arm_idle_flush(&mut idle_flush, files.flush_deadline());
            tokio::select! {
                result = next_line(&mut streams.out), if files.reading() => {
                    // Bound before the `match`: the future borrows `streams`,
                    // and a scrutinee's temporaries outlive the arms, so an
                    // arm could not clear the reader it was reading.
                    let after =
                        deliver_line(result, false, &mut files, &logs_tx, &mut ctl_rx, &mut streams)
                            .await;
                    match after {
                        AfterLine::KeepReading => {}
                        // Dropping the reader closes the descriptor, so the
                        // number stops being ours in the same statement it
                        // stops being readable.
                        AfterLine::StreamEnded => {
                            streams.out = None;
                            #[cfg(unix)]
                            {
                                files.pipes.out = None;
                            }
                        }
                        AfterLine::LogsClosed => break,
                    }
                }
                result = next_line(&mut streams.err), if files.reading() => {
                    let after =
                        deliver_line(result, true, &mut files, &logs_tx, &mut ctl_rx, &mut streams)
                            .await;
                    match after {
                        AfterLine::KeepReading => {}
                        // As above: the number goes with the reader.
                        AfterLine::StreamEnded => {
                            streams.err = None;
                            #[cfg(unix)]
                            {
                                files.pipes.err = None;
                            }
                        }
                        AfterLine::LogsClosed => break,
                    }
                }
                ctl = ctl_rx.recv() => {
                    match ctl {
                        Some(ctl) => files.serve(ctl, &mut streams).await,
                        None => break, // nothing holds a `log_ctl` sender
                    }
                }
                // The owning sheep task dropped its `logs` receiver. Its own
                // branch, because a pump whose child forked a lamb holding
                // the pipe reaches no EOF and has no next line to notice it
                // on. Cancel-safe: a closed channel stays closed.
                () = logs_tx.closed() => break,

                () = idle_flush_due(&mut idle_flush) => files.flush_idle().await,
            }
        }
        // On the way out rather than in the branch that prompted it: four
        // exits reach this line and each can leave a line unread. Not while
        // parked, since a parked pump is holding the pipe for a successor to
        // adopt.
        if files.reading() {
            final_drain(&mut files, &mut streams).await;
        }
        // A `BufWriter` cannot flush itself as it drops, and every way out
        // of the loop above drops both of them.
        files.flush_idle().await;
    });
}

/// Writes lines to one child's stdin, one at a time, acknowledging each.
///
/// Serial on purpose: two concurrent writers to one pipe can interleave
/// mid-line, and a REPL reading the result would see a command neither
/// caller sent. A line queued behind one the app is not reading waits, so
/// the caller bounds its own wait; abandoning a write halfway would leave a
/// partial line in the pipe.
///
/// A request whose caller has stopped listening is dropped rather than
/// written: delivering later would send a line the operator was told was not
/// written. The line already inside `write_all` is past that point. Ends
/// when the last sender drops, giving the app EOF on stdin.
pub(super) fn spawn_stdin_pump<W>(stdin: Option<W>, mut rx: mpsc::Receiver<StdinWrite>)
where
    W: AsyncWrite + Unpin + Send + 'static,
{
    tokio::spawn(async move {
        let Some(mut stdin) = stdin else {
            // `Stdio::piped()` was set and `child.stdin` was still `None`.
            // Answering nothing would hang every caller.
            while let Some(StdinWrite { done, .. }) = rx.recv().await {
                let _ = done.send(Err(RunnerError::WriteFailed(
                    "this child has no stdin pipe".to_string(),
                )));
            }
            return;
        };
        while let Some(StdinWrite { line, done }) = rx.recv().await {
            if done.is_closed() {
                // The supervisor's `STDIN_WRITE_TIMEOUT` expired and it
                // dropped the receiver. Writing now would deliver a line the
                // operator was already told was not written.
                continue;
            }
            let mut bytes = line.into_bytes();
            // Exactly one terminator, appended here and nowhere else: the
            // wire carries the line without one (`Request::SendLine::line`).
            bytes.push(b'\n');
            let result = match stdin.write_all(&bytes).await {
                Ok(()) => stdin.flush().await,
                Err(error) => Err(error),
            };
            let _ = done.send(result.map_err(|error| RunnerError::WriteFailed(error.to_string())));
        }
    });
}

/// Wires the daemon side of the shepherd channel: a reader task decodes
/// newline-JSON [`ChildMessage`]s onto `from_child_tx`; a writer task encodes
/// [`ShepherdMessage`]s taken from `to_child_rx` back onto the socket.
///
/// Generic over the transport: the daemon's end is a `UnixStream` half of a
/// socketpair on unix and an accepted named-pipe server instance on Windows.
/// `tokio::io::split` rather than `UnixStream::into_split`, since
/// `NamedPipeServer` has no `into_split` of its own.
pub(super) fn spawn_channel_pumps<S>(
    daemon_end: S,
    from_child_tx: mpsc::Sender<ChildMessage>,
    mut to_child_rx: mpsc::Receiver<ShepherdMessage>,
) where
    S: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
    let (read_half, mut write_half) = tokio::io::split(daemon_end);

    tokio::spawn(async move {
        let mut lines = LineReader::new(BufReader::new(read_half));
        loop {
            match lines.read_line().await {
                Ok(Some(line)) => match serde_json::from_str::<ChildMessage>(line) {
                    Ok(msg) => {
                        if from_child_tx.send(msg).await.is_err() {
                            break; // owning sheep task dropped from_child
                        }
                    }
                    Err(error) => {
                        tracing::warn!(%line, %error, "malformed shepherd-channel frame");
                    }
                },
                Ok(None) => break, // child closed its end, normally at exit
                Err(error) => {
                    tracing::error!(%error, "shepherd-channel read failed");
                    break;
                }
            }
        }
    });

    tokio::spawn(async move {
        while let Some(msg) = to_child_rx.recv().await {
            let mut line = match serde_json::to_string(&msg) {
                Ok(json) => json,
                Err(error) => {
                    tracing::error!(%error, "shepherd message encode failed");
                    continue;
                }
            };
            line.push('\n');
            if write_half.write_all(line.as_bytes()).await.is_err() {
                break; // child closed its end
            }
        }
    });
}