Skip to content

Commit 14ca267

Browse files
Enrique SaurezCopilot
authored andcommitted
cli: wait for a run's control endpoint on its VM's process
A managed cold run waits for its control endpoint with Session::connect_launched_cancellable, which ends the wait once OpenVMM exits, rather than at its timeout. It took the old supervisor's child type, spawn::Launched, which host::vm::Launched lent it through child_mut, and polled that child's status. It now takes the verified process of the run's host::vm::Launched, beside the endpoint and capability that the launch supplies, and asks that process whether it has exited. The process is the same OpenVMM, held through its pidfd or process handle, so the wait notices its exit as the child's status did, without reaping it: the foreground still reaps it through wait_status. A check that fails still keeps the wait going until the timeout. child_mut becomes private to host::vm, whose wait_status alone uses it, and the session's comments no longer name host::instance. sandbox exec and sandbox stop attach through Session::reattach, which verifies a saved VM through host::vm, and do not change. A new test gives the wait a process that runs and then exits: the wait goes on while the process runs, and ends as a closed endpoint once it exits, rather than at its timeout, on Linux and Windows. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0f92331 commit 14ca267

4 files changed

Lines changed: 109 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,12 +1190,12 @@ fn foreground(
11901190
if managed && output.console.is_some() {
11911191
let endpoint = launched.outputs.endpoint.clone();
11921192
let capability = *launched.capability.as_bytes();
1193-
let child = launched.child_mut().map_err(Exit::from_os_error)?;
1193+
let process = &launched.process;
11941194
match attach_with_output(
11951195
|cancellation| {
11961196
Session::connect_launched_cancellable(
11971197
&endpoint,
1198-
child,
1198+
process,
11991199
&capability,
12001200
60.0,
12011201
cancellation,

‎nvx-rs/src/host/cli/session.rs‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ use std::time::Duration;
3737
use super::deadline::{Deadline, RETRY_INTERVAL, sleep};
3838
use super::{Exit, Result, check_interrupt};
3939
use crate::host::platform::{self, Transport};
40+
use crate::host::process::Process;
4041
use crate::host::runtime::Runtime;
4142
use crate::host::sandbox::options::validate_exec_environment;
4243
use crate::host::session::{ControlSession, SessionError};
43-
use crate::host::supervisor::Launched;
4444
use crate::host::transport;
4545
use crate::host::vm::{self, SavedVm};
4646
use crate::protocol::{
@@ -335,7 +335,7 @@ impl Session {
335335
})
336336
}
337337

338-
/// Connects to `endpoint`, which `launched`, the OpenVMM process that a cold run launched,
338+
/// Connects to `endpoint`, which `process`, the OpenVMM process that a cold run launched,
339339
/// serves, as [`Self::connect_unless_exited`] does, but stops waiting for the endpoint once
340340
/// OpenVMM has exited, as `nvx::host::session::connect` does: an OpenVMM that exits before it
341341
/// serves its endpoint, as one that refuses its configuration does, then ends the wait at
@@ -344,22 +344,18 @@ impl Session {
344344
/// `cancellation`.
345345
pub(super) fn connect_launched_cancellable(
346346
endpoint: &str,
347-
launched: &mut Launched,
347+
process: &Process,
348348
capability: &[u8; CAPABILITY_LEN],
349349
timeout: f64,
350350
cancellation: AttachmentCancellation,
351351
) -> Result<Self> {
352-
let pid = launched.id();
352+
let pid = process.reference().pid;
353353
Self::connect_unless_exited(
354354
endpoint,
355355
pid,
356356
capability,
357357
timeout,
358-
|| {
359-
launched
360-
.wait_timeout(Duration::ZERO)
361-
.is_ok_and(|status| status.is_some())
362-
},
358+
|| process.has_exited().is_ok_and(|exited| exited),
363359
Some(cancellation),
364360
)
365361
}
@@ -686,7 +682,7 @@ impl Session {
686682
match kind {
687683
APP_STDOUT | APP_STDERR => {
688684
// The guest agent forwards at most this much output, so more breaks the
689-
// protocol, as `nvx::host::instance` also finds.
685+
// protocol.
690686
if stdout.len() + stderr.len() + response.len() > MAX_OUTPUT_BYTES {
691687
return Err(violation(&format!(
692688
"managed guest exceeded the {MAX_OUTPUT_BYTES}-byte output limit"

‎nvx-rs/src/host/cli/session/tests.rs‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,89 @@ fn check_waits_for_openvmm_that_exits(endpoint: &str) {
827827
assert_ne!(checks, 0);
828828
}
829829

830+
/// A process that runs for a minute unless it is killed, which dropping it kills and reaps.
831+
#[cfg(any(target_os = "linux", windows))]
832+
struct Sleeper(std::process::Child);
833+
834+
#[cfg(any(target_os = "linux", windows))]
835+
impl Sleeper {
836+
/// Spawns the sleeper under a `SpawnLock`, so that on Windows it holds no handle of a VMM that
837+
/// another test spawns meanwhile.
838+
fn spawn() -> Self {
839+
let mut command = if cfg!(windows) {
840+
let mut command = std::process::Command::new("ping");
841+
command.args(["-n", "60", "127.0.0.1"]);
842+
command
843+
} else {
844+
let mut command = std::process::Command::new("sleep");
845+
command.arg("60");
846+
command
847+
};
848+
command
849+
.stdin(std::process::Stdio::null())
850+
.stdout(std::process::Stdio::null());
851+
Self(crate::host::tests::spawn_locked(&mut command).unwrap())
852+
}
853+
854+
/// Returns the verified process of the sleeper.
855+
fn process(&self) -> Process {
856+
use crate::host::process::{BootId, Opened, ProcessRef};
857+
858+
let reference = ProcessRef {
859+
pid: self.0.id(),
860+
start_time: platform::process_start_time(self.0.id()).unwrap().unwrap(),
861+
boot: BootId::current().unwrap(),
862+
};
863+
let Opened::Running(process) = Process::open(&reference).unwrap() else {
864+
panic!("the sleeper runs");
865+
};
866+
process
867+
}
868+
}
869+
870+
#[cfg(any(target_os = "linux", windows))]
871+
impl Drop for Sleeper {
872+
fn drop(&mut self) {
873+
let _ = self.0.kill();
874+
let _ = self.0.wait();
875+
}
876+
}
877+
878+
/// Checks the wait for `endpoint`, which never appears, of a cold run whose OpenVMM is the
879+
/// verified process that [`Session::connect_launched_cancellable`] receives: the wait goes on
880+
/// while the process runs, and ends at once, as a closed endpoint, once it exits, rather than at
881+
/// the timeout.
882+
#[cfg(any(target_os = "linux", windows))]
883+
fn check_launched_waits_end_once_their_process_exits(endpoint: &str) {
884+
let mut sleeper = Sleeper::spawn();
885+
let process = sleeper.process();
886+
let started = Instant::now();
887+
let failure = thread::scope(|scope| {
888+
let waiting = scope.spawn(|| {
889+
Session::connect_launched_cancellable(
890+
endpoint,
891+
&process,
892+
&CAPABILITY,
893+
60.0,
894+
AttachmentCancellation::default(),
895+
)
896+
.err()
897+
});
898+
thread::sleep(Duration::from_millis(500));
899+
let ended_early = waiting.is_finished();
900+
sleeper.0.kill().unwrap();
901+
sleeper.0.wait().unwrap();
902+
assert!(!ended_early, "the wait ended while OpenVMM ran");
903+
waiting.join().unwrap().unwrap()
904+
});
905+
assert!(
906+
started.elapsed() < Duration::from_secs(30),
907+
"{:?}",
908+
started.elapsed()
909+
);
910+
assert!(matches!(failure, Exit::EndpointClosed), "{failure:?}");
911+
}
912+
830913
#[cfg(any(target_os = "linux", windows))]
831914
fn check_forward_failure_during_connect(endpoint: &str) {
832915
let (attempted, attempt) = mpsc::sync_channel(1);
@@ -1211,6 +1294,14 @@ mod endpoints {
12111294
check_waits_for_openvmm_that_exits(path.to_str().unwrap());
12121295
}
12131296

1297+
#[test]
1298+
#[cfg(target_os = "linux")]
1299+
fn launched_waits_end_once_their_process_exits() {
1300+
let directory = tempfile::tempdir().unwrap();
1301+
let path = directory.path().join("control.sock");
1302+
check_launched_waits_end_once_their_process_exits(path.to_str().unwrap());
1303+
}
1304+
12141305
#[test]
12151306
#[cfg(target_os = "linux")]
12161307
fn forward_failure_cancels_the_connection_before_its_timeout() {
@@ -1446,6 +1537,13 @@ mod endpoints {
14461537
check_waits_for_openvmm_that_exits(r"\\.\pipe\nvx-cli-session-test-exited");
14471538
}
14481539

1540+
#[test]
1541+
fn launched_waits_end_once_their_process_exits() {
1542+
check_launched_waits_end_once_their_process_exits(
1543+
r"\\.\pipe\nvx-cli-session-test-launched-exited",
1544+
);
1545+
}
1546+
14491547
#[test]
14501548
fn forward_failure_cancels_the_connection_before_its_timeout() {
14511549
check_forward_failure_during_connect(r"\\.\pipe\nvx-cli-session-test-cancelled");

‎nvx-rs/src/host/vm.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ pub struct Launched {
863863
pub capability: Capability,
864864
/// The preparation's same restore-ready listener, absent for a cold launch.
865865
pub ready: Option<ReadyListener>,
866-
/// Retains only a foreground caller's child for status and control attachment.
866+
/// Retains only a foreground caller's child, for its status.
867867
#[cfg(feature = "cli")]
868868
child: ForegroundChild,
869869
}
@@ -892,8 +892,8 @@ impl Launched {
892892
.map(|status| status as i32))
893893
}
894894

895-
/// Gives the CLI connector the same owned child whose exit it observes.
896-
pub(crate) fn child_mut(&mut self) -> io::Result<&mut spawn::Launched> {
895+
/// Returns the owned child, whose status [`Self::wait_status`] polls.
896+
fn child_mut(&mut self) -> io::Result<&mut spawn::Launched> {
897897
self.child
898898
.0
899899
.as_mut()

0 commit comments

Comments
 (0)