From b6262d9874103db334cab6c64c993b974ff71c10 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sat, 21 Oct 2023 22:16:33 -0700 Subject: [PATCH] Fix timeouts Signed-off-by: John Nunley --- src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e714676..d03e9d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,7 +70,7 @@ use std::marker::PhantomData; use std::num::NonZeroUsize; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Mutex; -use std::time::Duration; +use std::time::{Duration, Instant}; use cfg_if::cfg_if; @@ -646,12 +646,18 @@ impl Poller { /// poller.delete(&socket)?; /// # std::io::Result::Ok(()) /// ``` - pub fn wait(&self, events: &mut Events, timeout: Option) -> io::Result { + pub fn wait(&self, events: &mut Events, mut timeout: Option) -> io::Result { let span = tracing::trace_span!("Poller::wait", ?timeout); let _enter = span.enter(); if let Ok(_lock) = self.lock.try_lock() { + let deadline = timeout.and_then(|timeout| Instant::now().checked_add(timeout)); + loop { + // Figure out how long to wait for. + let timeout = + deadline.map(|deadline| deadline.saturating_duration_since(Instant::now())); + // Wait for I/O events. if let Err(e) = self.poller.wait(&mut events.events, timeout) { // If the wait was interrupted by a signal, clear events and try again.