From 84830fc07d65d58fce9b24b5ec8f1224b0969ac0 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 29 Aug 2016 19:40:24 -0700 Subject: [PATCH] QLockFile::tryLock: prevent over-sleeping in certain cases The sleep time increases exponentially, but we never checked whether the time to sleep was less than the remaining time. For example, if timeout was 4000 ms on entry, we'd progressively sleep 100 ms, 200, 400, 800, 1600 ms. At this point, the accummulated sleep time would be 3100 ms and the next sleep should be no more than 900 ms. Prior to this change, the tryLock() would then proceed to sleep 3200 ms, for a total wait time of 6300 ms, or 57.5% above the timeout provided by the user. Change-Id: Ifc295639c8cf4ddcaa69fffd146f7586a7ee95e4 Reviewed-by: David Faure --- src/corelib/io/qlockfile.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/corelib/io/qlockfile.cpp b/src/corelib/io/qlockfile.cpp index e7275eeaed..ae3a7c6abc 100644 --- a/src/corelib/io/qlockfile.cpp +++ b/src/corelib/io/qlockfile.cpp @@ -233,8 +233,13 @@ bool QLockFile::tryLock(int timeout) } break; } - if (timer.hasExpired()) + + int remainingTime = timer.remainingTime(); + if (remainingTime == 0) return false; + else if (uint(sleepTime) > uint(remainingTime)) + sleepTime = remainingTime; + QThread::msleep(sleepTime); if (sleepTime < 5 * 1000) sleepTime *= 2;