Skip to content

fix: correct race condition in with_for_update#607

Merged
cofin merged 3 commits intomainfrom
fix/with-for-update
Nov 15, 2025
Merged

fix: correct race condition in with_for_update#607
cofin merged 3 commits intomainfrom
fix/with-for-update

Conversation

@cofin
Copy link
Member

@cofin cofin commented Nov 11, 2025

This corrects an issue in the with_for_update behavior:

  • Before the change, passing with_for_update to service.update() or repository.update() only affected the post-flush session.refresh() call. The row that gets copied and mutated was always retrieved with a plain SELECT, so two concurrent writers could both read the same version
    • Now the with_for_update flag is honored when the row is first fetched (both in the service’s item_id branch and inside SQLAlchemyAsyncRepository.get()). When you call service.update(..., with_for_update=True) (or pass the richer dict form/ForUpdateArg), the initial SELECT ... FOR UPDATE runs, so the session holds the expected lock before any field copying or merges occur.

@codecov-commenter
Copy link

codecov-commenter commented Nov 11, 2025

Codecov Report

❌ Patch coverage is 73.33333% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.35%. Comparing base (c08ef25) to head (0ab0faf).

Files with missing lines Patch % Lines
advanced_alchemy/repository/_async.py 73.33% 2 Missing and 2 partials ⚠️
advanced_alchemy/repository/_sync.py 73.33% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #607      +/-   ##
==========================================
+ Coverage   80.32%   80.35%   +0.02%     
==========================================
  Files          87       87              
  Lines        6405     6433      +28     
  Branches      828      838      +10     
==========================================
+ Hits         5145     5169      +24     
  Misses       1000     1000              
- Partials      260      264       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud
Copy link

@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/advanced-alchemy-docs-preview/607

@cofin cofin merged commit a8b93b0 into main Nov 15, 2025
20 checks passed
@cofin cofin deleted the fix/with-for-update branch November 15, 2025 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants