a3s-box-runtime 3.1.0

MicroVM runtime engine — VM lifecycle, OCI images, attestation, networking
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! Runtime handler for a live `crun` Sandbox container.

use std::path::{Path, PathBuf};
use std::process::{Child, Command, Output};
use std::sync::Mutex;
use std::time::{Duration, Instant};

use a3s_box_core::error::{BoxError, Result};
use a3s_box_core::vmm::{VmHandler, VmMetrics};
use serde::Deserialize;
use sysinfo::{Pid, System};

// `crun kill` accepts Linux signal numbers even though this module must also
// type-check on hosts where libc does not expose POSIX signal constants.
const SIGKILL_NUMBER: i32 = 9;
const LIFECYCLE_TIMEOUT: Duration = Duration::from_secs(5);
const LIFECYCLE_POLL_INTERVAL: Duration = Duration::from_millis(25);

#[derive(Debug, Deserialize)]
pub(crate) struct CrunState {
    pub status: String,
    #[serde(default)]
    #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
    pub pid: u32,
}

/// Owns both the foreground `crun run` process and the OCI runtime state.
/// Lifecycle operations always target the container ID through `crun`; merely
/// signalling the wrapper process is never treated as cleanup.
/// Dropping the in-process handle deliberately detaches without destroying the
/// workload so short-lived CLI commands can launch persistent boxes. Explicit
/// lifecycle operations and crash reconciliation own runtime cleanup.
pub struct CrunHandler {
    runtime_path: PathBuf,
    runtime_root: PathBuf,
    container_id: String,
    init_pid: u32,
    process: Option<Child>,
    log_worker: Option<Child>,
    log_worker_pid: Option<u32>,
    log_worker_pid_start_time: Option<u64>,
    metrics_sys: Mutex<System>,
    exit_code: Option<i32>,
    bundle_dir: PathBuf,
    runtime_record: PathBuf,
    cleaned: bool,
}

#[cfg(target_os = "linux")]
pub(crate) struct CrunHandlerSpec {
    runtime_path: PathBuf,
    runtime_root: PathBuf,
    container_id: String,
    init_pid: u32,
    bundle_dir: PathBuf,
    runtime_record: PathBuf,
}

#[cfg(target_os = "linux")]
impl CrunHandlerSpec {
    pub(crate) fn new(
        runtime_path: PathBuf,
        runtime_root: PathBuf,
        container_id: String,
        init_pid: u32,
        bundle_dir: PathBuf,
        runtime_record: PathBuf,
    ) -> Self {
        Self {
            runtime_path,
            runtime_root,
            container_id,
            init_pid,
            bundle_dir,
            runtime_record,
        }
    }
}

impl CrunHandler {
    #[cfg(target_os = "linux")]
    pub(crate) fn from_child(
        spec: CrunHandlerSpec,
        process: Child,
        log_worker: Child,
        log_worker_pid_start_time: u64,
    ) -> Self {
        let log_worker_pid = log_worker.id();
        Self {
            runtime_path: spec.runtime_path,
            runtime_root: spec.runtime_root,
            container_id: spec.container_id,
            init_pid: spec.init_pid,
            process: Some(process),
            log_worker: Some(log_worker),
            log_worker_pid: Some(log_worker_pid),
            log_worker_pid_start_time: Some(log_worker_pid_start_time),
            metrics_sys: Mutex::new(System::new()),
            exit_code: None,
            bundle_dir: spec.bundle_dir,
            runtime_record: spec.runtime_record,
            cleaned: false,
        }
    }

    #[cfg(all(target_os = "linux", feature = "vm"))]
    pub(crate) fn from_recorded_runtime(
        spec: CrunHandlerSpec,
        log_worker_pid: Option<u32>,
        log_worker_pid_start_time: Option<u64>,
    ) -> Self {
        Self {
            runtime_path: spec.runtime_path,
            runtime_root: spec.runtime_root,
            container_id: spec.container_id,
            init_pid: spec.init_pid,
            process: None,
            log_worker: None,
            log_worker_pid,
            log_worker_pid_start_time,
            metrics_sys: Mutex::new(System::new()),
            exit_code: None,
            bundle_dir: spec.bundle_dir,
            runtime_record: spec.runtime_record,
            cleaned: false,
        }
    }

    pub(crate) fn query_state_at(
        runtime_path: &Path,
        runtime_root: &Path,
        container_id: &str,
    ) -> Result<Option<CrunState>> {
        let output = Command::new(runtime_path)
            .arg("--root")
            .arg(runtime_root)
            .arg("state")
            .arg(container_id)
            .env("LC_ALL", "C")
            .output()
            .map_err(|error| BoxError::BoxBootError {
                message: format!("Failed to query Sandbox runtime state: {error}"),
                hint: None,
            })?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            let normalized = stderr.to_ascii_lowercase();
            if normalized.contains("does not exist")
                || normalized.contains("not found")
                || normalized.contains("no such file or directory")
            {
                return Ok(None);
            }
            return Err(BoxError::BoxBootError {
                message: format!("crun state failed for {container_id}: {}", stderr.trim()),
                hint: None,
            });
        }
        let state =
            serde_json::from_slice(&output.stdout).map_err(|error| BoxError::BoxBootError {
                message: format!("Invalid crun state response: {error}"),
                hint: None,
            })?;
        Ok(Some(state))
    }

    pub(crate) fn pause_at(
        runtime_path: &Path,
        runtime_root: &Path,
        container_id: &str,
    ) -> Result<()> {
        Self::transition_state_at(
            runtime_path,
            runtime_root,
            container_id,
            "pause",
            &["created", "running"],
            "paused",
        )
    }

    pub(crate) fn resume_at(
        runtime_path: &Path,
        runtime_root: &Path,
        container_id: &str,
    ) -> Result<()> {
        Self::transition_state_at(
            runtime_path,
            runtime_root,
            container_id,
            "resume",
            &["paused"],
            "running",
        )
    }

    fn transition_state_at(
        runtime_path: &Path,
        runtime_root: &Path,
        container_id: &str,
        operation: &str,
        source_states: &[&str],
        target_state: &str,
    ) -> Result<()> {
        let state =
            Self::query_state_at(runtime_path, runtime_root, container_id)?.ok_or_else(|| {
                BoxError::StateError(format!(
                    "Sandbox runtime {container_id} does not exist for {operation}"
                ))
            })?;
        if state.status == target_state {
            return Ok(());
        }
        if !source_states.contains(&state.status.as_str()) {
            return Err(BoxError::StateError(format!(
                "Cannot {operation} Sandbox runtime {container_id} in state {}",
                state.status
            )));
        }

        let output = Command::new(runtime_path)
            .arg("--root")
            .arg(runtime_root)
            .arg(operation)
            .arg(container_id)
            .env("LC_ALL", "C")
            .output()
            .map_err(|error| {
                BoxError::ExecError(format!("Failed to run crun {operation}: {error}"))
            })?;
        if !output.status.success() {
            if Self::query_state_at(runtime_path, runtime_root, container_id)?
                .is_some_and(|state| state.status == target_state)
            {
                return Ok(());
            }
            return Err(runtime_failure(&format!("crun {operation}"), &output));
        }

        let deadline = Instant::now() + LIFECYCLE_TIMEOUT;
        loop {
            match Self::query_state_at(runtime_path, runtime_root, container_id)? {
                Some(state) if state.status == target_state => return Ok(()),
                Some(state) if state.status == "stopped" => {
                    return Err(BoxError::StateError(format!(
                        "Sandbox runtime {container_id} stopped while waiting for {operation}"
                    )))
                }
                None => {
                    return Err(BoxError::StateError(format!(
                        "Sandbox runtime {container_id} disappeared while waiting for {operation}"
                    )))
                }
                Some(_) if Instant::now() < deadline => {
                    std::thread::sleep(LIFECYCLE_POLL_INTERVAL);
                }
                Some(state) => {
                    return Err(BoxError::StateError(format!(
                        "Timed out waiting for Sandbox runtime {container_id} to enter {target_state}; current state is {}",
                        state.status
                    )))
                }
            }
        }
    }

    fn runtime_command(&self, operation: &str) -> Command {
        let mut command = Command::new(&self.runtime_path);
        command
            .arg("--root")
            .arg(&self.runtime_root)
            .arg(operation)
            .env("LC_ALL", "C");
        command
    }

    fn signal_container(&self, signal: i32) -> Result<()> {
        let output = self
            .runtime_command("kill")
            .arg(&self.container_id)
            .arg(signal.to_string())
            .output()
            .map_err(|error| BoxError::ExecError(format!("Failed to run crun kill: {error}")))?;
        if output.status.success() {
            return Ok(());
        }
        match self.query_state()? {
            None => return Ok(()),
            Some(state) if state.status == "stopped" => return Ok(()),
            Some(_) => {}
        }
        Err(runtime_failure("crun kill", &output))
    }

    fn query_state(&self) -> Result<Option<CrunState>> {
        Self::query_state_at(&self.runtime_path, &self.runtime_root, &self.container_id)
    }

    fn wait_for_exit(&mut self, timeout_ms: u64) -> Result<bool> {
        let deadline = Instant::now() + Duration::from_millis(timeout_ms);
        loop {
            if self.poll_child()?.is_some() || self.query_state()?.is_none() {
                return Ok(true);
            }
            if Instant::now() >= deadline {
                return Ok(false);
            }
            std::thread::sleep(Duration::from_millis(25));
        }
    }

    fn poll_child(&mut self) -> Result<Option<i32>> {
        if self.exit_code.is_some() {
            return Ok(self.exit_code);
        }
        let Some(process) = self.process.as_mut() else {
            return Ok(None);
        };
        match process.try_wait() {
            Ok(Some(status)) => {
                self.exit_code = status.code().or(Some(128));
                Ok(self.exit_code)
            }
            Ok(None) => Ok(None),
            Err(error) => Err(BoxError::ExecError(format!(
                "Failed to poll crun process for {}: {error}",
                self.container_id
            ))),
        }
    }

    fn reap_child(&mut self) {
        let Some(mut process) = self.process.take() else {
            return;
        };
        match process.try_wait() {
            Ok(Some(status)) => {
                self.exit_code = status.code().or(self.exit_code).or(Some(128));
                return;
            }
            Ok(None) => {
                // OCI cleanup already ran before this helper. Killing a stuck
                // wrapper here cannot replace container cleanup; it only
                // guarantees that handler teardown never blocks indefinitely.
                let _ = process.kill();
            }
            Err(error) => {
                tracing::warn!(
                    container_id = %self.container_id,
                    %error,
                    "Failed to poll crun run process before reaping"
                );
                let _ = process.kill();
            }
        }
        match process.wait() {
            Ok(status) => {
                self.exit_code = status.code().or(self.exit_code).or(Some(128));
            }
            Err(error) => {
                tracing::warn!(
                    container_id = %self.container_id,
                    %error,
                    "Failed to reap crun run process"
                );
            }
        }
    }

    fn reap_log_worker(&mut self) {
        const LOG_WORKER_EXIT_TIMEOUT: Duration = Duration::from_secs(2);
        const LOG_WORKER_EXIT_POLL: Duration = Duration::from_millis(10);

        if let Some(mut worker) = self.log_worker.take() {
            let deadline = Instant::now() + LOG_WORKER_EXIT_TIMEOUT;
            loop {
                match worker.try_wait() {
                    Ok(Some(_)) => return,
                    Ok(None) if Instant::now() < deadline => {
                        std::thread::sleep(LOG_WORKER_EXIT_POLL);
                    }
                    Ok(None) => break,
                    Err(error) => {
                        tracing::warn!(
                            container_id = %self.container_id,
                            %error,
                            "Failed to poll Sandbox log worker before reaping"
                        );
                        break;
                    }
                }
            }
            tracing::warn!(
                container_id = %self.container_id,
                "Sandbox log worker did not exit after crun; terminating it"
            );
            let _ = worker.kill();
            let _ = worker.wait();
            return;
        }

        let (Some(pid), Some(start_time)) = (self.log_worker_pid, self.log_worker_pid_start_time)
        else {
            return;
        };
        let deadline = Instant::now() + LOG_WORKER_EXIT_TIMEOUT;
        while crate::process::is_process_running_with_identity(pid, Some(start_time))
            && Instant::now() < deadline
        {
            std::thread::sleep(LOG_WORKER_EXIT_POLL);
        }
        if crate::process::is_process_running_with_identity(pid, Some(start_time)) {
            tracing::warn!(
                container_id = %self.container_id,
                log_worker_pid = pid,
                "Recovered Sandbox log worker did not exit after crun; terminating it"
            );
            // The start-time token was revalidated immediately before the
            // signal, so a reused PID cannot be targeted.
            #[cfg(target_os = "linux")]
            if let Ok(pid) = i32::try_from(pid) {
                unsafe {
                    libc::kill(pid, libc::SIGKILL);
                }
            }
        }
        #[cfg(target_os = "linux")]
        if !crate::process::wait_for_process_exit_with_identity(
            pid,
            start_time,
            LOG_WORKER_EXIT_TIMEOUT,
        ) {
            tracing::warn!(
                container_id = %self.container_id,
                log_worker_pid = pid,
                "Recovered Sandbox log worker remained present after cleanup"
            );
        }
    }

    fn delete_runtime_state(&mut self) -> Result<()> {
        if self.cleaned {
            return Ok(());
        }
        let output = self
            .runtime_command("delete")
            .arg("--force")
            .arg(&self.container_id)
            .output()
            .map_err(|error| BoxError::ExecError(format!("Failed to run crun delete: {error}")))?;
        if !output.status.success() && self.query_state()?.is_some() {
            return Err(runtime_failure("crun delete --force", &output));
        }
        // Reap the wrapper first: its inherited stdout/stderr descriptors must
        // close before the worker treats EOF as final. Then wait for the worker
        // to drain both streams before removing durable generation artifacts.
        self.reap_child();
        self.reap_log_worker();
        self.cleaned = true;
        remove_file_if_exists(&self.runtime_record);
        remove_dir_if_exists(&self.bundle_dir);
        remove_dir_if_exists(&self.runtime_root);
        Ok(())
    }
}

impl VmHandler for CrunHandler {
    fn stop(&mut self, signal: i32, timeout_ms: u64) -> Result<()> {
        let mut first_error = None;
        if self.query_state()?.is_some() {
            if let Err(error) = self.signal_container(signal) {
                first_error = Some(error);
            }
            match self.wait_for_exit(timeout_ms) {
                Ok(true) => {}
                Ok(false) => {
                    tracing::warn!(
                        container_id = %self.container_id,
                        timeout_ms,
                        "Sandbox did not stop gracefully; sending SIGKILL"
                    );
                    if let Err(error) = self.signal_container(SIGKILL_NUMBER) {
                        first_error.get_or_insert(error);
                    }
                    let _ = self.wait_for_exit(2_000);
                }
                Err(error) => {
                    first_error.get_or_insert(error);
                    let _ = self.signal_container(SIGKILL_NUMBER);
                }
            }
        }

        if let Err(error) = self.delete_runtime_state() {
            first_error.get_or_insert(error);
        }
        match first_error {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }

    fn metrics(&self) -> VmMetrics {
        let pid = Pid::from_u32(self.init_pid);
        let mut system = match self.metrics_sys.lock() {
            Ok(system) => system,
            Err(error) => {
                tracing::warn!(%error, "Sandbox metrics lock is poisoned");
                return VmMetrics::default();
            }
        };
        system.refresh_process(pid);
        system
            .process(pid)
            .map(|process| VmMetrics {
                cpu_percent: Some(process.cpu_usage()),
                memory_bytes: Some(process.memory()),
            })
            .unwrap_or_default()
    }

    fn is_running(&self) -> bool {
        self.query_state()
            .ok()
            .flatten()
            .is_some_and(|state| matches!(state.status.as_str(), "created" | "running" | "paused"))
    }

    fn has_exited(&self) -> bool {
        !self.is_running()
    }

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

    fn exit_code(&self) -> Option<i32> {
        self.exit_code
    }

    fn try_wait_exit(&mut self) -> Result<Option<i32>> {
        let exit = self.poll_child()?;
        if exit.is_some() {
            self.delete_runtime_state()?;
        }
        Ok(exit)
    }
}

fn runtime_failure(operation: &str, output: &Output) -> BoxError {
    let stderr = String::from_utf8_lossy(&output.stderr);
    BoxError::ExecError(format!(
        "{operation} exited with {}: {}",
        output.status,
        stderr.trim()
    ))
}

fn remove_file_if_exists(path: &Path) {
    match std::fs::remove_file(path) {
        Ok(()) => {}
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => {
            tracing::warn!(path = %path.display(), %error, "Failed to remove Sandbox runtime record")
        }
    }
}

fn remove_dir_if_exists(path: &Path) {
    match std::fs::remove_dir_all(path) {
        Ok(()) => {}
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => {
            tracing::warn!(path = %path.display(), %error, "Failed to remove Sandbox runtime directory")
        }
    }
}

#[cfg(all(test, target_os = "linux"))]
mod tests {
    use super::*;

    fn lifecycle_runtime(temporary: &tempfile::TempDir) -> (PathBuf, PathBuf) {
        use std::os::unix::fs::PermissionsExt;

        let runtime_root = temporary.path().join("runtime");
        std::fs::create_dir(&runtime_root).unwrap();
        std::fs::write(runtime_root.join("state"), "running\n").unwrap();
        let runtime = temporary.path().join("crun-fixture");
        std::fs::write(
            &runtime,
            r#"#!/bin/sh
root="$2"
operation="$3"
case "$operation" in
  state)
    status="$(cat "$root/state")"
    printf '{"status":"%s","pid":42}\n' "$status"
    ;;
  pause)
    printf 'paused\n' > "$root/state"
    ;;
  resume)
    printf 'running\n' > "$root/state"
    ;;
  *)
    exit 64
    ;;
esac
"#,
        )
        .unwrap();
        std::fs::set_permissions(&runtime, std::fs::Permissions::from_mode(0o700)).unwrap();
        (runtime, runtime_root)
    }

    #[test]
    fn crun_pause_and_resume_are_state_checked_and_idempotent() {
        let temporary = tempfile::tempdir().unwrap();
        let (runtime, runtime_root) = lifecycle_runtime(&temporary);

        CrunHandler::pause_at(&runtime, &runtime_root, "sandbox-1").unwrap();
        CrunHandler::pause_at(&runtime, &runtime_root, "sandbox-1").unwrap();
        assert_eq!(
            CrunHandler::query_state_at(&runtime, &runtime_root, "sandbox-1")
                .unwrap()
                .unwrap()
                .status,
            "paused"
        );

        CrunHandler::resume_at(&runtime, &runtime_root, "sandbox-1").unwrap();
        CrunHandler::resume_at(&runtime, &runtime_root, "sandbox-1").unwrap();
        assert_eq!(
            CrunHandler::query_state_at(&runtime, &runtime_root, "sandbox-1")
                .unwrap()
                .unwrap()
                .status,
            "running"
        );
    }

    #[test]
    fn crun_pause_rejects_a_terminal_runtime() {
        let temporary = tempfile::tempdir().unwrap();
        let (runtime, runtime_root) = lifecycle_runtime(&temporary);
        std::fs::write(runtime_root.join("state"), "stopped\n").unwrap();

        let error = CrunHandler::pause_at(&runtime, &runtime_root, "sandbox-1").unwrap_err();

        assert!(error.to_string().contains("state stopped"));
    }

    #[cfg(feature = "vm")]
    #[test]
    fn recorded_runtime_handler_attaches_without_owning_a_wrapper_process() {
        let temporary = tempfile::tempdir().unwrap();
        let runtime_path = PathBuf::from("/bin/true");
        let runtime_root = temporary.path().join("runtime");
        let bundle_dir = temporary.path().join("bundle");
        let runtime_record = temporary.path().join("runtime.json");

        let handler = CrunHandler::from_recorded_runtime(
            CrunHandlerSpec::new(
                runtime_path.clone(),
                runtime_root.clone(),
                "recorded-test".to_string(),
                42,
                bundle_dir.clone(),
                runtime_record.clone(),
            ),
            None,
            None,
        );

        assert_eq!(handler.runtime_path, runtime_path);
        assert_eq!(handler.runtime_root, runtime_root);
        assert_eq!(handler.container_id, "recorded-test");
        assert_eq!(handler.pid(), 42);
        assert!(handler.process.is_none());
        assert!(handler.log_worker.is_none());
        assert!(handler.log_worker_pid.is_none());
        assert_eq!(handler.bundle_dir, bundle_dir);
        assert_eq!(handler.runtime_record, runtime_record);
        assert!(!handler.cleaned);
    }

    #[test]
    fn dropping_handler_detaches_from_live_runtime_process() {
        let temporary = tempfile::tempdir().unwrap();
        let child = Command::new("sleep").arg("30").spawn().unwrap();
        let pid = child.id();
        let log_worker = Command::new("sleep").arg("30").spawn().unwrap();
        let log_worker_pid = log_worker.id();
        let log_worker_pid_start_time = crate::process::pid_start_time(log_worker_pid).unwrap();
        let handler = CrunHandler::from_child(
            CrunHandlerSpec::new(
                PathBuf::from("/bin/true"),
                temporary.path().join("runtime"),
                "detached-test".to_string(),
                pid,
                temporary.path().join("bundle"),
                temporary.path().join("runtime.json"),
            ),
            child,
            log_worker,
            log_worker_pid_start_time,
        );

        drop(handler);
        let remained_alive = unsafe { libc::kill(pid as i32, 0) == 0 };
        let log_worker_remained_alive = unsafe { libc::kill(log_worker_pid as i32, 0) == 0 };

        unsafe {
            libc::kill(pid as i32, libc::SIGKILL);
            let mut status = 0;
            libc::waitpid(pid as i32, &mut status, 0);
            libc::kill(log_worker_pid as i32, libc::SIGKILL);
            libc::waitpid(log_worker_pid as i32, &mut status, 0);
        }
        assert!(
            remained_alive,
            "dropping a runtime handle must not destroy a detached Sandbox"
        );
        assert!(
            log_worker_remained_alive,
            "dropping a runtime handle must not destroy its detached log worker"
        );
    }
}