fix: keep library scan progress from finishing a library mid-scan #64
No reviewers
Labels
No labels
bug
critical
ebook-branch
high
low
main
medium
release-blocker
security
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
jfro/fuzzy_catalog!64
Loading…
Reference in a new issue
No description provided.
Delete branch "jtk/scan-progress-races"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes the scan progress races from Nye's audit of
dae07b9.Libraries.mark_scanning/1now setsscan_progress_stage: "scanning"(current 0, total nil) along withscanning_status, andScanWorker.performsets it again at the start (for retried jobs). Before, the stage was first written afterdiscover_filesand the first file, so a ProcessWorker finishing while the scan was queued or walking a big share couldmark_scan_complete, andstart_scanwould then allow a second scan.ProcessWorker.update_library_progress/1now reads the library, counts and writes inside a transaction holdingpg_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 whilescanning_status == "scanning".{:ok, _}fromupdate_scan_progress(..., "processing", ...); on error it logsLogger.errorwith 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.update_ebookfails, ProcessWorker now callsupdate_library_progressafterhandle_error, like the other error path.mark_scanningclaims 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.mark_scanning/1and ScanWorker'sclaim_progressnow 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 aftermark_scanningcommits. 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
Review from Nye (approved) —
6150e24All 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 onmark_scan_completerolls back the transaction and is caught by the rescue. The JobFailureHandler test change is correct.mark_scanningandclaim_progresswrite the stage without the progress lock. A ProcessWorker already holding the lock canclear_scan_progresson its stale struct (stage processing → nil) right aftermark_scanningcommits, dropping the 'scanning' stage untilScanWorker.performre-claims it. Optional hardening: takepg_advisory_xact_lock(71_023_402, id)inmark_scanning, which already runs inside start_scan's transaction.Review from Nye (approved) at
9ae2381mark_scanning/1andScanWorker.claim_progress/1now take the same(71_023_402, library_id)advisory xact lock as ProcessWorker via sharedLibraries.lock_progress/1;claim_progressreloads 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_scanningstill builds its changeset from the caller's struct, so a stale struct withscan_progress_stagealready "scanning" (DB nil) would skip the write. start_scan'sscanning?/1guard makes it unlikely; reloading inside the lock would remove it. Optional follow-up.