Library scan progress races can flip a library to idle mid-scan #60

Closed
opened 2026-10-18 21:57:49 +00:00 by jfro · 0 comments
Owner

Found in Nye's audit of dae07b9 on main.

  1. (Medium) The stage gate at process_worker.ex:205 only skips when scan_progress_stage == "scanning", but that's first written at scan_worker.ex:254-257, after discover_files and the first file. mark_scanning (libraries.ex:181) only sets scanning_status, so a ProcessWorker can mark_scan_complete while the scan is queued or walking a big share, and start_scan then allows a second scan.
    Fix: set scan_progress_stage "scanning" (current 0, total nil) in mark_scanning or at the top of ScanWorker.perform, with a test.
  2. (Low-medium) write_library_progress (process_worker.ex:217-243) counts, then writes with no lock, so a stale "processing 9/10" can land after complete and leave a stuck bar.
    Fix: a transaction with pg_advisory_xact_lock(key, library_id), like @book_match_lock, and only show the bar (admin_libraries_live.html.heex:44) while scanning_status == "scanning".
  3. (Low) scan_worker.ex:74 ignores the update_scan_progress result. Match {:ok, _}, and on error Logger.error with library_id and changeset errors, then fall back to mark_scan_complete/clear_scan_progress.
  4. (Low) process_worker.ex:177-178: when the final update_ebook fails, it calls handle_error but not update_library_progress. Call it too.
  5. Tests: a ScanWorker handoff with a mix of completed and pending ebooks, and with all of them completed (scan_worker_test.exs:242 only passes because Oban testing is :manual).
Found in Nye's audit of dae07b9 on main. 1. **(Medium)** The stage gate at `process_worker.ex:205` only skips when `scan_progress_stage == "scanning"`, but that's first written at `scan_worker.ex:254-257`, after `discover_files` and the first file. `mark_scanning` (`libraries.ex:181`) only sets `scanning_status`, so a ProcessWorker can `mark_scan_complete` while the scan is queued or walking a big share, and `start_scan` then allows a second scan. Fix: set `scan_progress_stage` "scanning" (current 0, total nil) in `mark_scanning` or at the top of `ScanWorker.perform`, with a test. 2. **(Low-medium)** `write_library_progress` (`process_worker.ex:217-243`) counts, then writes with no lock, so a stale "processing 9/10" can land after complete and leave a stuck bar. Fix: a transaction with `pg_advisory_xact_lock(key, library_id)`, like `@book_match_lock`, and only show the bar (`admin_libraries_live.html.heex:44`) while `scanning_status == "scanning"`. 3. **(Low)** `scan_worker.ex:74` ignores the `update_scan_progress` result. Match `{:ok, _}`, and on error `Logger.error` with library_id and changeset errors, then fall back to `mark_scan_complete`/`clear_scan_progress`. 4. **(Low)** `process_worker.ex:177-178`: when the final `update_ebook` fails, it calls `handle_error` but not `update_library_progress`. Call it too. 5. **Tests:** a ScanWorker handoff with a mix of completed and pending ebooks, and with all of them completed (`scan_worker_test.exs:242` only passes because Oban testing is `:manual`).
jfro added this to the 0.6.2 milestone 2026-10-18 21:57:49 +00:00
jfro closed this issue 2026-10-18 23:00:04 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/fuzzy_catalog#60
No description provided.