From 689ec2a839673cc3293d653518b77cd48079909f Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Sat, 22 Jan 2022 23:44:07 +0100 Subject: [PATCH] PowerD: Prevent potential dependency loop in get*Capacity Not currently happening in vanilla code, but, not entirely unlikely. stateChanged already uses a similar guard. Re https://github.com/koreader/koreader/pull/8672#discussion_r790126388 --- frontend/device/generic/powerd.lua | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/frontend/device/generic/powerd.lua b/frontend/device/generic/powerd.lua index 6850de776..baa492de1 100644 --- a/frontend/device/generic/powerd.lua +++ b/frontend/device/generic/powerd.lua @@ -150,10 +150,15 @@ function BasePowerD:setIntensity(intensity) end function BasePowerD:getCapacity() - -- NOTE: UIManager *should* be loaded at this point. - -- If that doesn't hold, c.f., :stateChanged below. - local UIManager = require("ui/uimanager") - local now_ts = UIManager:getTime() + -- BasePowerD is loaded before UIManager. + -- Nothing *currently* calls this before UIManager is actually loaded, but future-proof this anyway. + local now_ts + if package.loaded["ui/uimanager"] ~= nil then + local UIManager = require("ui/uimanager") + now_ts = UIManager:getTime() + else + now_ts = TimeVal:now() + end if (now_ts - self.last_aux_capacity_pull_time):tonumber() >= 60 then self.batt_capacity = self:getCapacityHW() @@ -167,8 +172,13 @@ function BasePowerD:isCharging() end function BasePowerD:getAuxCapacity() - local UIManager = require("ui/uimanager") - local now_ts = UIManager:getTime() + local now_ts + if package.loaded["ui/uimanager"] ~= nil then + local UIManager = require("ui/uimanager") + now_ts = UIManager:getTime() + else + now_ts = TimeVal:now() + end if (now_ts - self.last_aux_capacity_pull_time):tonumber() >= 60 then local aux_batt_capa = self:getAuxCapacityHW()