Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f7d5ba70 authored by Mike Yu's avatar Mike Yu Committed by Automerger Merge Worker
Browse files

Merge "Make boot_time::timeout() timeout immediately when passing zero duration" am: 8bc0e5c6

Original change: https://android-review.googlesource.com/c/platform/packages/modules/DnsResolver/+/2015217

Change-Id: I5215d7a94cf6968b6a32bbc189f57bee8f56ed95
parents 51717221 8bc0e5c6
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ impl TimerFd {
    }

    fn set(&self, duration: Duration) {
        assert_ne!(duration, Duration::from_millis(0));
        let timer = libc::itimerspec {
            it_interval: libc::timespec { tv_sec: 0, tv_nsec: 0 },
            it_value: libc::timespec {
@@ -147,6 +148,13 @@ pub async fn timeout<T>(duration: Duration, future: impl Future<Output = T>) ->
    // Ideally, all timeouts in a runtime would share a timerfd. That will be much more
    // straightforwards to implement when moving this functionality into `tokio`.

    // According to timerfd_settime(), setting zero duration will disarm the timer, so
    // we return immediate timeout here.
    // Can't use is_zero() for now because sc-mainline-prod's Rust version is below 1.53.
    if duration == Duration::from_millis(0) {
        return Err(Elapsed(()));
    }

    // The failure conditions for this are rare (see `man 2 timerfd_create`) and the caller would
    // not be able to do much in response to them. When integrated into tokio, this would be called
    // during runtime setup.
@@ -204,3 +212,11 @@ async fn timeout_drift() {
        assert!(drift < Duration::from_millis(5));
    }
}

#[tokio::test]
async fn timeout_duration_zero() {
    let start = BootTime::now();
    assert!(timeout(Duration::from_millis(0), pending::<()>()).await.is_err());
    let taken = start.elapsed();
    assert!(taken < Duration::from_millis(5));
}