Skip to content

Commit

Permalink
attempt to fix crashes
Browse files Browse the repository at this point in the history
When the database is locked, an exception is raised and koreader crashes
(SEGV). Enable set_busy_timeout to reduce the likelihood this happens,
and also close all statements immediately instead of waiting for the GC.
  • Loading branch information
liskin committed Nov 3, 2023
1 parent f889726 commit 1dd53a8
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pocketbooksync.koplugin/main.lua
Expand Up @@ -12,6 +12,9 @@ local UIManager = require("ui/uimanager")
local SQ3 = require("lua-ljsqlite3/init")
local pocketbookDbConn = SQ3.open("/mnt/ext1/system/explorer-3/explorer-3.db")

-- wait for database locks for up to 1 second before raising an error
pocketbookDbConn:set_busy_timeout(1000)

local PocketbookSync = WidgetContainer:extend{
name = "pocketbooksync",
is_doc_only = false,
Expand All @@ -29,28 +32,32 @@ function PocketbookSync:sync(folder, file, page)
completed = 1
end

local stmt = [[
local sql = [[
SELECT book_id
FROM files
WHERE
folder_id = (SELECT id FROM folders WHERE name = ? LIMIT 1)
AND filename = ?
LIMIT 1
]]
local row = pocketbookDbConn:prepare(stmt):reset():bind(folder, file):step()
local stmt = pocketbookDbConn:prepare(sql)
local row = stmt:reset():bind(folder, file):step()
stmt:close()

if row == nil then
logger.info("Pocketbook Sync: Book id for " .. folder .. "/" .. file .. " not found")
return
end
local book_id = row[1]

local stmt = [[
local sql = [[
REPLACE INTO books_settings
(bookid, profileid, cpage, npage, completed, opentime)
VALUES (?, 1, ?, ?, ?, ?)
]]
pocketbookDbConn:prepare(stmt):reset():bind(book_id, page, totalPages, completed, os.time(os.date("!*t"))):step()
local stmt = pocketbookDbConn:prepare(sql)
stmt:reset():bind(book_id, page, totalPages, completed, os.time(os.date("!*t"))):step()
stmt:close()
end

function PocketbookSync:getFolderFile()
Expand Down

0 comments on commit 1dd53a8

Please sign in to comment.