From 37db00cedcb56aa63689107b5157b5cd4ea76de2 Mon Sep 17 00:00:00 2001 From: Davide Bertola Date: Mon, 2 Oct 2023 18:27:19 +0200 Subject: [PATCH] fix very slow poll_writable() --- src/reactor.rs | 4 +++- tests/async.rs | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/reactor.rs b/src/reactor.rs index d6f4ad9..bbb3c3a 100644 --- a/src/reactor.rs +++ b/src/reactor.rs @@ -461,7 +461,9 @@ impl Source { panic::catch_unwind(|| w.wake()).ok(); } state[dir].waker = Some(cx.waker().clone()); - state[dir].ticks = Some((Reactor::get().ticker(), state[dir].tick)); + if state[dir].ticks.is_none() { + state[dir].ticks = Some((Reactor::get().ticker(), state[dir].tick)); + } // Update interest in this I/O handle. if was_empty { diff --git a/tests/async.rs b/tests/async.rs index c856760..49b89d9 100644 --- a/tests/async.rs +++ b/tests/async.rs @@ -8,6 +8,7 @@ use std::thread; use std::time::Duration; use async_io::{Async, Timer}; +use futures_lite::future::poll_fn; use futures_lite::{future, prelude::*}; #[cfg(unix)] use tempfile::tempdir; @@ -155,6 +156,25 @@ fn udp_send_recv() -> io::Result<()> { }) } +#[test] +fn test_poll_writable_iterations() -> io::Result<()> { + future::block_on(async { + let socket = Async::::bind(([127, 0, 0, 1], 0))?; + + let mut attempts = 0; + + poll_fn(|cx| { + attempts += 1; + socket.poll_writable(cx) + }) + .await?; + + assert!(attempts < 5); + + Ok(()) + }) +} + #[cfg(unix)] #[test] fn udp_connect() -> io::Result<()> {