a3s-box-runtime 3.2.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
//! Durable Sandbox runtime recovery for the production local execution backend.

use super::*;

impl VmLocalExecutionBackend {
    pub(super) async fn inspect_sandbox(
        &self,
        record: &BoxRecord,
    ) -> ExecutionManagerResult<LocalExecutionObservation> {
        self.metadata(record)?;
        // The registered manager owns the live runtime and log-worker child
        // handles. Let it observe and reap terminal children so a subsequent
        // managed restart can claim the same execution ID. Durable inspection
        // below is only for a control plane that has no in-process owner.
        if let Some(manager) = self.manager(&record.id) {
            return self.inspect_registered(record, manager).await;
        }
        let home_dir = self.home_dir.clone();
        let box_dir = record.box_dir.clone();
        let box_id = record.id.clone();
        let execution_id = execution_id(record)?;
        let state = tokio::task::spawn_blocking(move || {
            inspect_recorded_sandbox(&home_dir, &box_dir, &box_id)
        })
        .await
        .map_err(|error| {
            ExecutionManagerError::Internal(format!(
                "Sandbox inspection task failed for {}: {error}",
                record.id
            ))
        })?
        .map_err(|error| runtime_error("inspect", record, error))?
        .ok_or(ExecutionManagerError::NotFound(execution_id))?;

        match state.status.as_str() {
            "created" | "running" => {
                if state.pid == 0 {
                    return Err(ExecutionManagerError::Internal(format!(
                        "Sandbox runtime returned PID zero for {}",
                        record.id
                    )));
                }
                let manager = self.attach_sandbox(record, state).await?;
                self.inspect_registered(record, manager).await
            }
            "paused" => {
                if state.pid == 0 {
                    return Err(ExecutionManagerError::Internal(format!(
                        "Sandbox runtime returned PID zero for {}",
                        record.id
                    )));
                }
                let manager = self.attach_sandbox(record, state).await?;
                let manager = manager.lock().await;
                let handle = self.handle_from_manager(record, &manager).await?;
                Ok(LocalExecutionObservation {
                    state: ExecutionState::Paused,
                    handle: Some(handle),
                    exit_code: None,
                })
            }
            "stopped" => {
                let exit_code = match crate::rootfs::read_persisted_exit_code(&record.box_dir) {
                    Some(exit_code) => exit_code,
                    None => {
                        self.collect_detached_sandbox_exit_code(record, &state)
                            .await?
                    }
                };
                self.cleanup_detached_sandbox(record).await?;
                Ok(LocalExecutionObservation {
                    state: ExecutionState::Stopped,
                    handle: None,
                    exit_code: Some(exit_code),
                })
            }
            status => Err(ExecutionManagerError::Internal(format!(
                "Sandbox runtime returned unknown state {status} for {}",
                record.id
            ))),
        }
    }

    #[cfg(target_os = "linux")]
    pub(super) async fn pause_sandbox(
        &self,
        record: &BoxRecord,
    ) -> ExecutionManagerResult<LocalExecutionHandle> {
        self.transition_sandbox(record, true).await
    }

    #[cfg(target_os = "linux")]
    pub(super) async fn resume_sandbox(
        &self,
        record: &BoxRecord,
    ) -> ExecutionManagerResult<LocalExecutionHandle> {
        self.transition_sandbox(record, false).await
    }

    #[cfg(not(target_os = "linux"))]
    pub(super) async fn pause_sandbox(
        &self,
        record: &BoxRecord,
    ) -> ExecutionManagerResult<LocalExecutionHandle> {
        Err(unsupported(
            record,
            "pause",
            "the Sandbox backend on this host",
        ))
    }

    #[cfg(not(target_os = "linux"))]
    pub(super) async fn resume_sandbox(
        &self,
        record: &BoxRecord,
    ) -> ExecutionManagerResult<LocalExecutionHandle> {
        Err(unsupported(
            record,
            "resume",
            "the Sandbox backend on this host",
        ))
    }

    #[cfg(target_os = "linux")]
    async fn transition_sandbox(
        &self,
        record: &BoxRecord,
        pause: bool,
    ) -> ExecutionManagerResult<LocalExecutionHandle> {
        self.metadata(record)?;
        let home_dir = self.home_dir.clone();
        let box_dir = record.box_dir.clone();
        let box_id = record.id.clone();
        let operation = if pause { "pause" } else { "resume" };
        let inspection = tokio::task::spawn_blocking(move || {
            let inspection =
                inspect_recorded_sandbox(&home_dir, &box_dir, &box_id)?.ok_or_else(|| {
                    a3s_box_core::BoxError::StateError(format!(
                        "Sandbox runtime record is missing for {box_id}"
                    ))
                })?;
            let socket = inspection.runtime.runtime_socket.as_ref().ok_or_else(|| {
                a3s_box_core::BoxError::StateError(format!(
                    "A3S OCI runtime socket is missing for {box_id}"
                ))
            })?;
            let generation = inspection.runtime.generation.ok_or_else(|| {
                a3s_box_core::BoxError::StateError(format!(
                    "A3S OCI generation is missing for {box_id}"
                ))
            })?;
            if pause {
                crate::sandbox::a3s_oci_handler::A3sOciHandler::pause_at(
                    socket, &box_id, generation,
                )?;
            } else {
                crate::sandbox::a3s_oci_handler::A3sOciHandler::resume_at(
                    socket, &box_id, generation,
                )?;
            }
            inspect_recorded_sandbox(&home_dir, &box_dir, &box_id)?.ok_or_else(|| {
                a3s_box_core::BoxError::StateError(format!(
                    "Sandbox runtime record disappeared after {operation} for {box_id}"
                ))
            })
        })
        .await
        .map_err(|error| {
            ExecutionManagerError::Internal(format!(
                "Sandbox {operation} task failed for {}: {error}",
                record.id
            ))
        })?
        .map_err(|error| runtime_error(operation, record, error))?;

        let expected = if pause { "paused" } else { "running" };
        if inspection.status != expected {
            return Err(ExecutionManagerError::Internal(format!(
                "Sandbox runtime returned state {} after {operation} for {}",
                inspection.status, record.id
            )));
        }
        let manager = self.attach_sandbox(record, inspection).await?;
        let manager = manager.lock().await;
        self.handle_from_manager(record, &manager).await
    }

    #[cfg(target_os = "linux")]
    async fn attach_sandbox(
        &self,
        record: &BoxRecord,
        inspection: SandboxInspection,
    ) -> ExecutionManagerResult<SharedVm> {
        let mut manager = self.new_manager(record)?;
        let socket_dir = crate::vm::runtime_socket_dir(&self.home_dir, &record.id);
        manager.exec_socket_path = Some(socket_dir.join("exec.sock"));
        manager.pty_socket_path = Some(socket_dir.join("pty.sock"));
        manager.port_forward_socket_path = Some(socket_dir.join("portfwd.sock"));
        let runtime_socket = inspection.runtime.runtime_socket.ok_or_else(|| {
            ExecutionManagerError::Internal(format!(
                "A3S OCI runtime socket is missing for {}",
                record.id
            ))
        })?;
        let generation = inspection.runtime.generation.ok_or_else(|| {
            ExecutionManagerError::Internal(format!(
                "A3S OCI generation is missing for {}",
                record.id
            ))
        })?;
        let owner_pid = inspection.runtime.owner_pid.ok_or_else(|| {
            ExecutionManagerError::Internal(format!(
                "A3S OCI owner PID is missing for {}",
                record.id
            ))
        })?;
        let owner_pid_start_time = inspection.runtime.owner_pid_start_time.ok_or_else(|| {
            ExecutionManagerError::Internal(format!(
                "A3S OCI owner identity is missing for {}",
                record.id
            ))
        })?;
        let container_id = a3s_oci_sdk::ContainerId::new(record.id.clone())
            .map_err(|error| ExecutionManagerError::Internal(error.to_string()))?;
        let handler: Box<dyn a3s_box_core::vmm::VmHandler> = Box::new(
            crate::sandbox::a3s_oci_handler::A3sOciHandler::from_recorded_runtime(
                crate::sandbox::a3s_oci_handler::A3sOciHandlerSpec {
                    runtime_socket,
                    runtime_root: inspection.runtime.runtime_root,
                    container_id,
                    generation: a3s_oci_sdk::Generation(generation),
                    init_pid: inspection.pid,
                    owner_pid,
                    owner_pid_start_time,
                    bundle_dir: inspection.runtime.bundle_dir,
                    runtime_record: record.box_dir.join("sandbox/runtime.json"),
                },
                inspection.runtime.log_worker_pid,
                inspection.runtime.log_worker_pid_start_time,
            )
            .await
            .map_err(|error| runtime_error("recover", record, error))?,
        );
        *manager.handler.write().await = Some(handler);
        if !matches!(
            managed_state(record)?,
            ManagedExecutionState::Starting | ManagedExecutionState::RestartStarting
        ) {
            *manager.state.write().await = crate::BoxState::Ready;
        }

        let recovered = Arc::new(Mutex::new(manager));
        match self.managers.entry(record.id.clone()) {
            Entry::Occupied(entry) => Ok(Arc::clone(entry.get())),
            Entry::Vacant(entry) => {
                entry.insert(Arc::clone(&recovered));
                Ok(recovered)
            }
        }
    }

    #[cfg(not(target_os = "linux"))]
    async fn attach_sandbox(
        &self,
        record: &BoxRecord,
        _inspection: SandboxInspection,
    ) -> ExecutionManagerResult<SharedVm> {
        Err(unsupported(
            record,
            "recovery",
            "the Sandbox backend on this host",
        ))
    }

    pub(super) async fn destroy_detached_sandbox(
        &self,
        record: &BoxRecord,
        remove_anonymous_volumes: bool,
        force_preserve_rootfs: bool,
        timeout_secs: Option<u64>,
    ) -> ExecutionManagerResult<LocalExecutionTermination> {
        let observation = self.inspect_sandbox(record).await?;
        if let Some(manager) = self.manager(&record.id) {
            return self
                .destroy_registered(
                    record,
                    manager,
                    remove_anonymous_volumes,
                    force_preserve_rootfs,
                    timeout_secs,
                )
                .await;
        }
        if remove_anonymous_volumes {
            let anonymous_volumes = self.anonymous_volumes_for_record(record).await;
            self.cleanup_anonymous_volumes(anonymous_volumes).await;
        }
        Ok(LocalExecutionTermination {
            outcome: KillOutcome::Killed,
            exit_code: observation.exit_code,
        })
    }

    #[cfg(target_os = "linux")]
    async fn collect_detached_sandbox_exit_code(
        &self,
        record: &BoxRecord,
        state: &SandboxInspection,
    ) -> ExecutionManagerResult<i32> {
        let runtime_socket = state.runtime.runtime_socket.clone().ok_or_else(|| {
            ExecutionManagerError::Internal(format!(
                "A3S OCI runtime socket is missing for {}",
                record.id
            ))
        })?;
        let generation = state.runtime.generation.ok_or_else(|| {
            ExecutionManagerError::Internal(format!(
                "A3S OCI generation is missing for {}",
                record.id
            ))
        })?;
        let box_id = record.id.clone();
        let exit_code =
            tokio::task::spawn_blocking(move || -> a3s_box_core::Result<Option<i32>> {
                let deadline = std::time::Instant::now() + TERMINAL_EXIT_POLL_TIMEOUT;
                loop {
                    if let Some(exit_code) = crate::sandbox::A3sOciHandler::try_wait_at(
                        &runtime_socket,
                        &box_id,
                        generation,
                    )? {
                        return Ok(Some(exit_code));
                    }
                    if std::time::Instant::now() >= deadline {
                        return Ok(None);
                    }
                    std::thread::sleep(TERMINAL_EXIT_POLL_INTERVAL);
                }
            })
            .await
            .map_err(|error| {
                ExecutionManagerError::Unavailable(format!(
                    "Sandbox exit-status task failed for {}: {error}",
                    record.id
                ))
            })?
            .map_err(|error| {
                ExecutionManagerError::Unavailable(format!(
                    "failed to collect exact Sandbox exit status for {}: {error}",
                    record.id
                ))
            })?;
        exit_code.ok_or_else(|| {
            ExecutionManagerError::Unavailable(format!(
                "Sandbox reported execution {} as terminal before its exact exit status became available",
                record.id
            ))
        })
    }

    #[cfg(not(target_os = "linux"))]
    async fn collect_detached_sandbox_exit_code(
        &self,
        record: &BoxRecord,
        _state: &SandboxInspection,
    ) -> ExecutionManagerResult<i32> {
        Err(unsupported(
            record,
            "exit-status recovery",
            "the Sandbox backend on this host",
        ))
    }

    async fn cleanup_detached_sandbox(&self, record: &BoxRecord) -> ExecutionManagerResult<()> {
        let home_dir = self.home_dir.clone();
        let box_dir = record.box_dir.clone();
        let box_id = record.id.clone();
        tokio::task::spawn_blocking(move || {
            crate::vm::reap::cleanup_recorded_sandbox_runtime_in(&home_dir, &box_dir, &box_id)
        })
        .await
        .map_err(|error| {
            ExecutionManagerError::Internal(format!(
                "Sandbox cleanup task failed for {}: {error}",
                record.id
            ))
        })?
        .map_err(|error| runtime_error("kill", record, error))?;

        let mut manager = self.new_manager(record)?;
        if should_force_rootfs_preservation(record)? {
            manager
                .destroy_preserving_rootfs()
                .await
                .map_err(|error| runtime_error("clean up", record, error))?;
        } else {
            manager
                .destroy()
                .await
                .map_err(|error| runtime_error("clean up", record, error))?;
        }
        Ok(())
    }
}

#[cfg(target_os = "linux")]
struct SandboxInspection {
    status: String,
    pid: u32,
    runtime: crate::vm::reap::RecordedSandboxRuntime,
}

#[cfg(target_os = "linux")]
fn inspect_recorded_sandbox(
    home_dir: &Path,
    box_dir: &Path,
    box_id: &str,
) -> a3s_box_core::Result<Option<SandboxInspection>> {
    let Some(runtime) = crate::vm::reap::load_recorded_sandbox_runtime(home_dir, box_dir, box_id)?
    else {
        return Ok(None);
    };
    let socket = runtime.runtime_socket.as_ref().ok_or_else(|| {
        a3s_box_core::BoxError::StateError(format!(
            "A3S OCI runtime socket is missing for {box_id}"
        ))
    })?;
    let generation = runtime.generation.ok_or_else(|| {
        a3s_box_core::BoxError::StateError(format!("A3S OCI generation is missing for {box_id}"))
    })?;
    let (status, pid) = match crate::sandbox::a3s_oci_handler::A3sOciHandler::query_state_at(
        socket, box_id, generation,
    )? {
        Some(state) => (state.status, state.pid),
        None => ("stopped".to_string(), 0),
    };
    if matches!(status.as_str(), "created" | "running" | "paused") && pid != runtime.init_pid {
        return Err(a3s_box_core::BoxError::StateError(format!(
            "Sandbox runtime PID disagrees with its durable record for {box_id}"
        )));
    }
    Ok(Some(SandboxInspection {
        status,
        pid,
        runtime,
    }))
}

#[cfg(not(target_os = "linux"))]
struct SandboxInspection {
    status: String,
    pid: u32,
}

#[cfg(not(target_os = "linux"))]
fn inspect_recorded_sandbox(
    _home_dir: &Path,
    _box_dir: &Path,
    _box_id: &str,
) -> a3s_box_core::Result<Option<SandboxInspection>> {
    Err(a3s_box_core::BoxError::StateError(
        "Sandbox execution requires Linux".to_string(),
    ))
}