Skip to content

Commit 38cc6bb

Browse files
authored
fix: gate unsafe set_var behind cfg(windows) in pyhl (#57)
Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent 86d2609 commit 38cc6bb

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

host/src/pyhl.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,28 @@ use std::time::Instant;
4848

4949
use crate::{Preopen, Sandbox};
5050

51-
/// Default Hyperlight's surrogate-process pool to 1 on Windows.
51+
/// Set `HYPERLIGHT_INITIAL_SURROGATES=1` if unset (Windows only).
5252
///
53-
/// The surrogate-process manager pre-spawns `HYPERLIGHT_INITIAL_SURROGATES`
54-
/// Windows processes (default 512) the first time any sandbox is created.
55-
/// At ~7 ms per `CreateProcessA` that is ~3.5 s of latency that library
56-
/// callers would pay on the very first sandbox. Pinning the default to 1
57-
/// drops that to ~7 ms. Callers (or the embedding binary) can still
58-
/// override by setting the env var before calling into `pyhl`.
53+
/// On Windows, Hyperlight pre-spawns surrogate processes (one per
54+
/// potential sandbox). The default count is 512, each costing ~7 ms
55+
/// of `CreateProcessA` latency. For pyhl's single-sandbox usage,
56+
/// pinning to 1 avoids ~3.5 s of wasted startup.
57+
///
58+
/// # Safety
59+
///
60+
/// Must be called from the main thread before spawning any additional
61+
/// threads or creating any `Sandbox`. Violating this is undefined
62+
/// behavior due to `std::env::set_var` not being thread-safe.
5963
pub fn default_surrogate_count() {
60-
if std::env::var_os("HYPERLIGHT_INITIAL_SURROGATES").is_none() {
61-
// Safety: must be called before any sandbox is created and
62-
// before additional threads are spawned.
63-
unsafe {
64-
std::env::set_var("HYPERLIGHT_INITIAL_SURROGATES", "1");
64+
#[cfg(target_os = "windows")]
65+
{
66+
if std::env::var_os("HYPERLIGHT_INITIAL_SURROGATES").is_none() {
67+
// SAFETY: Called from main thread before any sandbox or
68+
// thread pool is created. The binary entry points (cmd_run,
69+
// cmd_setup) call this first.
70+
unsafe {
71+
std::env::set_var("HYPERLIGHT_INITIAL_SURROGATES", "1");
72+
}
6573
}
6674
}
6775
}

0 commit comments

Comments
 (0)