fix: keep library scan progress from finishing a library mid-scan #64

Merged
jfro merged 2 commits from jtk/scan-progress-races into main 2026-10-18 23:00:04 +00:00
Owner

Fixes the scan progress races from Nye's audit of dae07b9.

  1. Stage claimed up front (medium). Libraries.mark_scanning/1 now sets scan_progress_stage: "scanning" (current 0, total nil) along with scanning_status, and ScanWorker.perform sets it again at the start (for retried jobs). Before, the stage was first written after discover_files and the first file, so a ProcessWorker finishing while the scan was queued or walking a big share could mark_scan_complete, and start_scan would then allow a second scan.
  2. Progress writes serialized (low-medium). ProcessWorker.update_library_progress/1 now reads the library, counts and writes inside a transaction holding pg_advisory_xact_lock(71_023_402, library_id), so a stale "processing 9/10" can't land after another worker marked the library complete. The stage check moved inside the lock too. The admin libraries page only shows the bar while scanning_status == "scanning".
  3. Handoff result checked (low). ScanWorker matches {:ok, _} from update_scan_progress(..., "processing", ...); on error it logs Logger.error with the library id and changeset errors, then marks the scan complete and clears progress so the library isn't left stuck in the "scanning" stage.
  4. Failed final update (low). When the final update_ebook fails, ProcessWorker now calls update_library_progress after handle_error, like the other error path.
  5. Tests: mark_scanning claims the stage; a ProcessWorker can't complete a library whose scan is queued; completing once everything's processed; ScanWorker handoff with a mix of completed and pending ebooks (stays scanning, "processing 1/2") and with all completed (idle, progress cleared, no jobs). The JobFailureHandler test now hands progress to "processing" first, since a discarded job during the scan stage should no longer finish the library.
  6. Stage writes under the lock (from Nye's review). mark_scanning/1 and ScanWorker's claim_progress now take the same progress lock (Libraries.lock_progress/1, shared with ProcessWorker), so a ProcessWorker holding it can't clear the "scanning" stage from its stale copy right after mark_scanning commits. Not unit-tested: under the SQL sandbox every process shares one connection, so advisory locks don't block each other there.

Behavior change: the progress bar now shows only while scanning_status == "scanning", so watcher-triggered processing on an idle library no longer shows "processing X/Y".

Fixes #60

Fixes the scan progress races from Nye's audit of dae07b9. 1. **Stage claimed up front (medium).** `Libraries.mark_scanning/1` now sets `scan_progress_stage: "scanning"` (current 0, total nil) along with `scanning_status`, and `ScanWorker.perform` sets it again at the start (for retried jobs). Before, the stage was first written after `discover_files` and the first file, so a ProcessWorker finishing while the scan was queued or walking a big share could `mark_scan_complete`, and `start_scan` would then allow a second scan. 2. **Progress writes serialized (low-medium).** `ProcessWorker.update_library_progress/1` now reads the library, counts and writes inside a transaction holding `pg_advisory_xact_lock(71_023_402, library_id)`, so a stale "processing 9/10" can't land after another worker marked the library complete. The stage check moved inside the lock too. The admin libraries page only shows the bar while `scanning_status == "scanning"`. 3. **Handoff result checked (low).** ScanWorker matches `{:ok, _}` from `update_scan_progress(..., "processing", ...)`; on error it logs `Logger.error` with the library id and changeset errors, then marks the scan complete and clears progress so the library isn't left stuck in the "scanning" stage. 4. **Failed final update (low).** When the final `update_ebook` fails, ProcessWorker now calls `update_library_progress` after `handle_error`, like the other error path. 5. **Tests:** `mark_scanning` claims the stage; a ProcessWorker can't complete a library whose scan is queued; completing once everything's processed; ScanWorker handoff with a mix of completed and pending ebooks (stays scanning, "processing 1/2") and with all completed (idle, progress cleared, no jobs). The JobFailureHandler test now hands progress to "processing" first, since a discarded job during the scan stage should no longer finish the library. 6. **Stage writes under the lock (from Nye's review).** `mark_scanning/1` and ScanWorker's `claim_progress` now take the same progress lock (`Libraries.lock_progress/1`, shared with ProcessWorker), so a ProcessWorker holding it can't clear the "scanning" stage from its stale copy right after `mark_scanning` commits. Not unit-tested: under the SQL sandbox every process shares one connection, so advisory locks don't block each other there. **Behavior change:** the progress bar now shows only while `scanning_status == "scanning"`, so watcher-triggered processing on an idle library no longer shows "processing X/Y". Fixes #60
jfro added this to the 0.6.2 milestone 2026-10-18 22:00:05 +00:00
fix: keep library scan progress from finishing a library mid-scan
All checks were successful
CI / test (pull_request) Successful in 1m57s
6150e24835
Author
Owner

Review from Nye (approved) — 6150e24

All 5 points of #60 are covered. The two-int4 advisory key can't collide with @book_match_lock (bigint key space), and I found no nested or reverse lock order. A MatchError on mark_scan_complete rolls back the transaction and is caught by the rescue. The JobFailureHandler test change is correct.

  • Low (non-blocking): mark_scanning and claim_progress write the stage without the progress lock. A ProcessWorker already holding the lock can clear_scan_progress on its stale struct (stage processing → nil) right after mark_scanning commits, dropping the 'scanning' stage until ScanWorker.perform re-claims it. Optional hardening: take pg_advisory_xact_lock(71_023_402, id) in mark_scanning, which already runs inside start_scan's transaction.
  • Note: the bar now shows only while scanning_status == 'scanning', so watcher-triggered processing on an idle library no longer shows 'processing X/Y'. Fine as intended; please mention it in the PR body.
Review from Nye (approved) — 6150e24 All 5 points of #60 are covered. The two-int4 advisory key can't collide with `@book_match_lock` (bigint key space), and I found no nested or reverse lock order. A MatchError on `mark_scan_complete` rolls back the transaction and is caught by the rescue. The JobFailureHandler test change is correct. - **Low** (non-blocking): `mark_scanning` and `claim_progress` write the stage without the progress lock. A ProcessWorker already holding the lock can `clear_scan_progress` on its stale struct (stage processing → nil) right after `mark_scanning` commits, dropping the 'scanning' stage until `ScanWorker.perform` re-claims it. Optional hardening: take `pg_advisory_xact_lock(71_023_402, id)` in `mark_scanning`, which already runs inside start_scan's transaction. - **Note:** the bar now shows only while scanning_status == 'scanning', so watcher-triggered processing on an idle library no longer shows 'processing X/Y'. Fine as intended; please mention it in the PR body.
fix: take the progress lock when marking a library as scanning
All checks were successful
CI / test (pull_request) Successful in 1m46s
9ae238100c
Author
Owner

Review from Nye (approved) at 9ae2381

mark_scanning/1 and ScanWorker.claim_progress/1 now take the same (71_023_402, library_id) advisory xact lock as ProcessWorker via shared Libraries.lock_progress/1; claim_progress reloads the library inside the lock. Return shapes unchanged; nesting inside start_scan's transaction is fine. Closes the low race from the earlier review. Merges clean onto main, CI green.

Non-blocking low: mark_scanning still builds its changeset from the caller's struct, so a stale struct with scan_progress_stage already "scanning" (DB nil) would skip the write. start_scan's scanning?/1 guard makes it unlikely; reloading inside the lock would remove it. Optional follow-up.

Review from Nye (approved) at 9ae2381 `mark_scanning/1` and `ScanWorker.claim_progress/1` now take the same `(71_023_402, library_id)` advisory xact lock as ProcessWorker via shared `Libraries.lock_progress/1`; `claim_progress` reloads the library inside the lock. Return shapes unchanged; nesting inside start_scan's transaction is fine. Closes the low race from the earlier review. Merges clean onto main, CI green. Non-blocking low: `mark_scanning` still builds its changeset from the caller's struct, so a stale struct with `scan_progress_stage` already "scanning" (DB nil) would skip the write. start_scan's `scanning?/1` guard makes it unlikely; reloading inside the lock would remove it. Optional follow-up.
jfro merged commit 613acc3115 into main 2026-10-18 23:00:04 +00:00
Sign in to join this conversation.
No reviewers
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!64
No description provided.