readest/apps/readest.koplugin/library
Huang Xin 4000a85797
fix(koplugin): collect sync workers before KOReader exits (#7293)
* fix(koplugin): collect sync workers before KOReader exits

Exiting KOReader from inside a book quits about a second after the
close-time pushes fork their workers. UIManager:quit() empties the task
queue, so the polls that would collect those workers never run and the
workers keep going after KOReader is gone. The reporter's Kindle often
segfaulted at exit when that happened.

The sync client now keeps the pid of every forked worker (sync RPCs,
book and cover downloads) and wraps UIManager.quit: running workers get
up to 5 s to finish, then the rest are killed and reaped before the
original quit runs.

Verified:
- New syncclient specs: a worker that finishes in the grace period is
  waited for, a stuck one is killed after 50 x 100 ms, and quit doesn't
  wait with no workers. Without the change the first two fail
  ("Expected 3, passed 0" and "Expected 50, passed 0").
- pnpm test:lua (510 passed), pnpm lint:lua.
- Kindle Voyage, firmware 5.13.6, KOReader v2026.07.2-300 (no Turbo),
  exiting from inside the book with auto sync on. origin/main: 2 of 5
  exits left workers running after KOReader was gone (reparented to
  init). This change: 4 of 4 exits logged "waiting for sync workers
  before exit", then killed and reaped the 3 workers still stuck in TLS;
  none outlived KOReader. No segfault in either build on this device, so
  the crash itself was not reproduced.
- Not verified: a worker finishing within the grace period on a device
  (every push on the test network timed out in TLS), and Android.

Fixes #6685. Reported by tramch on GitHub.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(koplugin): deliver finished worker results at exit and keep partial covers out of the cache

Review follow-up for #6685:
- The exit wait reaped a worker that finished inside the grace period
  but never ran its poll, so a token refresh that completed there lost
  its rotated refresh token. Sync RPC workers now register their poll
  with trackWorker, and the exit wait runs it for every worker that has
  finished. Workers that callbacks start then share the same 5 s budget.
- A cover worker killed at exit left a partial <hash>.png that the cover
  cache treats as complete. The worker now writes <hash>.png.part and
  renames it only after a 200; failures remove the .part.

Verified: two new specs fail without this change
("delivers the response of a worker that finishes while KOReader quits":
expected 1 reply, got 0; "keeps a cover under a temporary name until it
is complete": the file was at dst mid-transfer). pnpm test:lua (512
passed), pnpm lint:lua. Not run on the device: the Kindle could not
reach the Readest API at the time (curl timed out after 10 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(koplugin): give each cover download worker its own temporary file

Review follow-up for #6685. The manual Download Cover action bypasses
the automatic cover queue, so two workers can download the same cover
at once. With one shared <hash>.png.part, one worker could rename it
while the other was still writing, and the second then reported a
failure. Each worker now writes <hash>.png.<pid>.part.

Verified: "keeps a cover under a temporary name until it is complete"
fails without the change (no h1.png.123.part mid-transfer: expected
"file", got nil). pnpm test:lua (512 passed), pnpm lint:lua.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 16:41:51 +01:00
..
bim_patch.lua fix(koplugin): render group cover previews in Library (#4064) 2026-05-05 17:01:35 +02:00
cloud_covers.lua perf(koplugin): defer and cache Library group covers (#4954) (#4974) 2026-07-06 17:55:51 +02:00
cloud_icons.lua feat(koplugin): Readest Library view in KOReader (#4056) 2026-05-04 23:12:34 +02:00
coverprovider.lua feat(koplugin): Readest Library view in KOReader (#4056) 2026-05-04 23:12:34 +02:00
downloadqueue.lua fix(koreader): reliable progress sync with hardened auth and error handling (#6684) 2026-10-09 13:31:01 +02:00
exts.lua feat(koplugin): Readest Library view in KOReader (#4056) 2026-05-04 23:12:34 +02:00
group_covers.lua perf(koplugin): defer and cache Library group covers (#4954) (#4974) 2026-07-06 17:55:51 +02:00
libraryitem.lua perf(koplugin): defer and cache Library group covers (#4954) (#4974) 2026-07-06 17:55:51 +02:00
librarypaint.lua feat(koplugin): Readest Library view in KOReader (#4056) 2026-05-04 23:12:34 +02:00
librarystore.lua fix(koreader): reliable progress sync with hardened auth and error handling (#6684) 2026-10-09 13:31:01 +02:00
libraryviewmenu.lua feat(koreader): background sync, group downloads, and library controls (#6321) 2026-09-20 09:08:31 +02:00
librarywidget.lua fix(koplugin): give the Readest Library menu KOReader's UI for CoverBrowser (#6845) 2026-10-16 13:27:14 +08:00
list_strip.lua feat(koplugin): Readest Library view in KOReader (#4056) 2026-05-04 23:12:34 +02:00
localscanner.lua fix(koplugin): fix sync on Android, library pull ANR, and Rescan library (#6364) 2026-09-22 17:17:50 +02:00
localsend_firewall.lua feat(koplugin): LocalSend receive and send for KOReader devices (#5687) 2026-08-13 19:47:27 +02:00
localsend_helper.lua fix(koplugin): stop BookDrop start errors popping up on every launch (#6604) 2026-10-03 14:55:58 +02:00
readingstatus.lua feat: sync reading status across devices and with KOReader (#4634) (#4656) 2026-06-19 05:00:40 +02:00
statussync.lua feat: sync reading status across devices and with KOReader (#4634) (#4656) 2026-06-19 05:00:40 +02:00
syncbooks.lua fix(koplugin): collect sync workers before KOReader exits (#7293) 2026-11-03 16:41:51 +01:00