Repository navigation
fix(catalog): stop parallel scans creating duplicate name-only people - #3166
Conversation
FindOrCreate looked a person up by name and then inserted with no lock, so parallel audiobook and ebook scan workers (or replicas sharing one database) that all missed the lookup each inserted their own row for the same author. The author's books then split across namesake people. Hold a transaction-scoped advisory lock keyed on the lower-cased name around the name lookup and insert. People may still share a name: the provider-safe name rules decide whether a stored row matches, the lock only stops concurrent callers from both missing.
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
This fixes a user-visible change: the body describes duplicate Authors entries becoming one entry with all books. Please replace the no-user-visible-change statement with before-and-after captures of that Authors view using the same synthetic library, identify the builds, and include desktop and phone-width web captures (or explain why mobile is unaffected). |
|
The name-only person fix changes which books appear under an author, so the Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness. Posted via Macroscope — Visible change evidence |
|
The two cases listed as unblocked (#1254 C1 and #1256 C11) are -Validation tasks: unblocks #1254 C1; unblocks #1256 C11
+Validation tasks: noneAutomated check: Macroscope check run agent (gpt-6-luna). No validation was performed. Posted via Macroscope — v1 validation impact |
|
Not adding Authors captures. The race only shows when two scan workers create the same person at the same moment, and I can't reproduce that on demand on a real server. The new concurrency test covers it. A library scanned without the race looks the same before and after, so a before/after pair would show identical screens. |
Quick104
left a comment
There was a problem hiding this comment.
Reviewed 80023e74b3d210eb82a91ae0ea4f26a663e876c5. One conditional concurrency defect reproduced against a disposable PostgreSQL database; I am fixing it on this branch.
Note
AI disclosure: OpenAI Codex in T3 Code, model gpt-6.1-sol, using GitHub CLI and Go. Independent source review by Claude, model claude-opus-5-5; its isolation concern was verified by running the existing test on the reviewed head.
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
Fit: 10/10 · Readiness: 9/10 — ready. CI passed on Reviewed Finding: one P3, fixed in Verification: the original test failed on the merge base with eight rows for one name. The enhanced test failed on the unfixed PR head with eight rows. After the repair, concurrency and namesake-preservation tests passed five runs with Validated features: source tracing found no regression to these accepted Movie/Series person cases; their provider-backed credits use the existing batch ingestion path.
Coordination: no API or client change is required for Apple, Android or Jellyfin; each reads the same stored people. The manual's audiobook library and listening pages, release definition and feature tables remain accurate. No manual issue is needed. The disclosed batch-insert race and existing duplicate rows remain outside this fix. Note AI disclosure: OpenAI Codex in T3 Code, model |
Problem
Related issue: #1253
Validation tasks: changes #1254 C1; changes #1256 C11
A fresh scan can create the same person twice. In a synthetic audiobook library with three books by one author, the first scan stored two "Ada Lane" rows: two books credited one, the third the other. Her author page, the library's Authors view and People search then split her books, and "Also by Ada Lane" misses the book on the other row.
The audiobook scan processes folders in parallel, and
PersonRepository.FindOrCreatelooked a name up and then inserted it with nothing in between, so two workers that both missed the lookup each inserted a row. Names can't carry a unique index, because different people share names. This change serialises the lookup and insert for one name.Approach
The name lookup and the insert now run in one transaction behind
pg_advisory_xact_lock(hashtextextended('people-name:' || LOWER(name), 0)), following the repo's existinghashtextextended('<namespace>:'||key, 0)locks. The transaction explicitly uses READ COMMITTED so the lookup sees the preceding lock holder's insert even when the connection defaults to REPEATABLE READ. The lock lives in Postgres, so it also holds between Silo replicas sharing one database, and it is held only for one SELECT and one INSERT. Matching still goes throughcanResolvePersonByName, so namesakes with different provider IDs stay separate. Lookups by TMDB or IMDb ID are unchanged; those columns already have unique indexes.Ebook scans call the same function and are covered too.
BatchFindOrCreate(metadata ingestion and book enrichment) has the same look-up-then-insert gap for people with no provider ID. Closing it there needs the transaction to span several phases and multi-key locking, so it is left for a follow-up. Existing duplicate rows are not merged; there is no merge tool for name-only people yet.Validation
scripts/ci/db-contracts.txt) runs 20 rounds of 8 concurrentFindOrCreatecalls for one name in mixed case and expects one row. It sets the connection default to REPEATABLE READ to check snapshot visibility after the lock wait. The original test failed on the merge base with eight rows; the enhanced test failed on the unfixed PR head with eight rows. After the isolation fix, the concurrency and namesake-preservation tests pass five runs with-race.7c8e0e354:make test-db-pins(157 query pins and 372 database contracts), catalog/scanner package tests, package vet, andmake lint-changedpass against a disposable PostgreSQL 18 database where applicable. This review did not repeat a complete scan through the UI or the full local pre-PR gate.go test ./internal/catalog/... ./internal/scanner/...with a database,make test-db-contracts,make lint-changed: pass.Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/pr-3166/
Postgres test results for 8 concurrent creations of one name-only person: the base commit made 5 people for one name; this branch makes one. In the library, the race shows as one author split into several Authors entries. With this change, the Authors view shows "Ada Lane · 4 books" instead of two separate entries for her.
Risks
None identified. The lock is per name and held briefly; scans that create many different people do not wait on each other.
Checklist
AI Disclosure
BatchFindOrCreategap remains disclosed above.Note
Fix duplicate name-only people from parallel
FindOrCreatescanscatalog.PersonRepository.FindOrCreatewith the same name-only person could create duplicate rows. The name fallback now runs throughfindOrCreateByNamein a read-committed transaction, which takes a transaction-scoped advisory lock keyed by the lowercased name before lookup and insert.findPersonByNameand the insert intoinsertPerson; both run through the caller's transaction, and the insert commits with it.TestPersonFindOrCreateConcurrentNameOnlyPostgresin person_name_fallback_db_test.go — 20 rounds of 8 concurrent workers with mixed capitalization, asserting one row and one person ID per round — and registers it in db-contracts.txt.insertPersonnow writes through the caller's transaction, so insert failures roll back with it; name lookups acquire an advisory lock (personNameLockSQL), which serializes concurrent name-only creates.Macroscope summarized 7c8e0e3.