sofie-cli 0.2.0

The `sofie` command: gen, check and the tools reading the app model.
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
use std::collections::BTreeMap;
use std::io::{self, Read};
use std::process::{Child, Command, ExitStatus, Output, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, MutexGuard, PoisonError};
use std::time::Duration;

/// Process-wide, as the interrupt is: Ctrl-C or SIGTERM stops every child sofie started,
/// which a SIGTERM sent to sofie alone would otherwise leave running.
static LIVE: Registry = Registry(Mutex::new(BTreeMap::new()));

/// Waiting polls, so a waiting thread never holds the lock an interrupt needs.
const WAIT_POLL: Duration = Duration::from_millis(20);

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum ProcessGroup {
    /// sofie's, so a child run in the terminal gets its Ctrl-C as it would run by hand.
    Inherited,
    /// The child's own, so stopping it stops what it started too, such as cargo's test
    /// binaries. Not on Windows, where stopping it stops the child alone.
    Own,
}

struct Running {
    child: Child,
    group: ProcessGroup,
}

type Live = BTreeMap<u32, Running>;

#[derive(Default)]
struct Registry(Mutex<Live>);

impl Registry {
    /// Spawned under the lock, so an interrupt handled meanwhile cannot miss it.
    fn spawn(
        &'static self,
        group: ProcessGroup,
        command: &mut Command,
        spawn: impl FnOnce(&mut Command) -> io::Result<Child>,
    ) -> io::Result<ChildProcess> {
        #[cfg(unix)]
        if group == ProcessGroup::Own {
            std::os::unix::process::CommandExt::process_group(command, 0);
        }
        let mut live = self.lock();
        let child = spawn(command)?;
        let pid = child.id();
        live.insert(pid, Running { child, group });
        Ok(ChildProcess {
            pid,
            registry: self,
            status: None,
        })
    }

    fn reap(&self, pid: u32) -> io::Result<Option<ExitStatus>> {
        let mut live = self.lock();
        let running = live
            .get_mut(&pid)
            .ok_or_else(|| io::Error::other(format!("child process {pid} is no longer tracked")))?;
        let status = running.child.try_wait()?;
        if status.is_some() {
            live.remove(&pid);
        }
        Ok(status)
    }

    /// Holding the returned lock until the process exits keeps anything from spawning
    /// another child.
    fn stop_all_for_exit(&self) -> MutexGuard<'_, Live> {
        let mut live = self.lock();
        for running in live.values_mut() {
            kill(running);
        }
        live.clear();
        live
    }

    fn lock(&self) -> MutexGuard<'_, Live> {
        self.0.lock().unwrap_or_else(PoisonError::into_inner)
    }
}

/// A child process in the registry; dropping it while it runs kills it.
pub(crate) struct ChildProcess {
    pid: u32,
    registry: &'static Registry,
    /// Once reaped, its pid may name another process, which nothing here may signal.
    status: Option<ExitStatus>,
}

impl ChildProcess {
    pub(crate) fn spawn(
        group: ProcessGroup,
        command: &mut Command,
        spawn: impl FnOnce(&mut Command) -> io::Result<Child>,
    ) -> io::Result<Self> {
        LIVE.spawn(group, command, spawn)
    }

    pub(crate) fn pid(&self) -> u32 {
        self.pid
    }

    pub(crate) fn exited(&mut self) -> io::Result<Option<ExitStatus>> {
        if self.status.is_none() {
            self.status = self.registry.reap(self.pid)?;
        }
        Ok(self.status)
    }

    pub(crate) fn wait(mut self) -> io::Result<ExitStatus> {
        loop {
            if let Some(status) = self.exited()? {
                return Ok(status);
            }
            std::thread::sleep(WAIT_POLL);
        }
    }

    pub(crate) fn kill(mut self) -> Option<ExitStatus> {
        self.stop()
    }

    /// Under the lock, so an interrupt arriving meanwhile still finds the child.
    fn stop(&mut self) -> Option<ExitStatus> {
        if self.status.is_none() {
            let mut live = self.registry.lock();
            self.status = kill(&mut live.remove(&self.pid)?);
        }
        self.status
    }
}

#[cfg(all(test, unix))]
impl ChildProcess {
    pub(crate) fn untracked() -> Self {
        let registry: &'static Registry = Box::leak(Box::default());
        let child = registry
            .spawn(
                ProcessGroup::Inherited,
                &mut Command::new("true"),
                Command::spawn,
            )
            .expect("`true` starts");
        drop(registry.stop_all_for_exit());
        child
    }
}

impl Drop for ChildProcess {
    fn drop(&mut self) {
        self.stop();
    }
}

/// `Command::output`, with the child in the registry.
pub(crate) fn output(group: ProcessGroup, command: &mut Command) -> io::Result<Output> {
    LIVE.output(group, command)
}

impl Registry {
    fn output(&'static self, group: ProcessGroup, command: &mut Command) -> io::Result<Output> {
        let mut stdout = None;
        let mut stderr = None;
        let process = self.spawn(group, command, |command| {
            let mut child = command
                .stdin(Stdio::null())
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()?;
            stdout = child.stdout.take();
            stderr = child.stderr.take();
            Ok(child)
        })?;
        // Read beside stdout, so neither pipe fills while the other is read.
        let stderr = std::thread::spawn(move || read_all(stderr));
        let stdout = read_all(stdout)?;
        let stderr = stderr
            .join()
            .map_err(|_| io::Error::other("reading a child process's stderr panicked"))??;
        Ok(Output {
            status: process.wait()?,
            stdout,
            stderr,
        })
    }
}

fn read_all(pipe: Option<impl Read>) -> io::Result<Vec<u8>> {
    let mut bytes = Vec::new();
    if let Some(mut pipe) = pipe {
        pipe.read_to_end(&mut bytes)?;
    }
    Ok(bytes)
}

/// Killed rather than stopped: a development app's held reload requests would keep a
/// graceful shutdown waiting, and nothing is lost. Killing fails only for a process that
/// already exited, which `wait` then reaps.
fn kill(running: &mut Running) -> Option<ExitStatus> {
    if running.group == ProcessGroup::Own {
        kill_group(running.child.id());
    }
    let _ = running.child.kill();
    running.child.wait().ok()
}

/// std signals a single process, and libc's `killpg` would need `unsafe`. The leader is
/// not reaped yet, so its pid still names the group and no other.
#[cfg(unix)]
fn kill_group(leader: u32) {
    let killed = Command::new("kill")
        .args(["-s", "KILL", "--", &format!("-{leader}")])
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
    if let Err(error) = killed {
        eprintln!("sofie: stopping process group {leader} failed: {error}");
    }
}

#[cfg(not(unix))]
fn kill_group(_leader: u32) {}

/// Ctrl-C, the SIGTERM an agent stops `sofie mcp` with, or a closed terminal's SIGHUP stops
/// and reaps every child and exits with 128 plus the signal's number, as a shell reports a
/// process the signal ended. A SIGHUP ignored at start, as under `nohup`, stays ignored for
/// sofie and its children. `process::exit` flushes stdout. Listening again does nothing.
pub(crate) fn stop_on_interrupt() -> io::Result<()> {
    static LISTENING: AtomicBool = AtomicBool::new(false);
    if LISTENING.swap(true, Ordering::SeqCst) {
        return Ok(());
    }
    let runtime = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()?;
    let mut signals = {
        let _context = runtime.enter();
        Signals::listen()?
    };
    std::thread::Builder::new()
        .name("sofie-interrupt".to_owned())
        .spawn(move || {
            let code = runtime.block_on(signals.next());
            let _held = LIVE.stop_all_for_exit();
            std::process::exit(code);
        })?;
    Ok(())
}

#[cfg(unix)]
struct Signals {
    interrupt: tokio::signal::unix::Signal,
    terminate: tokio::signal::unix::Signal,
    hangup: Option<tokio::signal::unix::Signal>,
}

#[cfg(unix)]
impl Signals {
    /// tokio's handler replaces an inherited ignore, so a SIGHUP ignored at start is left alone.
    fn listen() -> io::Result<Self> {
        use tokio::signal::unix::{SignalKind, signal};

        let hangup = if hangup_is_ignored()? {
            None
        } else {
            Some(signal(SignalKind::hangup())?)
        };
        Ok(Self {
            interrupt: signal(SignalKind::interrupt())?,
            terminate: signal(SignalKind::terminate())?,
            hangup,
        })
    }

    async fn next(&mut self) -> i32 {
        std::future::poll_fn(|context| {
            if self.interrupt.poll_recv(context).is_ready() {
                return std::task::Poll::Ready(130);
            }
            if self.terminate.poll_recv(context).is_ready() {
                return std::task::Poll::Ready(143);
            }
            match &mut self.hangup {
                Some(hangup) => hangup.poll_recv(context).map(|_| 129),
                None => std::task::Poll::Pending,
            }
        })
        .await
    }
}

/// Reading the disposition takes `sigaction`, which needs `unsafe`, and macOS's `ps` does not
/// show it. A child inherits an ignored signal across exec, so a shell that hangs itself up
/// survives exactly when sofie ignores SIGHUP. Asked before any handler is installed, since
/// a handled signal reaches a child as the default.
#[cfg(unix)]
fn hangup_is_ignored() -> io::Result<bool> {
    use std::os::unix::process::ExitStatusExt;

    let probe = Command::new("sh")
        .args(["-c", "kill -s HUP $$"])
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()?;
    match (probe.code(), probe.signal()) {
        (Some(0), _) => Ok(true),
        (_, Some(1)) => Ok(false),
        _ => Err(io::Error::other(format!(
            "telling whether SIGHUP is ignored failed: the probing shell ended with {probe}"
        ))),
    }
}

#[cfg(not(unix))]
struct Signals;

#[cfg(not(unix))]
impl Signals {
    fn listen() -> io::Result<Self> {
        Ok(Self)
    }

    async fn next(&mut self) -> i32 {
        match tokio::signal::ctrl_c().await {
            Ok(()) => 130,
            Err(error) => {
                eprintln!("sofie: listening for Ctrl-C failed: {error}");
                std::future::pending().await
            }
        }
    }
}

#[cfg(unix)]
#[cfg(test)]
mod tests {
    use std::io::{BufRead, BufReader, Read};
    use std::process::{Command, Stdio};
    use std::time::{Duration, Instant};

    use super::{ChildProcess, ProcessGroup, Registry, Running, Signals};

    fn sleeping(registry: &'static Registry) -> ChildProcess {
        registry
            .spawn(
                ProcessGroup::Inherited,
                Command::new("sleep").arg("60"),
                Command::spawn,
            )
            .unwrap()
    }

    fn is_running(pid: u32) -> bool {
        Command::new("kill")
            .args(["-0", &pid.to_string()])
            .stderr(std::process::Stdio::null())
            .status()
            .unwrap()
            .success()
    }

    #[test]
    fn stopping_for_exit_reaps_every_child_and_keeps_another_from_starting() {
        let registry: &'static Registry = Box::leak(Box::default());
        let children = [sleeping(registry), sleeping(registry)];

        let held = registry.stop_all_for_exit();

        assert!(held.is_empty());
        drop(held);
        assert!(children.iter().all(|child| !is_running(child.pid())));
    }

    #[test]
    fn a_dropped_child_is_killed_and_a_waited_one_leaves_the_registry() {
        let registry: &'static Registry = Box::leak(Box::default());
        let dropped = sleeping(registry).pid();

        let finished = registry
            .output(ProcessGroup::Own, Command::new("echo").arg("done"))
            .unwrap();

        assert!(!is_running(dropped));
        assert!(registry.lock().is_empty());
        assert_eq!(finished.stdout, b"done\n");
        assert!(finished.status.success());
    }

    #[test]
    fn a_reaped_child_dropped_later_leaves_a_child_reusing_its_pid_running() {
        let registry: &'static Registry = Box::leak(Box::default());
        let mut finished = registry
            .spawn(
                ProcessGroup::Inherited,
                &mut Command::new("true"),
                Command::spawn,
            )
            .unwrap();
        while finished.exited().unwrap().is_none() {
            std::thread::sleep(Duration::from_millis(5));
        }
        let reusing = Command::new("sleep").arg("60").spawn().unwrap();
        let reusing_pid = reusing.id();
        registry.lock().insert(
            finished.pid(),
            Running {
                child: reusing,
                group: ProcessGroup::Inherited,
            },
        );

        drop(finished);

        assert!(is_running(reusing_pid));
        drop(registry.stop_all_for_exit());
    }

    #[test]
    fn stopping_a_child_in_its_own_group_stops_what_it_started() {
        let registry: &'static Registry = Box::leak(Box::default());
        let mut stdout = None;
        let child = registry
            .spawn(
                ProcessGroup::Own,
                Command::new("sh").args(["-c", "sleep 60 & echo started; wait"]),
                |command| {
                    let mut child = command.stdout(Stdio::piped()).spawn()?;
                    stdout = child.stdout.take();
                    Ok(child)
                },
            )
            .unwrap();
        let mut stdout = BufReader::new(stdout.unwrap());
        stdout.read_line(&mut String::new()).unwrap();
        let killed = Instant::now();

        child.kill();

        // The backgrounded sleep holds the pipe open until it stops too.
        stdout.read_to_end(&mut Vec::new()).unwrap();
        assert!(killed.elapsed() < Duration::from_secs(30));
    }

    #[test]
    fn each_signal_is_told_apart_by_its_exit_code() {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        for (signal, code) in [("-TERM", 143), ("-INT", 130), ("-HUP", 129)] {
            let mut signals = {
                let _context = runtime.enter();
                Signals::listen().unwrap()
            };
            let sent = Command::new("kill")
                .args([signal, &std::process::id().to_string()])
                .status()
                .unwrap();
            assert!(sent.success());

            let received = runtime.block_on(async {
                tokio::time::timeout(Duration::from_secs(5), signals.next()).await
            });

            assert_eq!(received, Ok(code), "{signal}");
        }
    }
}