subc-daemon 0.49.2

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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Windows carried-job tests check each child's parent and kernel creation
//! time before retaining the process handle used for forced cleanup.

use super::*;
use cortexkit_test_support::ScratchDir;

struct OwnedProcess {
    process: subc_os::Process,
    birth: u64,
}
impl OwnedProcess {
    fn capture(pid: u32, parent: u32) -> Self {
        assert_eq!(subc_os::parent_pid(pid), Some(parent));
        let process = subc_os::Process::open(pid).unwrap().unwrap();
        let birth = process.observe().unwrap().start_time;
        assert_eq!(subc_os::parent_pid(pid), Some(parent));
        Self { process, birth }
    }
    fn alive(&self) -> bool {
        !matches!(self.process.wait_for_exit(Duration::ZERO), Ok(true))
            && self
                .process
                .observe()
                .is_some_and(|now| now.start_time == self.birth)
    }
    fn kill(&self) {
        if self.alive() {
            assert!(self
                .process
                .force_stop(self.birth, Duration::from_secs(5))
                .unwrap());
        }
    }
    async fn gone(&self) {
        timeout(Duration::from_secs(5), async {
            while self.alive() {
                sleep(Duration::from_millis(5)).await;
            }
        })
        .await
        .expect("the job must stop its owned holder");
    }
}
impl Drop for OwnedProcess {
    fn drop(&mut self) {
        if self.alive() {
            let _ = self.process.force_stop(self.birth, Duration::from_secs(5));
        }
    }
}

struct MonitorGuard(Option<SupervisedModule>);
impl Drop for MonitorGuard {
    fn drop(&mut self) {
        if let Some(module) = &self.0 {
            if let Some(monitor) = module.inner.monitor.lock().unwrap().take() {
                monitor.abort();
            }
        }
    }
}

struct Fixture {
    module: MonitorGuard,
    supervisor: Supervisor,
    _dir: ScratchDir,
    spec: ModuleSpec,
    first: OwnedProcess,
    holder: OwnedProcess,
    holder_pid: u32,
    holder_file: PathBuf,
    record: PathBuf,
}
impl Fixture {
    async fn new(label: &str, budget: u32, backoff: Duration) -> Self {
        let dir = ScratchDir::new(label);
        let holder_file = dir.join("holder.pid");
        let record = dir.join("live-children.json");
        let stub = cortexkit_test_support::cargo_profile_bin("fake-aft-stub");
        assert!(
            stub.is_file(),
            "build prerequisite: cargo build -p subc-core --bin fake-aft-stub"
        );
        let mut spec = ModuleSpec {
            script: None,
            carry_containment: true,
            module_id: label.into(),
            program: stub,
            args: Vec::new(),
            env: vec![
                ("FAKE_AFT_NEVER_CONNECT".into(), "1".into()),
                (
                    "FAKE_AFT_GRANDCHILD_PID_FILE".into(),
                    holder_file.display().to_string(),
                ),
            ],
            reserved: false,
            reserved_prefixes: Vec::new(),
            protocol: ModuleProtocol::None,
            overlap: Default::default(),
        };
        spec.env.extend(
            ["XDG_DATA_HOME", "XDG_RUNTIME_DIR", "XDG_CONFIG_HOME"]
                .into_iter()
                .map(|key| (key.into(), dir.display().to_string())),
        );
        let policy = RestartPolicy::new(budget, backoff);
        let supervisor = Supervisor::new_for_test(Arc::new(Registry::default()), policy)
            .with_drain_timeout(Duration::from_millis(20))
            .with_forwarding(Arc::new(ForwardingTable::default()))
            .with_live_children_record(&record);
        let module = MonitorGuard(Some(
            supervisor
                .supervise_configured_requested(
                    spec.clone(),
                    true,
                    HealthConfig::default(),
                    None,
                    policy,
                    Duration::from_secs(1),
                    None,
                )
                .unwrap(),
        ));
        let first = timeout(Duration::from_secs(5), async {
            loop {
                if let Some(pid) = module.0.as_ref().unwrap().status().unwrap().pid {
                    break OwnedProcess::capture(pid, std::process::id());
                }
                sleep(Duration::from_millis(5)).await;
            }
        })
        .await
        .expect("the assigned first leader must start");
        let leader_pid = module.0.as_ref().unwrap().status().unwrap().pid.unwrap();
        let (holder, holder_pid) = timeout(Duration::from_secs(5), async {
            loop {
                if let Some(pid) = std::fs::read_to_string(&holder_file)
                    .ok()
                    .and_then(|text| text.trim().parse().ok())
                {
                    break (OwnedProcess::capture(pid, leader_pid), pid);
                }
                sleep(Duration::from_millis(5)).await;
            }
        })
        .await
        .expect("the owned leader must publish its own holder");
        // Only the first generation creates the holder. Updating the next
        // generation's environment avoids adding a second holder or trusting
        // a stale PID file inside the child fixture.
        spec.env
            .retain(|(key, _)| key != "FAKE_AFT_GRANDCHILD_PID_FILE");
        module
            .0
            .as_ref()
            .unwrap()
            .update_configuration(spec.clone(), HealthConfig::default(), None)
            .await
            .unwrap();
        Self {
            module,
            supervisor,
            _dir: dir,
            spec,
            first,
            holder,
            holder_pid,
            holder_file,
            record,
        }
    }
    fn module(&self) -> &SupervisedModule {
        self.module.0.as_ref().unwrap()
    }
    async fn status(&self, predicate: impl Fn(&ModuleStatus) -> bool) -> ModuleStatus {
        timeout(Duration::from_secs(5), async {
            loop {
                let status = self.module().status().unwrap();
                if predicate(&status) {
                    break status;
                }
                sleep(Duration::from_millis(5)).await;
            }
        })
        .await
        .expect("the carried job must reach the expected lifecycle state")
    }
    fn same_holder(&self) {
        assert!(
            self.holder.alive(),
            "the original holder must survive the leader replacement"
        );
        assert_eq!(
            std::fs::read_to_string(&self.holder_file)
                .unwrap()
                .trim()
                .parse::<u32>()
                .unwrap(),
            self.holder_pid
        );
        assert!(
            crate::live_children::read_record(&self.record)
                .unwrap()
                .iter()
                .any(|entry| entry.container.is_some()),
            "the container roster entry must outlive every leader"
        );
    }
    async fn stop(&self) {
        self.module().stop().await.unwrap();
        self.holder.gone().await;
        assert!(crate::live_children::read_record(&self.record)
            .unwrap()
            .is_empty());
    }
}

#[tokio::test]
async fn carried_windows_operator_restart_keeps_holder_across_two_generations() {
    let fixture = Fixture::new("carried-job-operator", 3, Duration::from_millis(5)).await;
    let mut generation = fixture.module().status().unwrap().spawn_generation;
    for _ in 0..2 {
        fixture.module().restart(None).await.unwrap();
        generation = fixture
            .status(|status| {
                status.state == ModuleState::Running && status.spawn_generation > generation
            })
            .await
            .spawn_generation;
        fixture.same_holder();
    }
    fixture.stop().await;
}

#[tokio::test]
async fn carried_windows_crash_restart_keeps_holder() {
    let fixture = Fixture::new("carried-job-crash", 3, Duration::from_millis(5)).await;
    let generation = fixture.module().status().unwrap().spawn_generation;
    fixture.first.kill();
    fixture
        .status(|status| {
            status.state == ModuleState::Running && status.spawn_generation > generation
        })
        .await;
    fixture.same_holder();
    fixture.stop().await;
}

#[tokio::test]
async fn carried_windows_stop_during_backoff_terminates_holder() {
    let fixture = Fixture::new("carried-job-stop-backoff", 3, Duration::from_secs(60)).await;
    fixture.first.kill();
    fixture
        .status(|status| status.state == ModuleState::Restarting && status.pid.is_none())
        .await;
    fixture.same_holder();
    fixture.stop().await;
}

#[tokio::test]
async fn carried_windows_disable_during_backoff_terminates_holder() {
    let fixture = Fixture::new("carried-job-disable-backoff", 3, Duration::from_secs(60)).await;
    fixture.first.kill();
    fixture
        .status(|status| status.state == ModuleState::Restarting && status.pid.is_none())
        .await;
    fixture.same_holder();
    assert!(fixture.module().set_enabled(false).await.unwrap());
    fixture.holder.gone().await;
    assert!(crate::live_children::read_record(&fixture.record)
        .unwrap()
        .is_empty());
    fixture.module().stop().await.unwrap();
}

#[tokio::test]
async fn carried_windows_budget_exhaustion_terminates_holder() {
    let fixture = Fixture::new("carried-job-budget", 0, Duration::ZERO).await;
    fixture.first.kill();
    fixture
        .status(|status| status.state == ModuleState::Failed)
        .await;
    fixture.holder.gone().await;
    fixture.module().stop().await.unwrap();
}

#[tokio::test]
async fn carried_windows_monitor_abort_terminates_holder() {
    let fixture = Fixture::new("carried-job-abort", 3, Duration::ZERO).await;
    let monitor = fixture
        .module()
        .inner
        .monitor
        .lock()
        .unwrap()
        .take()
        .unwrap();
    monitor.abort();
    assert!(monitor.await.unwrap_err().is_cancelled());
    fixture.holder.gone().await;
}

#[tokio::test]
async fn carried_windows_health_restart_keeps_holder() {
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    let fixture = Fixture::new("carried-job-health", 3, Duration::from_millis(5)).await;
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();
    let server = tokio::spawn(async move {
        let mut first = true;
        loop {
            let (mut stream, _) = listener.accept().await.unwrap();
            let mut method = [0; 4];
            stream.read_exact(&mut method).await.unwrap();
            assert_eq!(&method, b"GET ");
            let response: &[u8] = if first {
                b"HTTP/1.1 503 Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
            } else {
                b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
            };
            first = false;
            let _ = stream.write_all(response).await;
        }
    });
    let generation = fixture.module().status().unwrap().spawn_generation;
    fixture
        .module()
        .update_configuration(
            fixture.spec.clone(),
            HealthConfig {
                http: Some(format!("http://{address}/health")),
                cadence: Duration::from_millis(50),
                deadline: Duration::from_millis(100),
                failure_threshold: 1,
                on_failing: HealthAction::Restart,
                ..HealthConfig::default()
            },
            None,
        )
        .await
        .unwrap();
    fixture
        .status(|status| {
            status.state == ModuleState::Running && status.spawn_generation > generation
        })
        .await;
    fixture.same_holder();
    fixture.stop().await;
    server.abort();
}

#[tokio::test]
async fn carried_windows_reload_keeps_holder() {
    let fixture = Fixture::new("carried-job-reload", 3, Duration::from_millis(5)).await;
    let mut replacement = fixture.spec.clone();
    replacement.protocol = ModuleProtocol::Subc;
    fixture
        .module()
        .update_configuration(replacement, HealthConfig::default(), None)
        .await
        .unwrap();
    let generation = fixture.module().status().unwrap().spawn_generation;
    let connection = crate::registry::ConnectionId::new(43);
    // Supply HELLO's Registry effect directly; the actual child, suspended
    // assignment, replacement and holder are real Windows processes/jobs.
    let (reloaded, ()) = tokio::join!(fixture.module().reload(), async {
        fixture
            .status(|status| status.pid.is_some() && status.spawn_generation > generation)
            .await;
        fixture
            .supervisor
            .registry
            .register_with_control_ops(
                subc_protocol::manifest::ModuleManifest::builder(&fixture.spec.module_id, "0.1.0")
                    .build(),
                1,
                connection,
                Vec::new(),
            )
            .unwrap();
    });
    reloaded.unwrap();
    fixture.same_holder();
    fixture
        .supervisor
        .registry
        .deregister_connection(connection)
        .unwrap();
    fixture.stop().await;
}

#[tokio::test]
async fn carried_windows_reload_registration_timeout_terminates_holder() {
    let fixture = Fixture::new("carried-job-reload-timeout", 3, Duration::from_millis(5)).await;
    let error = fixture
        .module()
        .reload()
        .await
        .expect_err("the replacement never registers");
    assert!(
        matches!(error, SuperviseError::ReloadFailed { ref reason, .. } if reason.contains("did not register")),
        "{error:?}"
    );
    fixture.holder.gone().await;
    fixture.module().stop().await.unwrap();
}

#[tokio::test]
async fn carried_windows_replacements_share_the_original_kernel_job() {
    fn independently_observed_members(leaders: &[u32], holder: u32, current: u32) -> u64 {
        let owned = leaders
            .iter()
            .copied()
            .chain([holder])
            .map(|pid| pid.to_string())
            .collect::<Vec<_>>()
            .join(",");
        let output = std::process::Command::new("powershell.exe")
            .args(["-NoLogo", "-NoProfile", "-NonInteractive", "-Command"])
            .arg(format!("Get-CimInstance Win32_Process | Where-Object {{ $_.ProcessId -in @({owned}) -or $_.ParentProcessId -in @({owned}) }} | Select-Object ProcessId,ParentProcessId,Name | ConvertTo-Json -Compress"))
            .output().unwrap();
        assert!(output.status.success());
        let facts: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
        let facts = facts
            .as_array()
            .expect("both owned live fixture processes must be present");
        let mut stubs = std::collections::BTreeSet::new();
        for entry in facts {
            let name = entry["Name"].as_str().unwrap();
            if name.eq_ignore_ascii_case("fake-aft-stub.exe") {
                stubs.insert(u32::try_from(entry["ProcessId"].as_u64().unwrap()).unwrap());
            } else {
                assert!(
                    name.eq_ignore_ascii_case("conhost.exe"),
                    "unexpected fixture descendant: {entry:?}"
                );
            }
        }
        assert_eq!(
            stubs,
            [holder, current].into_iter().collect(),
            "only the original holder and current leader may be live"
        );
        // Each stub can create a Windows console host, which also belongs in
        // the job. Count those from an independent process-tree observation,
        // not from the job query whose membership we are checking.
        u64::try_from(facts.len()).unwrap()
    }
    let fixture = Fixture::new("carried-job-kernel-membership", 3, Duration::from_millis(5)).await;
    let crate::subtree_resources::Target::Job(original) = fixture.module().subtree_target() else {
        panic!("Windows resources must expose the job query target")
    };
    let job = original
        .upgrade()
        .expect("the actor must own the original job");
    // Assigned-process counts can retain exited entries while a process handle
    // remains open. Identity-safe cleanup retains leader handles deliberately,
    // so use the query that independently confirms each live member instead.
    let live_members = || {
        job.lock()
            .unwrap()
            .query()
            .unwrap()
            .memory(std::time::Instant::now() + Duration::from_secs(5))
            .unwrap()
            .process_count
    };
    let mut leaders = vec![fixture.first.process.pid()];
    let mut retained_leaders = Vec::new();
    let expected = independently_observed_members(&leaders, fixture.holder_pid, leaders[0]);
    assert_eq!(live_members(), expected);
    assert_eq!(
        Arc::strong_count(&job),
        2,
        "only the actor's container and this test retain strong job Arcs; children are weak owners"
    );
    let mut generation = fixture.module().status().unwrap().spawn_generation;
    for _ in 0..2 {
        fixture.module().restart(None).await.unwrap();
        let status = fixture
            .status(|status| {
                status.state == ModuleState::Running && status.spawn_generation > generation
            })
            .await;
        generation = status.spawn_generation;
        let pid = status.pid.unwrap();
        retained_leaders.push(OwnedProcess::capture(pid, std::process::id()));
        leaders.push(pid);
        fixture.same_holder();
        assert!(
            !fixture.first.alive(),
            "the old module leader must have exited"
        );
        let crate::subtree_resources::Target::Job(current) = fixture.module().subtree_target()
        else {
            panic!("replacement must expose its actual assigned job")
        };
        assert!(
            std::sync::Weak::ptr_eq(&original, &current),
            "replacement resource accounting must point to the original job"
        );
        let expected = independently_observed_members(&leaders, fixture.holder_pid, pid);
        assert_eq!(live_members(), expected, "the original kernel job must contain the replacement and the whole carried tree, not only old survivors");
        assert_eq!(Arc::strong_count(&job), 2);
    }
    fixture.stop().await;
    assert_eq!(
        live_members(),
        0,
        "terminal cleanup explicitly terminates the job even while this test retains a handle"
    );
    drop(retained_leaders);
}

#[tokio::test]
async fn carried_windows_assignment_failure_terminates_old_holder_without_resuming() {
    let fixture = Fixture::new(
        "carried-job-assignment-rejection",
        3,
        Duration::from_millis(5),
    )
    .await;
    let marker = fixture._dir.join("rejected-child-ran");
    let mut spec = fixture.spec.clone();
    spec.env.extend([
        ("SUBC_TEST_ASSIGNMENT_FAILURE".into(), "1".into()),
        ("FAKE_AFT_PID_PATH".into(), marker.display().to_string()),
    ]);
    fixture
        .module()
        .update_configuration(spec, HealthConfig::default(), None)
        .await
        .unwrap();
    fixture.module().restart(None).await.unwrap();
    fixture
        .status(|status| status.state == ModuleState::Failed)
        .await;
    fixture.holder.gone().await;
    assert!(
        !marker.exists(),
        "the suspended rejected generation must never execute its startup marker"
    );
    assert!(crate::live_children::read_record(&fixture.record)
        .unwrap()
        .is_empty());
    fixture.module().stop().await.unwrap();
}