Skip to content

🐛(backend) fix race condition in reconciliation requests CSV import#2153

Open
Ash-Crow wants to merge 1 commit intomainfrom
sbl-reconciliation-fix-race-condition
Open

🐛(backend) fix race condition in reconciliation requests CSV import#2153
Ash-Crow wants to merge 1 commit intomainfrom
sbl-reconciliation-fix-race-condition

Conversation

@Ash-Crow
Copy link
Copy Markdown
Collaborator

@Ash-Crow Ash-Crow commented Mar 30, 2026

Purpose

The call to the background task is now wrapped in a on_commit to ensure that it isn't called before the save is finished, in order to avoid race condition issues.

Proposal

  • Wrapped the call to user_reconciliation_csv_import_job in a transaction.on_commit on UserReconciliationCsvImportAdmin.save_model()

External contributions

Thank you for your contribution! 🎉

Please ensure the following items are checked before submitting your pull request:

  • I have read and followed the contributing guidelines
  • I have read and agreed to the Code of Conduct
  • I have signed off my commits with git commit --signoff (DCO compliance)
  • I have signed my commits with my SSH or GPG key (git commit -S)
  • My commit messages follow the required format: <gitmoji>(type) title description
  • I have added a changelog entry under ## [Unreleased] section (if noticeable change)
  • I have added corresponding tests for new features or bug fixes (if applicable)

@Ash-Crow Ash-Crow force-pushed the sbl-reconciliation-fix-race-condition branch from d425857 to 2de0452 Compare March 30, 2026 14:18
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a812963f-29ad-4a90-b713-90d333e15370

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

This pull request addresses a race condition in the CSV import reconciliation workflow. The CHANGELOG.md is updated to document a bug fix for reconciliation requests CSV import. The backend admin file is modified to defer Celery task queuing for UserReconciliationCsvImport objects until after the database transaction commits, rather than queuing immediately. This prevents the task from executing before the object is persisted to the database.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: fixing a race condition in reconciliation CSV import, directly matching the changeset's core purpose.
Description check ✅ Passed The description is directly related to the changeset, explaining the purpose of wrapping the background task call in transaction.on_commit to prevent race conditions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sbl-reconciliation-fix-race-condition

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/backend/core/admin.py`:
- Around line 114-116: The worker task user_reconciliation_csv_import_job should
defensively handle the case where
UserReconciliationCsvImport.objects.get(id=job_id) raises DoesNotExist: update
the task (the function user_reconciliation_csv_import_job in
src/backend/core/tasks/user_reconciliation.py) to wrap the get(...) call in a
try/except catching UserReconciliationCsvImport.DoesNotExist (or the model's
DoesNotExist), log a clear warning including the job_id, and return gracefully
instead of allowing the exception to bubble up.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1e9b5a58-283d-4817-920b-b4bb5ef45c9c

📥 Commits

Reviewing files that changed from the base of the PR and between f166e75 and 2de0452.

📒 Files selected for processing (2)
  • CHANGELOG.md
  • src/backend/core/admin.py

@Ash-Crow Ash-Crow force-pushed the sbl-reconciliation-fix-race-condition branch 2 times, most recently from 8dc3c18 to d434b95 Compare March 30, 2026 14:59

if not change:
user_reconciliation_csv_import_job.delay(obj.pk)
transaction.on_commit(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Django documentation says this: If you call on_commit() while there isn’t an open transaction, the callback will be executed immediately.

Maybe I'm wrong, but if you are not in an transaction.atomic block, the callback will be executed immediately. Is it what you want ?

Copy link
Copy Markdown
Collaborator Author

@Ash-Crow Ash-Crow Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are in Django Admin here, which IIRC uses transactions by default (contrary to normal Django views.)

The call to the background task is now wrapped in a on_commit to ensure
that it isn't called before the save is finished, in order to avoid
race condition issues.
@Ash-Crow Ash-Crow force-pushed the sbl-reconciliation-fix-race-condition branch from d434b95 to 9405535 Compare March 31, 2026 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants