Skip to content

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

Open
ludfjig wants to merge 2 commits into
hyperlight-dev:mainfrom
ludfjig:recover_unrecoverable
Open

ludfjig wants to merge 2 commits 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

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 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.

borsh-derive still depended on syn 3.0.6 after the syn bump to 3.0.8
removed that version from the lock file. The two dependency bumps
merged separately, which left Cargo.lock out of date and made
`cargo fetch --locked` fail.

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: a3f0ebebb7af
Baseline commit: c58dc9407dbe

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 761.43 ns (➖ 1.02x faster)
vec_bytes 603.79 ns (➖ 1.01x slower)
371.97 µs (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
262144 523.80 ns (➖ 1.02x faster)
65536 141.51 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 81.47 ms (➖ 1.02x faster)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
7.71 ns (➖ 1.00x faster) 7.72 ns (➖ 1.00x faster) 7.79 ns (➖ 1.01x slower)

snapshot_files

load_snapshot_unverified
small 92.88 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 7.25 µs (➖ 1.00x slower) 7.16 µs (➖ 1.00x slower)
65536 2.06 µs (➖ 1.06x slower) 1.93 µs (➖ 1.09x faster)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 6.29 µs (➖ 1.02x faster) 6.28 µs (➖ 1.05x faster)
8192 1.06 µs (➖ 1.01x slower) 1.05 µs (➖ 1.01x faster)
262144 27.27 µs (➖ 1.02x faster)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 740.71 ns (➖ 1.02x slower)
vec_bytes 581.75 ns (➖ 1.06x slower)
660.30 µs (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
262144 590.85 ns (➖ 1.04x slower)
65536 145.78 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 80.26 ms (➖ 1.08x faster)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
7.49 ns (➖ 1.04x faster) 7.57 ns (➖ 1.02x faster) 7.63 ns (➖ 1.03x faster)

snapshot_files

load_snapshot_unverified
small 47.80 µs (➖ 1.03x slower)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 8.18 µs (➖ 1.01x slower) 8.17 µs (➖ 1.01x slower)
65536 2.29 µs (➖ 1.00x slower) 2.28 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 7.85 µs (➖ 1.02x faster) 7.74 µs (➖ 1.04x faster)
8192 836.33 ns (➖ 1.00x faster) 830.11 ns (➖ 1.03x slower)
262144 32.68 µs (➖ 1.02x slower)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 918.43 ns (➖ 1.01x faster)
vec_bytes 713.60 ns (➖ 1.02x slower)
314.76 µs (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
262144 676.40 ns (➖ 1.04x faster)
65536 190.75 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 64.12 ms (➖ 1.04x slower)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
9.98 ns (➖ 1.00x slower) 9.93 ns (➖ 1.00x faster) 9.95 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 85.51 µs (➖ 1.00x slower)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 10.57 µs (➖ 1.14x slower) 9.48 µs (➖ 1.00x faster)
65536 2.67 µs (➖ 1.01x faster) 2.48 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 8.05 µs (➖ 1.02x faster) 8.05 µs (➖ 1.03x faster)
8192 1.46 µs (➖ 1.07x slower) 1.37 µs (➖ 1.02x slower)
262144 35.72 µs (➖ 1.00x slower)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 922.54 ns (➖ 1.02x slower)
vec_bytes 656.39 ns (➖ 1.00x faster)
730.93 µs (➖ 1.04x slower)

payload_allocation

slot_pool_segmented
262144 659.22 ns (➖ 1.01x slower)
65536 185.66 ns (➖ 1.08x slower)

sandboxes

create_initialized_and_drop
medium 69.08 ms (➖ 1.06x faster)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
9.70 ns (➖ 1.01x slower) 9.42 ns (➖ 1.01x slower) 9.52 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 44.42 µs (➖ 1.00x faster)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 7.91 µs (➖ 1.00x faster) 7.95 µs (➖ 1.00x slower)
65536 2.28 µs (➖ 1.01x faster) 2.27 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 7.64 µs (➖ 1.01x slower) 7.49 µs (➖ 1.03x faster)
8192 936.28 ns (➖ 1.02x faster) 927.98 ns (➖ 1.01x slower)
262144 41.18 µs (➖ 1.01x 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.16 µs (➖ 1.03x slower)
vec_bytes 763.98 ns (➖ 1.01x faster)
1.86 ms (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
262144 791.53 ns (➖ 1.01x slower)
65536 233.55 ns (➖ 1.01x slower)

sandboxes

create_initialized_and_drop
medium 82.71 ms (➖ 1.01x slower)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
10.91 ns (➖ 1.09x faster) 10.92 ns (➖ 1.01x faster) 10.86 ns (➖ 1.02x faster)

snapshot_files

load_snapshot_unverified
small 539.38 µs (➖ 1.20x faster)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 8.66 µs (➖ 1.01x slower) 8.78 µs (➖ 1.00x faster)
65536 2.54 µs (➖ 1.01x faster) 2.53 µs (➖ 1.00x faster)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 8.74 µs (➖ 1.01x faster) 8.97 µs (➖ 1.01x slower)
8192 1.44 µs (➖ 1.05x faster) 1.45 µs (➖ 1.04x slower)
262144 37.90 µs (➖ 1.02x 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.22 µs (➖ 1.02x slower)
vec_bytes 786.31 ns (➖ 1.03x faster)
3.05 ms (➖ 1.04x faster)

payload_allocation

slot_pool_segmented
262144 731.08 ns (➖ 1.08x faster)
65536 209.74 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 115.42 ms (➖ 1.02x faster)

slot_pool

alloc_dealloc_1500 alloc_dealloc_4096 alloc_dealloc_128
10.16 ns (➖ 1.00x slower) 10.04 ns (➖ 1.01x faster) 9.98 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 737.69 µs (➖ 1.19x slower)

virtq_readonly

slot_pool_segmented_fragmented slot_pool_segmented
262144 7.87 µs (➖ 1.02x slower) 7.81 µs (➖ 1.00x slower)
65536 2.47 µs (➖ 1.07x slower) 2.35 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented_fragmented slot_pool_segmented
65536 8.47 µs (➖ 1.02x faster) 9.55 µs (➖ 1.12x slower)
8192 1.15 µs (➖ 1.11x faster) 1.12 µs (➖ 1.00x faster)
262144 45.12 µs (➖ 1.02x slower)

Reported by cargo ci bench-report --candidate run:39336074689 --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