Skip to content

feat(krun): add exit signal handlers, exit handle, and custom console backends#44

Merged
appcypher merged 2 commits into
krunfrom
appcypher/add-exit-signal-handlers
Mar 26, 2026
Merged

feat(krun): add exit signal handlers, exit handle, and custom console backends#44
appcypher merged 2 commits into
krunfrom
appcypher/add-exit-signal-handlers

Conversation

@appcypher
Copy link
Copy Markdown
Member

Summary

  • Add cross-platform SIGTERM/SIGUSR1 signal handlers that trigger graceful VMM shutdown through the exit event fd, ensuring exit observers run before process termination
  • Add ExitHandle type for triggering VM exit programmatically from any thread (cloneable, thread-safe, async-signal-safe)
  • Propagate the VM exit code through the observer chain so on_exit callbacks receive the i32 exit code, and expose Vm::exit_code() as a shared Arc<AtomicI32>
  • Add ConsolePortBackend trait and PortConfig::Custom variant for custom in-process console port I/O without file descriptors on the data path

Changes

  • New src/vmm/src/exit_signal.rs: POSIX sigaction-based handlers for SIGTERM and SIGUSR1 that write to the VMM exit event fd, working on both Linux and macOS
  • New src/krun/src/api/exit_handle.rs: ExitHandle struct that dups the exit event fd write end and exposes a trigger() method, with Clone/Send/Sync implementations
  • New src/krun/src/backends/console.rs: Re-exports ConsolePortBackend from the devices crate
  • Modified src/devices/src/virtio/console/port_io.rs: Added ConsolePortBackend trait with read/write/read_wake_fd methods, plus ConsolePortBackendInputAdapter and ConsolePortBackendOutputAdapter that bridge the trait to PortInput/PortOutput
  • Modified src/devices/src/virtio/device.rs: Changed VmmExitObserver::on_vmm_exit() and its blanket Fn impl to accept exit_code: i32
  • Modified src/krun/src/api/builder.rs: on_exit callback signature changed from Fn() to Fn(i32), pre-creates exit_evt and exit_code and passes them to Vm::new()
  • Modified src/krun/src/api/builders.rs: Added ConsoleBuilder::custom() method that wraps a ConsolePortBackend into PortConfig::Custom
  • Modified src/krun/src/api/vm.rs: Added Vm::exit_handle() and Vm::exit_code() public methods; updated enter() to pass exit_evt and exit_code to build_microvm
  • Modified src/vmm/src/builder.rs: build_microvm now accepts pre-created exit_evt: EventFd and exit_code: Arc<AtomicI32> instead of creating them internally; registers exit signal handlers; added PortConfig::Custom arm in create_explicit_ports; attach_console_devices takes owned VirtioConsoleConfigMode instead of a reference
  • Modified src/vmm/src/lib.rs: notify_exit_observers and stop propagate exit_code: i32
  • Modified src/vmm/src/resources.rs: Added PortConfig::Custom variant with boxed PortInput/PortOutput; removed Debug/Clone derives from PortConfig
  • Modified src/libkrun/src/lib.rs: Updated krun_start_enter to create and pass exit_evt/exit_code to build_microvm
  • Updated examples/rust_vm/src/main.rs: on_exit callback now receives and prints the exit code

Test Plan

  • Run cargo build to verify compilation succeeds across all crates
  • Run cargo test to verify existing unit tests pass (including updated Vm::new() signatures in vm.rs tests)
  • Verify the rust_vm example compiles: cargo build -p rust_vm
  • Test graceful shutdown by sending SIGTERM to a running VM and confirming exit observers fire
  • Test ExitHandle::trigger() from a background thread to verify programmatic shutdown works

… backends

Add cross-platform SIGTERM/SIGUSR1 signal handlers that trigger
graceful VMM shutdown through the exit event fd, ensuring exit
observers run before process termination.

- Add ExitHandle type for triggering VM exit from any thread
  (cloneable, thread-safe, async-signal-safe)
- Add exit_signal module with POSIX sigaction-based handlers
  that work on both Linux and macOS
- Propagate exit code through the observer chain: on_exit
  callbacks now receive the i32 exit code, and Vm exposes
  exit_code() as a shared Arc<AtomicI32>
- Add ConsolePortBackend trait for custom in-process console
  port I/O without file descriptors on the data path
- Add PortConfig::Custom variant with input/output adapters
  for ConsolePortBackend
- Move exit EventFd and exit code creation to callers
  (VmBuilder/libkrun) so they can be shared before VM entry
- Update build_microvm signature to accept pre-created exit_evt
  and exit_code parameters
@appcypher appcypher changed the base branch from main to krun March 26, 2026 13:38
- Fix alphabetical ordering of re-exports in krun/lib.rs and module
  declarations in vmm/lib.rs to satisfy cargo fmt
- Cast signal handler through *const () before usize to fix
  -D function-casts-as-integer lint error
@appcypher appcypher merged commit 019720f into krun Mar 26, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant