Skip to content

Commit 9a19349

Browse files
authored
Merge pull request #2239 from microsoft/agents/issue-2237-host-time-until
nvx: compute the time left until a host deadline with one helper
2 parents 8d47379 + 31c446e commit 9a19349

6 files changed

Lines changed: 40 additions & 31 deletions

File tree

‎nvx/src/host/platform/linux/endpoint.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ pub(crate) fn endpoint_server_pid_until(
326326
/// Bounds each wait for the backlog to drain to 25 ms before rechecking the
327327
/// endpoint and the caller's deadline.
328328
const MAX_BACKLOG_WAIT: Duration = Duration::from_millis(25);
329-
let left = time_left(Some(deadline))?.expect("the probe has a deadline");
329+
let left =
330+
crate::host::platform::time_until(deadline).ok_or(io::ErrorKind::TimedOut)?;
330331
thread::sleep(left.min(MAX_BACKLOG_WAIT));
331332
}
332333
Err(error) => return Err(error),

‎nvx/src/host/platform/mod.rs‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,18 +321,21 @@ pub(crate) type TransportHandle = std::os::fd::OwnedFd;
321321
#[cfg(windows)]
322322
pub(crate) type TransportHandle = std::os::windows::io::OwnedHandle;
323323

324+
/// Returns the time left until `deadline`, or `None` once it has passed: a deadline that is due
325+
/// now has no time left.
326+
pub(super) fn time_until(deadline: std::time::Instant) -> Option<Duration> {
327+
deadline
328+
.checked_duration_since(std::time::Instant::now())
329+
.filter(|left| !left.is_zero())
330+
}
331+
324332
/// Returns the time left until `deadline`, `None` without one, or an [`io::ErrorKind::TimedOut`]
325333
/// error once it has passed.
326334
#[cfg(any(target_os = "linux", windows))]
327335
fn time_left(deadline: Option<std::time::Instant>) -> io::Result<Option<Duration>> {
328-
match deadline {
329-
None => Ok(None),
330-
Some(deadline) => deadline
331-
.checked_duration_since(std::time::Instant::now())
332-
.filter(|left| !left.is_zero())
333-
.map(Some)
334-
.ok_or_else(|| io::ErrorKind::TimedOut.into()),
335-
}
336+
deadline
337+
.map(|deadline| time_until(deadline).ok_or_else(|| io::ErrorKind::TimedOut.into()))
338+
.transpose()
336339
}
337340

338341
/// Returns the error of an owned child whose exit could not be confirmed: `wait`, the failure of

‎nvx/src/host/platform/tests.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,17 @@ fn an_unconfirmed_exit_reports_both_failures_when_the_termination_failed_too() {
226226
"cannot terminate the child: access denied; cannot wait for its exit: the wait failed"
227227
);
228228
}
229+
230+
#[test]
231+
fn time_until_returns_the_time_left_before_a_deadline_and_none_after_it() {
232+
let span = Duration::from_secs(3600);
233+
let before = std::time::Instant::now();
234+
let left = time_until(before + span).expect("an hour away has time left");
235+
assert!(left > Duration::ZERO && left <= span);
236+
237+
let now = std::time::Instant::now();
238+
assert_eq!(time_until(now), None);
239+
if let Some(passed) = now.checked_sub(span) {
240+
assert_eq!(time_until(passed), None);
241+
}
242+
}

‎nvx/src/host/platform/windows/endpoint.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ pub(crate) fn endpoint_server_pid_until(
285285
/// Bounds each busy-pipe wait to 100 ms before rechecking the endpoint and
286286
/// the caller's deadline.
287287
const MAX_PIPE_WAIT_MS: u128 = 100;
288-
let left = time_left(Some(deadline))?.expect("the probe has a deadline");
288+
let left =
289+
crate::host::platform::time_until(deadline).ok_or(io::ErrorKind::TimedOut)?;
289290
let wait = left.as_millis().min(MAX_PIPE_WAIT_MS) as u32;
290291
// Zero selects the server's default timeout, not a nonblocking wait.
291292
if wait == 0 {

‎nvx/src/host/restore_driver.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,7 @@ impl Watch<'_> {
360360
/// Returns how long the next wait until `deadline` may take, or why the step ends.
361361
fn slice(&self, deadline: Instant) -> Result<Duration, SessionError> {
362362
self.check()?;
363-
deadline
364-
.checked_duration_since(Instant::now())
365-
.filter(|left| !left.is_zero())
363+
super::platform::time_until(deadline)
366364
.map(|left| left.min(POLL))
367365
.ok_or(SessionError::TimedOut)
368366
}

‎nvx/src/host/transport.rs‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,13 @@ pub(crate) fn connect_verified(
6666
let mut unavailable = None;
6767
loop {
6868
check_running(&process)?;
69-
let remaining = deadline
70-
.checked_duration_since(Instant::now())
71-
.filter(|remaining| !remaining.is_zero())
72-
.ok_or_else(|| {
73-
Error::timed_out("the control connection timed out").with_source(
74-
unavailable
75-
.take()
76-
.unwrap_or_else(|| io::Error::from(io::ErrorKind::TimedOut)),
77-
)
78-
})?;
69+
let remaining = platform::time_until(deadline).ok_or_else(|| {
70+
Error::timed_out("the control connection timed out").with_source(
71+
unavailable
72+
.take()
73+
.unwrap_or_else(|| io::Error::from(io::ErrorKind::TimedOut)),
74+
)
75+
})?;
7976
let attempt =
8077
platform::connect_endpoint(endpoint, reference.pid, remaining.min(ATTEMPT_TIMEOUT));
8178
check_running(&process)?;
@@ -402,14 +399,9 @@ impl HostConnection {
402399

403400
/// Converts a deadline into the time left, failing once it has passed.
404401
fn remaining(deadline: Option<Instant>) -> Result<Option<Duration>, HostConnectionError> {
405-
match deadline {
406-
None => Ok(None),
407-
Some(deadline) => deadline
408-
.checked_duration_since(Instant::now())
409-
.filter(|remaining| !remaining.is_zero())
410-
.map(Some)
411-
.ok_or(HostConnectionError::TimedOut),
412-
}
402+
deadline
403+
.map(|deadline| platform::time_until(deadline).ok_or(HostConnectionError::TimedOut))
404+
.transpose()
413405
}
414406

415407
/// Returns the failure for an event that the session reports in no state that the connection

0 commit comments

Comments
 (0)