Skip to content

Add Sandbox::recreate to recover unrecoverable sandboxes - #1751

Open
ludfjig wants to merge 1 commit into
hyperlight-dev:mainfrom
ludfjig:recover_unrecoverable
Open

ludfjig wants to merge 1 commit into
hyperlight-dev:mainfrom
ludfjig:recover_unrecoverable

Conversation

@ludfjig

@ludfjig ludfjig commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Adds Sandbox::recreate(&mut self, snapshot), which replaces a sandbox's VM and memory with new ones built from snapshot. It recovers sandboxes left Unrecoverable by a failed restore.

  • The sandbox keeps its configuration, host functions, page table root finder, guest log level, and interrupt handles.
  • Existing interrupt handles control the new VM. Windows rebinds the handle to the new partition.
  • The new VM is built before the old one is dropped. A failed recreate leaves the sandbox unchanged, so it can be retried.
  • Like restore(), recreation removes regions added with map_region() or map_file_cow().
  • Sandbox stores its configuration. It replaces the separate guest log level field.

On Windows the new VM needs a surrogate process, as when creating any sandbox. Recreation fails with HYPERLIGHT_MAX_SURROGATES=0 and blocks while all surrogates are in use. The rustdoc documents this.

Closes #1747

@ludfjig

ludfjig commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

@syntactically first draft at #1747

@ludfjig ludfjig added the kind/enhancement For PRs adding features, improving functionality, docs, tests, etc. label Aug 21, 2026
@ludfjig ludfjig changed the title Add method for recreate unrecoverable sandbox Add method for recreating unrecoverable sandbox Aug 21, 2026
@ludfjig
ludfjig marked this pull request as ready for review August 21, 2026 17:06
Copilot AI lite review requested due to automatic review settings August 21, 2026 17:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a public MultiUseSandbox::recreate() API in hyperlight-host to rebuild an unrecoverable sandbox from scratch using its existing configuration and the snapshot that triggered the unrecoverable restore failure (addressing #1747’s “replace this sandbox with a new one with the same configuration” request).

Changes:

  • Track additional lifecycle state needed for recreation (store the failed-restore snapshot and sandbox config when transitioning to Unrecoverable).
  • Add MultiUseSandbox::recreate(self) -> Result<Self> plus tests validating what state/config is preserved vs not preserved.
  • Update errors, changelog, and test recipes to cover the new recreation flow.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/hyperlight_host/src/sandbox/uninitialized_evolve.rs Thread sandbox configuration into MultiUseSandbox::from_uninit so it can be reused for recreation.
src/hyperlight_host/src/sandbox/initialized_multi_use.rs Introduce SandboxLifecycle, store recreation snapshot/config, and implement MultiUseSandbox::recreate() with accompanying tests.
src/hyperlight_host/src/sandbox/host_funcs.rs Add an internal constructor to rebuild HostFunctions from a FunctionRegistry during recreation.
src/hyperlight_host/src/error.rs Add recreation-specific error variants and adjust unrecoverable messaging.
Justfile Ensure the new unrecoverable-mapping test is exercised in crashdump/coverage recipes.
CHANGELOG.md Document the new MultiUseSandbox::recreate() API.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/hyperlight_host/src/error.rs Outdated
Comment thread src/hyperlight_host/src/sandbox/initialized_multi_use.rs Outdated
@ludfjig
ludfjig force-pushed the recover_unrecoverable branch from 9e79d10 to f806823 Compare August 21, 2026 20:21

@syntactically syntactically left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few comments inline.

I'm still trying to work out what the best API here is. It feels to me like it is a smaller/less breaking change to move this into restore() and add a new way to opt-out of the implicit recreate, but the possibility of recreation itself failing (which is probably pretty likely if we've recently failed to restore, since the relevant mapping failures seem likely to be resource exhaustion) is a little difficult to handle ergonomically. It seems inescapable to me that client code, if it is going to handle this locally, will need to do something like

while should_keep_trying() {
  match sb.<restore/recreate/whatever>(snapshot) {
    Ok(()) => break,
    Err(HyperlightError::UnrecoverableSandbox) => {
      // maybe try to free up resources somehow, try a different snapshot, etc
      continue;
    },
    Err(e) => return e,
}

which is just kind of nasty, whether you have one function for restore/recreate or multiple.

(The other option for clients I guess would be to just back out all the way to some top level retry when they get an unrecoverable error. That probably doesn't benefit at all from having recreate functionality at all, though...)

self.0
}

#[cfg(not(gdb))]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is so much stuff not(gdb) guarded?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

/// again on the rebuilt sandbox.
///
/// Existing interrupt handles do not apply to the rebuilt sandbox. Call
/// [`interrupt_handle()`](Self::interrupt_handle) on the rebuilt sandbox to

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a nasty footgun. Is there really no way to get around this? I guess we should just need to update the partition handle here on the WHP sandboxes? I would expect the linux/hvf ones might just work, since the tid update happens just before run? If the latter is true, maybe we can just check/update the partition id on the whp sandboxes before run so that the lifecycle is the same?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! I didn't realize it would be possible

if let Err(error) = self.restore_memory_and_mappings(&snapshot) {
self.status = SandboxStatus::Unrecoverable;
self.lifecycle = SandboxLifecycle::Unrecoverable {
recreation_snapshot: snapshot,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to save the snapshot here rather than in self.snapshot?

@ludfjig ludfjig Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed so recreate now takes snapshot as parameter

Poisoned,
Unrecoverable {
#[cfg_attr(gdb, allow(dead_code))]
recreation_snapshot: Arc<Snapshot>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with the current lifecycle of MultiUseSandbox::snapshot, this means we can only ever transition into the unrecoverable state from restore(), since we are not guaranteed to have a valid snapshot around otherwise? That doesn't seem right to me---surely we should be able to end up in unrecoverable from e.g. map_region as well, even if we don't have a current valid snapshot? Like with poisoned, I would expect that the new snapshot should be supplied on the recreate operation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, recreate now takes snapshot as param, just like restore

@ludfjig
ludfjig force-pushed the recover_unrecoverable branch from f806823 to d37d029 Compare October 19, 2026 20:54
@ludfjig ludfjig changed the title Add method for recreating unrecoverable sandbox Add Sandbox::recreate to recover unrecoverable sandboxes Oct 19, 2026
@hyperlight-gh-bot

This comment has been minimized.

@hyperlight-gh-bot

This comment has been minimized.

recreate() replaces a sandbox's VM and memory with new ones built
from a snapshot. It recovers unrecoverable sandboxes. A failed
recreate leaves the sandbox unchanged, so it can be retried.

The sandbox keeps its configuration, host functions, page table root
finder, guest log level, and interrupt handles. Existing interrupt
handles control the new VM.

Sandbox stores its configuration, which replaces the separate guest
log level field.

Closes hyperlight-dev#1747

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@ludfjig
ludfjig force-pushed the recover_unrecoverable branch from a3f0ebe to 6d37d09 Compare October 21, 2026 23:18
@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: 6d37d0952000
Baseline commit: da688e9a114a

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 753.74 ns (➖ 1.01x slower)
vec_bytes 579.03 ns (➖ 1.14x faster)
377.45 µs (➖ 1.01x slower)

payload_allocation

slot_pool_segmented
262144 523.64 ns (➖ 1.01x faster)
65536 142.14 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 82.00 ms (➖ 1.02x slower)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
7.73 ns (➖ 1.01x faster) 7.74 ns (➖ 1.00x slower) 7.78 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 93.13 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 7.04 µs (➖ 1.02x faster) 7.00 µs (➖ 1.00x faster)
65536 2.05 µs (➖ 1.06x slower) 1.91 µs (➖ 1.10x faster)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 6.16 µs (➖ 1.08x faster) 6.15 µs (➖ 1.04x faster)
8192 1.07 µs (➖ 1.02x slower) 1.05 µs (➖ 1.01x slower)
262144 27.57 µs (➖ 1.01x slower)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 688.43 ns (➖ 1.17x faster)
vec_bytes 532.04 ns (➖ 1.11x faster)
694.42 µs (➖ 1.04x faster)

payload_allocation

slot_pool_segmented
262144 574.01 ns (➖ 1.01x faster)
65536 153.20 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 80.68 ms (➖ 1.03x faster)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
7.81 ns (➖ 1.04x slower) 7.83 ns (➖ 1.04x slower) 7.79 ns (➖ 1.04x slower)

snapshot_files

load_snapshot_unverified
small 47.29 µs (➖ 1.03x faster)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 8.34 µs (➖ 1.06x slower) 8.24 µs (➖ 1.02x slower)
65536 2.32 µs (➖ 1.04x slower) 2.37 µs (➖ 1.05x slower)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 7.80 µs (➖ 1.02x slower) 7.72 µs (➖ 1.02x faster)
8192 819.34 ns (➖ 1.03x slower) 820.08 ns (➖ 1.05x slower)
262144 31.96 µs (➖ 1.02x faster)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 922.11 ns (➖ 1.01x faster)
vec_bytes 710.66 ns (➖ 1.00x slower)
269.28 µs (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
262144 729.35 ns (➖ 1.12x faster)
65536 195.44 ns (➖ 1.11x faster)

sandboxes

create_initialized_and_drop
medium 58.72 ms (➖ 1.01x faster)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
9.81 ns (➖ 1.04x faster) 9.78 ns (➖ 1.04x faster) 9.77 ns (➖ 1.05x faster)

snapshot_files

load_snapshot_unverified
small 83.30 µs (➖ 1.02x faster)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 9.76 µs (➖ 1.01x faster) 9.89 µs (➖ 1.02x slower)
65536 2.38 µs (➖ 1.04x faster) 2.25 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 9.83 µs (➖ 1.09x faster) 8.25 µs (➖ 1.16x faster)
8192 1.26 µs (➖ 1.09x faster) 1.26 µs (➖ 1.01x slower)
262144 35.75 µs (➖ 1.05x faster)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 905.72 ns (➖ 1.03x faster)
vec_bytes 653.18 ns (➖ 1.00x faster)
651.76 µs (➖ 1.02x faster)

payload_allocation

slot_pool_segmented
262144 643.35 ns (➖ 1.02x slower)
65536 172.98 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 64.52 ms (➖ 1.11x faster)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
8.82 ns (➖ 1.00x slower) 8.84 ns (➖ 1.02x faster) 8.76 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 45.92 µs (➖ 1.04x slower)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 7.99 µs (➖ 1.00x faster) 7.89 µs (➖ 1.01x faster)
65536 2.30 µs (➖ 1.01x slower) 2.31 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 7.47 µs (➖ 1.01x faster) 7.43 µs (➖ 1.00x slower)
8192 918.19 ns (➖ 1.02x faster) 909.25 ns (➖ 1.02x faster)
262144 39.50 µs (➖ 1.02x slower)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 1.17 µs (➖ 1.01x slower)
vec_bytes 774.19 ns (➖ 1.01x faster)
1.87 ms (➖ 1.03x faster)

payload_allocation

slot_pool_segmented
262144 810.24 ns (➖ 1.02x slower)
65536 231.60 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 84.54 ms (➖ 1.08x slower)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
9.86 ns (➖ 1.00x faster) 10.12 ns (➖ 1.00x slower) 10.08 ns (➖ 1.05x faster)

snapshot_files

load_snapshot_unverified
small 680.77 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 9.20 µs (➖ 1.01x faster) 10.09 µs (➖ 1.09x slower)
65536 2.36 µs (➖ 1.01x faster) 2.52 µs (➖ 1.07x slower)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 9.11 µs (➖ 1.00x faster) 9.86 µs (➖ 1.07x slower)
8192 1.56 µs (➖ 1.07x slower) 1.28 µs (➖ 1.06x faster)
262144 39.51 µs (➖ 1.31x faster)
hyperv-ws2025 / intel (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 1.20 µs (➖ 1.01x faster)
vec_bytes 763.01 ns (➖ 1.02x faster)
2.89 ms (➖ 1.02x faster)

payload_allocation

slot_pool_segmented
262144 738.68 ns (➖ 1.02x faster)
65536 214.46 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 102.64 ms (➖ 1.01x faster)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
10.45 ns (➖ 1.00x slower) 10.08 ns (➖ 1.01x slower) 10.09 ns (➖ 1.01x slower)

snapshot_files

load_snapshot_unverified
small 663.47 µs (➖ 1.17x slower)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 7.75 µs (➖ 1.00x slower) 7.67 µs (➖ 1.02x faster)
65536 2.37 µs (➖ 1.12x faster) 2.32 µs (➖ 1.02x faster)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 8.67 µs (➖ 1.02x slower) 8.72 µs (➖ 1.07x faster)
8192 1.58 µs (➖ 1.11x slower) 1.15 µs (➖ 1.06x slower)
262144 42.49 µs (➖ 1.23x faster)

Reported by cargo ci bench-report --candidate run:39692578660 --config-file bench_report.toml.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement For PRs adding features, improving functionality, docs, tests, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add API to "replace this sandbox with a new one with the same configuration"

3 participants