From 4e1cfe01a8e8fb02b97a201abbc57b3540284e88 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 22 Nov 2019 14:06:17 -0800 Subject: [PATCH] linux: fix potentially busy loop I introduced this issue with the recent rate limiting changes. If we generated sufficient events to fill the pipe buffer and trigger an EAGAIN on the write side of the pipe, we'd end up in a state where epoll would continually wake us up to deal with it, but because we gated reading from the pipe on having entries in the queue we could decide that there was nothing to do and leave the pipe unread. This commit adjusts things so that we always try to read some data from it. This is OK because we're using the pipe to knock the main thread out of a sleep rather than as the definitive count of events. --- window/src/os/x11/connection.rs | 12 +++--------- window/src/spawn.rs | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/window/src/os/x11/connection.rs b/window/src/os/x11/connection.rs index 9f68bbd51..e539059c9 100644 --- a/window/src/os/x11/connection.rs +++ b/window/src/os/x11/connection.rs @@ -282,15 +282,9 @@ impl ConnectionOps for Connection { match poll.poll(&mut events, Some(period)) { Ok(_) => { - for event in &events { - let t = event.token(); - if t == tok_xcb { - self.process_queued_xcb()?; - } else if t == tok_spawn { - SPAWN_QUEUE.run(); - } else { - } - } + // We process both event sources unconditionally + // in the loop above anyway; we're just using + // this to get woken up. } Err(err) => { diff --git a/window/src/spawn.rs b/window/src/spawn.rs index 8c6f9de42..0f2ee6853 100644 --- a/window/src/spawn.rs +++ b/window/src/spawn.rs @@ -141,13 +141,21 @@ impl SpawnQueue { // On linux we only ever process one at at time, so that // we can return to the main loop and process messages // from the X server - use std::io::Read; if let Some(func) = self.pop_func() { func(); - - let mut byte = [0u8]; - self.read.lock().unwrap().read(&mut byte).ok(); } + + // try to drain the pipe. + // We do this regardless of whether we popped an item + // so that we avoid being in a perpetually signalled state. + // It is ok if we completely drain the pipe because the + // main loop uses the return value to set the sleep + // interval and will unconditionally call us on each + // iteration. + let mut byte = [0u8; 64]; + use std::io::Read; + self.read.lock().unwrap().read(&mut byte).ok(); + self.has_any_queued() } }