Skip to content

Commit 3fe14b7

Browse files
authored
Merge pull request #2233 from microsoft/agents/issue-2232-run-foreground-timing
nvx: name the timing bounds of the run command's foreground
2 parents d1504b0 + e1a4b2c commit 3fe14b7

1 file changed

Lines changed: 45 additions & 15 deletions

File tree

‎nvx/src/host/cli/run.rs‎

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use crate::host::filesystem::{
7676
self, ExportPlan, ExportRequest, Mapping, Owner, PathList, ReservedTargets, Target,
7777
};
7878
use crate::host::platform::{self, Transport};
79+
use crate::host::process::KILL_TIMEOUT;
7980
use crate::host::runtime::{ArtifactIdentity, LaunchOptions, Runtime, RuntimeIdentity};
8081
use crate::host::sandbox::mount::{
8182
MountAccess, MountKind, MountOwner, POLICY_KINDS, PolicyKind, mount_kind, parse_mount_value,
@@ -183,6 +184,35 @@ const PROCESSORS: [&str; 4] = ["1", "2", "4", "8"];
183184
/// The name of a cold run's temporary VM directory, before 32 random lowercase hexadecimal digits.
184185
const RUN_DIRECTORY: &str = "nvx-run-";
185186

187+
/// The longest that the foreground waits for console output, and its pause after a check of the
188+
/// console endpoint, the attachment, or the VM's output that found no progress: about 100 checks
189+
/// a second.
190+
const FOREGROUND_POLL: Duration = Duration::from_millis(10);
191+
/// The longest that one attempt to connect to the VM console's named pipe blocks before the
192+
/// foreground checks again whether OpenVMM has exited.
193+
#[cfg(not(target_os = "linux"))]
194+
const CONSOLE_ATTEMPT_TIMEOUT: Duration = Duration::from_millis(250);
195+
/// The longest that a cold run waits for OpenVMM to serve its console endpoint.
196+
const CONSOLE_TIMEOUT: Duration = Duration::from_secs(60);
197+
/// The longest wait for OpenVMM's exit status after a console connection fails for good: peer
198+
/// authentication may observe OpenVMM's exit just before its status becomes available.
199+
const PEER_EXIT_WAIT: Duration = Duration::from_millis(250);
200+
/// The longest that a managed cold run waits for each phase of its attachment to the VM's control
201+
/// session: first for the control endpoint, then for the attachment handshake over it.
202+
const ATTACH_TIMEOUT: Duration = Duration::from_secs(60);
203+
/// The longest that OpenVMM has to exit after its control endpoint closed during the
204+
/// attachment, before the run reports the closed endpoint.
205+
const ATTACH_CLOSED_GRACE: Duration = Duration::from_secs(60);
206+
/// The longest that the foreground keeps forwarding boot output that still arrives after a
207+
/// failed attachment, before it reports the failure.
208+
const ATTACH_FAILURE_DRAIN: Duration = Duration::from_millis(250);
209+
/// The longest that a stop waits for the guest to stop through the control session before it
210+
/// terminates OpenVMM.
211+
const STOP_GRACE: Duration = Duration::from_millis(250);
212+
/// The bytes that one read takes from the standard input, the VM log, or the console: 8 KiB, the
213+
/// size of the buffer of `std::io::copy`.
214+
const TRANSFER_BUFFER_LEN: usize = 8192;
215+
186216
/// The options of `snapshot restore` that a restore of `run` takes, which `sandbox` defines and
187217
/// parses for both: the sandbox's state directory, the image, its upper, the workload, and the
188218
/// timeout of the restore.
@@ -761,7 +791,7 @@ fn connect_running(
761791
#[cfg(target_os = "linux")]
762792
let connect = || platform::connect_child_endpoint(endpoint, pid);
763793
#[cfg(not(target_os = "linux"))]
764-
let connect = || platform::connect_endpoint(endpoint, pid, Duration::from_millis(250));
794+
let connect = || platform::connect_endpoint(endpoint, pid, CONSOLE_ATTEMPT_TIMEOUT);
765795
connect_before_exit(endpoint, connect, |timeout| launched.wait_status(timeout))
766796
}
767797

@@ -771,7 +801,7 @@ fn connect_before_exit<T>(
771801
mut connect: impl FnMut() -> io::Result<T>,
772802
mut wait_status: impl FnMut(Duration) -> io::Result<Option<i32>>,
773803
) -> Result<Option<T>> {
774-
let deadline = Instant::now() + Duration::from_secs(60);
804+
let deadline = Instant::now() + CONSOLE_TIMEOUT;
775805
loop {
776806
check_interrupt()?;
777807
if wait_status(Duration::ZERO)
@@ -795,7 +825,7 @@ fn connect_before_exit<T>(
795825
let exit_wait = if retry {
796826
Duration::ZERO
797827
} else {
798-
Duration::from_millis(250).min(deadline.saturating_duration_since(Instant::now()))
828+
PEER_EXIT_WAIT.min(deadline.saturating_duration_since(Instant::now()))
799829
};
800830
if wait_status(exit_wait)
801831
.map_err(Exit::from_os_error)?
@@ -812,7 +842,7 @@ fn connect_before_exit<T>(
812842
format!("VM console endpoint did not become available: {endpoint}"),
813843
));
814844
}
815-
thread::sleep(Duration::from_millis(10));
845+
thread::sleep(FOREGROUND_POLL);
816846
}
817847
}
818848

@@ -843,7 +873,7 @@ fn console_input(console: Box<dyn Transport>) -> Result<(Box<dyn Transport>, Inp
843873
.spawn(move || {
844874
let result = (|| {
845875
let mut stdin = io::stdin().lock();
846-
let mut buffer = [0; 8192];
876+
let mut buffer = [0; TRANSFER_BUFFER_LEN];
847877
loop {
848878
let count = stdin.read(&mut buffer)?;
849879
if count == 0 {
@@ -898,7 +928,7 @@ impl<'a> ForegroundOutput<'a> {
898928
}
899929

900930
fn forward(&mut self) -> Result<bool> {
901-
let mut buffer = [0; 8192];
931+
let mut buffer = [0; TRANSFER_BUFFER_LEN];
902932
let count = self.log.read(&mut buffer).map_err(Exit::from_os_error)?;
903933
let mut progressed = count != 0;
904934
self.stderr
@@ -908,7 +938,7 @@ impl<'a> ForegroundOutput<'a> {
908938
let timeout = if progressed {
909939
Duration::ZERO
910940
} else {
911-
Duration::from_millis(10)
941+
FOREGROUND_POLL
912942
};
913943
match transport.read(&mut buffer, Some(timeout)) {
914944
Ok(0) => {
@@ -965,7 +995,7 @@ pub(super) fn attach_with_output<T: Send>(
965995
while !attaching.is_finished() {
966996
check_interrupt()?;
967997
if !forward()? {
968-
thread::sleep(Duration::from_millis(10));
998+
thread::sleep(FOREGROUND_POLL);
969999
}
9701000
}
9711001
Ok(())
@@ -976,7 +1006,7 @@ pub(super) fn attach_with_output<T: Send>(
9761006
if let Err(failure) = cancellation.cancel() {
9771007
cancel_failure.get_or_insert(failure);
9781008
}
979-
thread::sleep(Duration::from_millis(10));
1009+
thread::sleep(FOREGROUND_POLL);
9801010
}
9811011
let _ = attaching
9821012
.join()
@@ -992,7 +1022,7 @@ pub(super) fn attach_with_output<T: Send>(
9921022
.join()
9931023
.unwrap_or_else(|panic| std::panic::resume_unwind(panic));
9941024
if attached.is_err() {
995-
let deadline = Instant::now() + Duration::from_millis(250);
1025+
let deadline = Instant::now() + ATTACH_FAILURE_DRAIN;
9961026
loop {
9971027
match forward() {
9981028
Ok(true) if Instant::now() < deadline => {}
@@ -1042,15 +1072,15 @@ fn foreground(
10421072
&endpoint,
10431073
process,
10441074
&capability,
1045-
60.0,
1075+
ATTACH_TIMEOUT.as_secs_f64(),
10461076
cancellation,
10471077
)
10481078
},
10491079
|| output.forward(),
10501080
) {
10511081
Ok(session) => *control = Some(session),
10521082
Err(Exit::EndpointClosed) => {
1053-
attach_closed = Some(Instant::now() + Duration::from_secs(60));
1083+
attach_closed = Some(Instant::now() + ATTACH_CLOSED_GRACE);
10541084
}
10551085
Err(error) => return Err(error),
10561086
}
@@ -1075,7 +1105,7 @@ fn foreground(
10751105
return Err(Exit::EndpointClosed);
10761106
}
10771107
if !progressed {
1078-
thread::sleep(Duration::from_millis(10));
1108+
thread::sleep(FOREGROUND_POLL);
10791109
}
10801110
}
10811111
}
@@ -1217,8 +1247,8 @@ fn run_cold(
12171247
}
12181248
})
12191249
},
1220-
now + Duration::from_millis(250),
1221-
now + Duration::from_secs(10),
1250+
now + STOP_GRACE,
1251+
now + KILL_TIMEOUT,
12221252
)
12231253
.map_err(error_with_cause)?;
12241254
cleanup = true;

0 commit comments

Comments
 (0)