From e5535a3a3a31557bc35cc5acc35b91f8a0b43f4e Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Sun, 24 Sep 2023 00:49:09 +0200 Subject: [PATCH] ReaderRolling: Guard against races between scheduled tasks and CloseDocument (#10934) Fix #10932 --------- Co-authored-by: Frans de Jonge --- .../apps/reader/modules/readerrolling.lua | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/frontend/apps/reader/modules/readerrolling.lua b/frontend/apps/reader/modules/readerrolling.lua index 310709fac..c6a9e937d 100644 --- a/frontend/apps/reader/modules/readerrolling.lua +++ b/frontend/apps/reader/modules/readerrolling.lua @@ -78,6 +78,10 @@ local ReaderRolling = InputContainer:extend{ RELOADING_DOCUMENT = 4, DO_RELOAD_DOCUMENT = 5, }, + + mark_func = nil, + unmark_func = nil, + _stepRerenderingAutomation = nil, } function ReaderRolling:init() @@ -313,6 +317,16 @@ end -- we cannot do it in onSaveSettings() because getLastPercent() uses self.ui.document function ReaderRolling:onCloseDocument() self:tearDownRerenderingAutomation() + -- Unschedule anything that might still somehow be... + if self.mark_func then + UIManager:unschedule(self.mark_func) + end + if self.unmark_func then + UIManager:unschedule(self.unmark_func) + end + UIManager:unschedule(self.onCheckDomStyleCoherence) + UIManager:unschedule(self.onUpdatePos) + self.current_header_height = nil -- show unload progress bar at top self.ui.doc_settings:saveSetting("percent_finished", self:getLastPercent()) @@ -349,7 +363,7 @@ function ReaderRolling:onCheckDomStyleCoherence() ok_callback = function() -- Allow for ConfirmBox to be closed before showing -- "Opening file" InfoMessage - UIManager:scheduleIn(0.5, function () + UIManager:scheduleIn(0.5, function() -- And check we haven't quit reader in these 0.5s if self.ui.document then self.ui:reloadDocument() @@ -801,7 +815,7 @@ end function ReaderRolling:onGotoXPointer(xp, marker_xp) if self.mark_func then - -- unschedule previous marker as it's no more accurate + -- Unschedule previous marker as it's no longer accurate. UIManager:unschedule(self.mark_func) self.mark_func = nil end @@ -1005,9 +1019,7 @@ function ReaderRolling:onBatchedUpdateDone() self.batched_update_count = 0 -- Be sure any Notification gets a chance to be painted before -- a blocking rerendering - UIManager:nextTick(function() - self:onUpdatePos() - end) + UIManager:nextTick(self.onUpdatePos, self) end end @@ -1036,7 +1048,10 @@ function ReaderRolling:onUpdatePos(force) -- Calling this now ensures the re-rendering is done by crengine -- so updatePos() has good info and can reposition -- the previous xpointer accurately: - self.ui.document:getCurrentPos() + if self.ui.document then + -- This can be racy with CloseDocument, as it's scheduled by onBatchedUpdateDone, guard it + self.ui.document:getCurrentPos() + end -- Otherwise, _readMetadata() would do that, but the positioning -- would not work as expected, for some reason (it worked @@ -1087,9 +1102,7 @@ function ReaderRolling:updatePos(force) -- Allow for the new rendering to be shown before possibly showing -- the "Styles have changed..." ConfirmBox so the user can decide -- if it is really needed - UIManager:scheduleIn(0.1, function () - self:onCheckDomStyleCoherence() - end) + UIManager:scheduleIn(0.1, self.onCheckDomStyleCoherence, self) end function ReaderRolling:onChangeViewMode() @@ -1619,7 +1632,7 @@ Note that %1 (out of %2) xpaths from your bookmarks and highlights have been nor ok_text = _("Upgrade now"), ok_callback = function() -- Allow for ConfirmBox to be closed before migrating - UIManager:scheduleIn(0.5, function () + UIManager:scheduleIn(0.5, function() -- And check we haven't quit reader in these 0.5s if self.ui.document then -- We'd rather not have any painting between the upgrade