subc-daemon 0.54.0

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
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
//! Drain child pipes on two blocking reader threads, with a separate writer.
//! Neither the Tokio driver nor the log sink can hold up a pipe read.

use crate::stderr_tail::{unix_ms, write_captured_line, ChildOutputSink, LineFramer, StderrRing};
use std::collections::VecDeque;
use std::fs::File;
use std::io::{self, Read};
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
use std::time::SystemTime;
use tokio::process::Child;
use tokio::sync::oneshot;

#[cfg(unix)]
mod interruptible;
#[cfg(unix)]
use crate::supervise::live_upgrade::output::{self, ParkResult};
#[cfg(all(test, unix))]
pub(crate) use interruptible::fail_next_pair_for_test;
#[cfg(unix)]
pub(crate) use interruptible::StopReaders;
#[cfg(unix)]
pub(crate) const PLAIN_PIPE_DRAIN: std::time::Duration = std::time::Duration::from_secs(2);

// Charge small lines for their allocation and queue entries as well as their
// bytes, so a burst of empty lines cannot grow the queue without bound.
const QUEUE_BYTES: usize = 2 * 1024 * 1024;
const PER_LINE_BYTES: usize = 128;

#[derive(Clone, Copy)]
enum Pipe {
    Stdout = 0,
    Stderr = 1,
}

impl Pipe {
    const BOTH: [Self; 2] = [Self::Stdout, Self::Stderr];

    fn name(self) -> &'static str {
        match self {
            Self::Stdout => "stdout",
            Self::Stderr => "stderr",
        }
    }
}

enum Event {
    Line {
        raw: Box<[u8]>,
        terminated: bool,
        at_ms: u64,
    },
    Dropped {
        count: u64,
        at_ms: u64,
    },
    End(io::Result<()>),
    #[cfg(unix)]
    Park(u64, ParkResult),
}

#[derive(Default)]
struct QueueState {
    events: VecDeque<(Pipe, Event)>,
    bytes: usize,
    dropped: [Option<(u64, u64)>; 2],
}

impl QueueState {
    fn enqueue_drops(&mut self, pipe: Pipe) {
        if let Some((count, at_ms)) = self.dropped[pipe as usize].take() {
            self.events
                .push_back((pipe, Event::Dropped { count, at_ms }));
        }
    }
}

#[derive(Default)]
struct Queue {
    // Only queue bookkeeping is done under this lock. The writer releases it
    // before touching stderr, the ring, or any file.
    state: Mutex<QueueState>,
    ready: Condvar,
}

impl Queue {
    fn line(&self, pipe: Pipe, raw: &[u8], terminated: bool) {
        let at_ms = unix_ms(SystemTime::now());
        let cost = raw.len() + PER_LINE_BYTES;
        let mut state = self.state.lock().unwrap();
        if state.bytes + cost > QUEUE_BYTES {
            let (count, _) = state.dropped[pipe as usize].get_or_insert((0, at_ms));
            *count = count.saturating_add(1);
            return;
        }
        state.enqueue_drops(pipe);
        state.bytes += cost;
        state.events.push_back((
            pipe,
            Event::Line {
                raw: raw.into(),
                terminated,
                at_ms,
            },
        ));
        self.ready.notify_one();
    }

    fn end(&self, pipe: Pipe, result: io::Result<()>) {
        let mut state = self.state.lock().unwrap();
        // EOF and loss markers never compete for the line budget: at most one
        // marker precedes each queued line or EOF, plus the pending counters.
        state.enqueue_drops(pipe);
        state.events.push_back((pipe, Event::End(result)));
        self.ready.notify_one();
    }

    fn next(&self) -> (Pipe, Event) {
        let mut state = self.state.lock().unwrap();
        loop {
            if state.events.is_empty() {
                // Also report the loss if the child goes quiet after a burst
                // instead of printing another line or closing the pipe.
                for pipe in Pipe::BOTH {
                    state.enqueue_drops(pipe);
                }
            }
            if let Some(entry) = state.events.pop_front() {
                if let Event::Line { raw, .. } = &entry.1 {
                    state.bytes -= raw.len() + PER_LINE_BYTES;
                }
                return entry;
            }
            state = self.ready.wait(state).unwrap();
        }
    }
}

pub(crate) struct StderrPump<F = oneshot::Receiver<()>> {
    pub(crate) task: F,
    pub(crate) generation: u64,
}

pub(crate) struct Pumps {
    pub(crate) stdout: Option<oneshot::Receiver<()>>,
    pub(crate) stderr: Option<StderrPump>,
    #[cfg(unix)]
    pub(crate) stop: Option<StopReaders>,
}

pub(crate) fn start(
    child: &mut Child,
    sink: ChildOutputSink,
    ring: &Arc<Mutex<StderrRing>>,
    module_id: &str,
    #[cfg(unix)] stop_on_drop: bool,
) -> Pumps {
    // Tokio's owned-handle conversion restores blocking mode on Unix; Windows
    // gives back the standard synchronous pipe handle, before any async reads.
    #[cfg(unix)]
    let (stdout, stderr) = (
        child
            .stdout
            .take()
            .map(|pipe| pipe.into_owned_fd().map(File::from)),
        child
            .stderr
            .take()
            .map(|pipe| pipe.into_owned_fd().map(File::from)),
    );
    #[cfg(windows)]
    let (stdout, stderr) = (
        child
            .stdout
            .take()
            .map(|pipe| pipe.into_owned_handle().map(File::from)),
        child
            .stderr
            .take()
            .map(|pipe| pipe.into_owned_handle().map(File::from)),
    );
    start_sources(
        stdout,
        stderr,
        sink,
        ring,
        module_id,
        #[cfg(unix)]
        Default::default(),
        #[cfg(unix)]
        stop_on_drop,
    )
}

#[cfg(unix)]
pub(crate) fn resume(
    streams: [Option<(std::os::fd::OwnedFd, Vec<u8>)>; 2],
    sink: ChildOutputSink,
    ring: &Arc<Mutex<StderrRing>>,
    module_id: &str,
    stop_on_drop: bool,
) -> Pumps {
    let split = |stream: Option<(std::os::fd::OwnedFd, Vec<u8>)>| {
        stream.map_or((None, Vec::new()), |(fd, tail)| {
            (Some(Ok(File::from(fd))), tail)
        })
    };
    let [stdout, stderr] = streams;
    let (stdout, stdout_tail) = split(stdout);
    let (stderr, stderr_tail) = split(stderr);
    start_sources(
        stdout,
        stderr,
        sink,
        ring,
        module_id,
        [stdout_tail, stderr_tail],
        stop_on_drop,
    )
}

fn start_sources(
    stdout: Option<io::Result<File>>,
    stderr: Option<io::Result<File>>,
    sink: ChildOutputSink,
    ring: &Arc<Mutex<StderrRing>>,
    module_id: &str,
    #[cfg(unix)] tails: [Vec<u8>; 2],
    #[cfg(unix)] stop_on_drop: bool,
) -> Pumps {
    #[cfg(unix)]
    let (stdout, stderr, stop, wake) = match StopReaders::pair(stop_on_drop) {
        Ok((stop, wake)) => (stdout, stderr, Some(stop), Some(wake)),
        Err(error) => {
            tracing::warn!(module_id, %error, "capturing child output without a wake pair; this launch cannot be carried");
            (stdout, stderr, None, None)
        }
    };
    let generation = stderr.as_ref().map(|_| {
        ring.lock()
            .unwrap_or_else(|p| p.into_inner())
            .begin_process()
    });
    if stdout.is_none() {
        tracing::warn!(
            module_id,
            "spawned child exposed no stdout pipe; file capture will be incomplete"
        );
    }
    if stderr.is_none() {
        ring.lock()
            .unwrap_or_else(|p| p.into_inner())
            .mark_not_captured("stderr pipe was not available on spawn");
        tracing::warn!(
            module_id,
            "spawned child exposed no stderr pipe; tail will be unavailable"
        );
    }
    let queue = Arc::new(Queue::default());
    let (stdout_done, stdout_wait) = completion(stdout.is_some());
    let (stderr_done, stderr_wait) = completion(stderr.is_some());
    if stdout_done.is_some() || stderr_done.is_some() {
        let queue = Arc::clone(&queue);
        let ring = Arc::clone(ring);
        #[cfg(unix)]
        let control = wake.as_ref().map(|wake| wake.control.clone());
        if let Err(error) = thread::Builder::new()
            .name("child-log-writer".into())
            .spawn(move || {
                write_output(
                    &queue,
                    sink,
                    &ring,
                    generation,
                    [stdout_done, stderr_done],
                    #[cfg(unix)]
                    control.as_deref(),
                );
            })
        {
            tracing::warn!(module_id, %error, "could not start child log writer; child output will be discarded");
        }
    }
    #[cfg(unix)]
    let mut tails = tails.into_iter();
    for (pipe, source) in [(Pipe::Stdout, stdout), (Pipe::Stderr, stderr)] {
        #[cfg(unix)]
        let thread_wake = wake.clone();
        #[cfg(unix)]
        let tail = tails.next().expect("two streams");
        #[cfg(unix)]
        if source.is_none() {
            if let Some(wake) = &wake {
                wake.control.ended(pipe as usize);
            }
        }
        if let Some(source) = source {
            let reader_queue = Arc::clone(&queue);
            let started = source.and_then(|file| {
                thread::Builder::new()
                    .name(format!("child-{}-reader", pipe.name()))
                    .spawn(move || {
                        let result = read_pipe(
                            file,
                            pipe,
                            &reader_queue,
                            #[cfg(unix)]
                            thread_wake.as_ref(),
                            #[cfg(unix)]
                            tail,
                        );
                        #[cfg(unix)]
                        if let Some(wake) = thread_wake {
                            wake.finish(pipe);
                        }
                        reader_queue.end(pipe, result);
                    })
            });
            if let Err(error) = started {
                queue.end(pipe, Err(error));
            } else {
                continue;
            }
        }
        #[cfg(unix)]
        if let Some(wake) = &wake {
            wake.finish(pipe);
        }
    }
    Pumps {
        stdout: stdout_wait,
        stderr: stderr_wait
            .zip(generation)
            .map(|(task, generation)| StderrPump { task, generation }),
        #[cfg(unix)]
        stop,
    }
}

fn completion(enabled: bool) -> (Option<oneshot::Sender<()>>, Option<oneshot::Receiver<()>>) {
    if enabled {
        let (send, recv) = oneshot::channel();
        (Some(send), Some(recv))
    } else {
        (None, None)
    }
}

fn read_pipe(
    mut source: File,
    pipe: Pipe,
    queue: &Queue,
    #[cfg(unix)] wake: Option<&interruptible::ReaderWake>,
    #[cfg(unix)] tail: Vec<u8>,
) -> io::Result<()> {
    #[cfg(unix)]
    let mut framer = LineFramer::from_tail(tail);
    #[cfg(not(unix))]
    let mut framer = LineFramer::default();
    let mut buf = [0; 8192];
    let send = |raw: &[u8], terminated| queue.line(pipe, raw, terminated);
    loop {
        #[cfg(unix)]
        let read = match wake {
            Some(wake) => {
                wake.control
                    .read(&mut source, &mut buf, framer.tail(), |request, result| {
                        let mut state = queue.state.lock().unwrap();
                        state.enqueue_drops(pipe);
                        state.events.push_back((pipe, Event::Park(request, result)));
                        queue.ready.notify_one();
                    })
            }
            None => source.read(&mut buf),
        };
        #[cfg(not(unix))]
        let read = source.read(&mut buf);
        match read {
            Ok(0) => break,
            Ok(n) => framer.push(&buf[..n], send),
            Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
            Err(error) => return Err(error),
        }
    }
    framer.finish(send);
    Ok(())
}

fn write_output(
    queue: &Queue,
    mut sink: ChildOutputSink,
    ring: &Arc<Mutex<StderrRing>>,
    generation: Option<u64>,
    mut completions: [Option<oneshot::Sender<()>>; 2],
    #[cfg(unix)] control: Option<&output::Control>,
) {
    let lock_ring = || ring.lock().unwrap_or_else(|p| p.into_inner());
    if generation.is_some() {
        lock_ring().mark_captured();
    }
    while completions.iter().any(Option::is_some) {
        let (pipe, event) = queue.next();
        let stderr = match pipe {
            Pipe::Stdout => None,
            Pipe::Stderr => generation.map(|generation| (ring, generation)),
        };
        match event {
            Event::Line {
                raw,
                terminated,
                at_ms,
            } => {
                write_captured_line(stderr, &mut sink, &raw, terminated, at_ms);
            }
            Event::Dropped { count, at_ms } => {
                let reason = format!(
                    "child output capture: {count} lines dropped from {} (capture queue full)",
                    pipe.name()
                );
                if let Some((_, generation)) = stderr {
                    lock_ring().mark_incomplete_from(generation, reason.clone());
                }
                write_captured_line(stderr, &mut sink, reason.as_bytes(), true, at_ms);
            }
            Event::End(result) => {
                if let Some((_, generation)) = stderr {
                    let mut ring = lock_ring();
                    if let Err(error) = result {
                        ring.mark_incomplete_from(
                            generation,
                            format!("stderr read failed: {error}"),
                        );
                    }
                    ring.finish_pump(generation);
                } else if let Err(error) = result {
                    tracing::warn!(stream = pipe.name(), %error, "child output capture read failed");
                }
                if let Some(done) = completions[pipe as usize].take() {
                    let _ = done.send(());
                }
                #[cfg(unix)]
                if let Some(control) = control {
                    control.ended(pipe as usize);
                }
            }
            #[cfg(unix)]
            Event::Park(request, result) => {
                if let Some(control) = control {
                    control.drained(pipe as usize, request, result);
                }
            }
        }
    }
}

#[cfg(all(test, unix))]
mod tests;