From a3ca0965cff2c37758a07c9130b92fdb7dc5f655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Thu, 29 Dec 2016 22:51:07 +0100 Subject: [PATCH] Fix wrong epoll timeout in case of already expired timers. This could cause process/task starvation due to an infinite wait timeout. --- source/eventcore/drivers/epoll.d | 4 ++-- source/eventcore/drivers/posix.d | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/eventcore/drivers/epoll.d b/source/eventcore/drivers/epoll.d index 9432832..70d59f7 100644 --- a/source/eventcore/drivers/epoll.d +++ b/source/eventcore/drivers/epoll.d @@ -34,13 +34,13 @@ final class EpollEventLoop : PosixEventLoop { override bool doProcessEvents(Duration timeout) @trusted { - import std.algorithm : min; + import std.algorithm : min, max; //assert(Fiber.getThis() is null, "processEvents may not be called from within a fiber!"); debug (EventCoreEpollDebug) print("Epoll wait %s, %s", m_events.length, timeout); long tomsec; if (timeout == Duration.max) tomsec = long.max; - else tomsec = (timeout.total!"hnsecs" + 9999) / 10_000; + else tomsec = max((timeout.total!"hnsecs" + 9999) / 10_000, 0); auto ret = epoll_wait(m_epoll, m_events.ptr, cast(int)m_events.length, tomsec > int.max ? -1 : cast(int)tomsec); debug (EventCoreEpollDebug) print("Epoll wait done: %s", ret); diff --git a/source/eventcore/drivers/posix.d b/source/eventcore/drivers/posix.d index c2e954d..b634d8b 100644 --- a/source/eventcore/drivers/posix.d +++ b/source/eventcore/drivers/posix.d @@ -132,7 +132,7 @@ final class PosixEventDriverCore(Loop : PosixEventLoop, Timers : EventDriverTime final override ExitReason processEvents(Duration timeout) { - import std.algorithm : min; + import std.algorithm : min, max; import core.time : hnsecs, seconds; if (m_exit) { @@ -150,7 +150,7 @@ final class PosixEventDriverCore(Loop : PosixEventLoop, Timers : EventDriverTime } else { long now = currStdTime; do { - auto nextto = min(m_timers.getNextTimeout(now), timeout); + auto nextto = max(min(m_timers.getNextTimeout(now), timeout), 0.seconds); got_events = m_loop.doProcessEvents(nextto); long prev_step = now; now = currStdTime;