Race-guard the six bookmark/context join tables - #702
Merged
Conversation
Each of ArticleSummaryContext, ArticleFragmentContext, ArticleTitleContext, VideoTitleContext, VideoCaptionContext and ExampleSentenceContext carried a copy-pasted find_or_create with two latent bugs: 1. `except NoResultFound or InterfaceError:` -- `A or B` evaluates to `A`, so only NoResultFound was ever caught. 2. No UNIQUE(bookmark_id, <anchor_id>), so a concurrent first-open race could INSERT two rows for the same (bookmark, anchor), after which find_by_bookmark/find_or_create (.one()) 500'd with MultipleResultsFound. Mirror the fix already applied to ArticleLevelSummaryContext (commit 01520e6): add a db.UniqueConstraint to each model plus a matching ADD UNIQUE migration, and rewrite find_or_create to one_or_none() + insert inside a SAVEPOINT, catching IntegrityError to re-query and return the row the concurrent request created (respecting the commit param). Prod was verified duplicate-free for all six (bookmark_id, anchor_id) pairs on 2026-08-13, so the migration needs no de-dupe step. Adds test_bookmark_context_uniqueness.py covering idempotency and unique-constraint rejection across all six models (12 subtests). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
ArchLens - No architecturally relevant changes to the existing views |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Each of the six bookmark/context join models shares a copy-pasted
find_or_createwith two latent bugs:except sqlalchemy.orm.exc.NoResultFound or sqlalchemy.exc.InterfaceError:is a Python bug:A or Bevaluates toA, so onlyNoResultFoundwas ever caught;InterfaceErrornever was.(bookmark_id, <anchor_id>)— a concurrent first-open race (two requests INSERTing between each other's SELECT and INSERT) silently created duplicate rows, after whichfind_by_bookmark/find_or_create(.one()) 500'd withMultipleResultsFound.Affected models:
ArticleSummaryContext,ArticleFragmentContext,ArticleTitleContext,VideoTitleContext,VideoCaptionContext,ExampleSentenceContext.Fix
Mirrors the pattern already applied to
ArticleLevelSummaryContext(commit 01520e6, #698):db.UniqueConstraint(bookmark_id, <anchor_id>)to each model's__table_args__(dict → tuple) so the SQLite test DB enforces it too, plus a matchingADD UNIQUE KEYin a new SQL migration.find_or_createto.one_or_none()+ insert insidewith session.begin_nested():, catchingIntegrityErrorto re-query and return the row the concurrent request created (respecting thecommitparam, so the caller's still-open transaction isn't rolled back whencommit=False).Prod verification
Checked prod before adding the constraints — 0 duplicate
(bookmark_id, anchor_id)groups across all six tables on 2026-08-13, so the migration needs no de-dupe step and theALTERs won't fail on existing data.Tests
test_bookmark_context_uniqueness.pycovers idempotency and unique-constraint rejection across all six models (2 tests × 6 models = 12 subtests, passing).🤖 Generated with Claude Code