Skip to content

Race-guard the six bookmark/context join tables - #702

Merged
mircealungu merged 1 commit into
masterfrom
race-guard-bookmark-context-joins
Aug 13, 2026
Merged

Race-guard the six bookmark/context join tables#702
mircealungu merged 1 commit into
masterfrom
race-guard-bookmark-context-joins

Conversation

@mircealungu

Copy link
Copy Markdown
Member

Problem

Each of the six bookmark/context join models shares a copy-pasted find_or_create with two latent bugs:

  1. Wrong exception swallowedexcept sqlalchemy.orm.exc.NoResultFound or sqlalchemy.exc.InterfaceError: is a Python bug: A or B evaluates to A, so only NoResultFound was ever caught; InterfaceError never was.
  2. No UNIQUE constraint on (bookmark_id, <anchor_id>) — a concurrent first-open race (two requests INSERTing between each other's SELECT and INSERT) silently created duplicate rows, after which find_by_bookmark / find_or_create (.one()) 500'd with MultipleResultsFound.

Affected models: ArticleSummaryContext, ArticleFragmentContext, ArticleTitleContext, VideoTitleContext, VideoCaptionContext, ExampleSentenceContext.

Fix

Mirrors the pattern already applied to ArticleLevelSummaryContext (commit 01520e6, #698):

  • Add a db.UniqueConstraint(bookmark_id, <anchor_id>) to each model's __table_args__ (dict → tuple) so the SQLite test DB enforces it too, plus a matching ADD UNIQUE KEY in a new SQL migration.
  • Rewrite find_or_create to .one_or_none() + insert inside with session.begin_nested():, catching IntegrityError to re-query and return the row the concurrent request created (respecting the commit param, so the caller's still-open transaction isn't rolled back when commit=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 the ALTERs won't fail on existing data.

Tests

test_bookmark_context_uniqueness.py covers idempotency and unique-constraint rejection across all six models (2 tests × 6 models = 12 subtests, passing).

🤖 Generated with Claude Code

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>
@github-actions

Copy link
Copy Markdown

ArchLens - No architecturally relevant changes to the existing views

@mircealungu
mircealungu merged commit fcd5237 into master Aug 13, 2026
1 of 3 checks passed
@mircealungu
mircealungu deleted the race-guard-bookmark-context-joins branch August 13, 2026 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant