boxlite-ai / boxlite-ai/boxlite
Improve box recovery after host reboot
- Dominant language
- Rust
- Stars
- 2.3k
- Forks
- 179
- Avg merge
- 23h 25m
- Merged PRs (30d)
- 121
Description
## Summary
Improve box recovery mechanism when host system reboots, following Podman's proven refresh pattern.
## Current Behavior
- Boxes marked as Running/Starting may have dead processes after reboot
- Recovery relies on PID polling via `kill(pid, 0)` which may be delayed
- No explicit detection of system reboot
## Proposed Improvements
### 1. Alive File Detection
Detect reboot by checking for an "alive" marker file:
```rust
// On runtime init
fn init(&self) -> BoxliteResult<()> {
let alive_path = self.layout.home_dir().join(".alive");
if !alive_path.exists() {
// Alive file missing = system rebooted
// All "Running" boxes are now dead
self.refresh_after_reboot()?;
}
// Create/touch alive file
fs::write(&alive_path, "")?;
Ok(())
}
```
### 2. Refresh Logic
Reset transient state for all boxes after reboot:
```rust
fn refresh_after_reboot(&self) -> BoxliteResult<()> {
tracing::info!("System reboot detected, refreshing box states");
for box_ in self.state.all_boxes()? {
if box_.state.state.is_active() {
// Process is dead after reboot
box_.state.state = BoxState::Stopped;
box_.state.pid = None;
self.state.save_state(&box_.config.id, &box_.state)?;
tracing::info!(box_id = %box_.config.id, "Marked as stopped after reboot");
}
// Recreate runtime directories (tmpfs was cleared)
self.recreate_runtime_dirs(&box_)?;
}
Ok(())
}
```
### 3. Auto-cleanup
Handle boxes that should have been auto-removed before reboot but weren't.
### 4. State Reset
Clear ephemeral data from database:
- Exec sessions
- Temporary mount info
- Network status
## Benefits
- Immediate state consistency on startup
- No delay waiting for PID polling
- Clean separation of reboot vs normal startup
- Follows battle-tested Podman pattern
## References
- Podman refresh mechanism: https://github.com/containers/podman/blob/main/libpod/runtime.go
## Tasks
- [ ] Add alive file detection
- [ ] Implement refresh_after_reboot()
- [ ] Clear ephemeral state on reboot
- [ ] Handle auto-remove boxes
- [ ] Add tests
- [ ] Document recovery behavior
Contributor guide
Research direction
Start at the runtime init entry point and inspect the state APIs referenced by all_boxes() and save_state(). Compare the proposed recovery flow with Podman's refresh mechanism, then add tests covering alive-file detection, state reset, runtime-directory recreation, and auto-removal; done means rebooted boxes have consistent stopped state and ephemeral data is cleared.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- infrastructure
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100