Skip to content

Conversation

@praffq
Copy link
Contributor

@praffq praffq commented Nov 22, 2025

Proposed Changes

  • Fixing deadlock causing in GET and LIST endpoint of tag config.

Cause

GET/LIST endpoints were calling .save(), which triggered the post_save signal used for cache invalidation. Since reads don't change data, they shouldn't invalidate cache. This caused recursive invalidation cascades and deadlocks.


Only PR's with test cases included and passing lint and test pipelines will be reviewed

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

  • Refactor
    • Optimized internal caching mechanism to improve performance by reducing unnecessary processing overhead.

✏️ Tip: You can customize this high-level summary in your review settings.

@praffq praffq requested a review from a team as a code owner November 22, 2025 07:36
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 22, 2025

📝 Walkthrough

Walkthrough

The get_parent_json method in care/emr/models/tag_config.py now persists cached parent JSON through direct database update instead of the standard model save flow, bypassing model-level hooks.

Changes

Cohort / File(s) Change Summary
Cache Persistence Optimization
care/emr/models/tag_config.py
Modified get_parent_json to use direct TagConfig.objects.filter(id=self.id).update(...) for storing computed cached_parent_json instead of invoking self.save(update_fields=[...]), thereby circumventing full save flow and associated model hooks

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Signal handlers & hooks: Verify no model signals (pre_save, post_save) depend on this save operation for cache invalidation, logging, or other side effects
  • Consistency implications: Confirm that bypassing the save flow doesn't inadvertently skip validation, audit trails, or timestamp updates that might be relied upon elsewhere
  • Concurrent access: Assess whether direct query execution introduces any race conditions or stale cache scenarios in high-concurrency environments

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Description check ❓ Inconclusive The description covers proposed changes and the root cause, but omits the Associated Issue link and merge checklist sections required by the template. Add a link to the associated GitHub issue (#3372) and complete the merge checklist with test status, documentation updates, and linting confirmation.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title directly addresses the main change: fixing a deadlock issue in TagConfig endpoints by avoiding .save() calls during read operations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch prafful/bugs/tag-config-deadlock

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

@praffq praffq changed the title fix the deadlock fix deadlock in TagConfig endpoints Nov 22, 2025
Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
care/emr/models/tag_config.py (1)

69-90: Direct update to persist cache looks correct; confirm skipped save/signals are intentional and consider documenting

Switching to

TagConfig.objects.filter(id=self.id).update(
    cached_parent_json=self.cached_parent_json
)

nicely avoids the save()/post_save cycle that was causing the deadlock, while still doing a single atomic DB UPDATE on just this field. That aligns with the stated PR goal.

Two non‑blocking points to double‑check:

  • This bypasses Model.save(), signals, and any auto_now/updated_at style fields on EMRBaseModel. If anything downstream relies on those changing when the cache is refreshed, this behavior will silently diverge from before.
  • It might be slightly safer/clearer to use the instance’s class/PK and leave a short comment so nobody “helpfully” changes this back to save() later, e.g.:
type(self).objects.filter(pk=self.pk).update(
    cached_parent_json=self.cached_parent_json
)
# NB: use queryset.update() here specifically to avoid save() signals / cache invalidation loops.

If those trade‑offs are acceptable, the change itself looks good and should resolve the deadlock.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6d59888 and 74b0668.

📒 Files selected for processing (1)
  • care/emr/models/tag_config.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py

📄 CodeRabbit inference engine (.cursorrules)

**/*.py: Prioritize readability and maintainability; follow Django's coding style guide (PEP 8 compliance).
Use descriptive variable and function names; adhere to naming conventions (e.g., lowercase with underscores for functions and variables).

Files:

  • care/emr/models/tag_config.py
**/{models,views,management/commands}/*.py

📄 CodeRabbit inference engine (.cursorrules)

Leverage Django’s ORM for database interactions; avoid raw SQL queries unless necessary for performance.

Files:

  • care/emr/models/tag_config.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Test / Test

@codecov
Copy link

codecov bot commented Nov 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.92%. Comparing base (6d59888) to head (74b0668).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3372      +/-   ##
===========================================
+ Coverage    73.91%   73.92%   +0.01%     
===========================================
  Files          435      435              
  Lines        19748    19748              
  Branches      2138     2138              
===========================================
+ Hits         14597    14599       +2     
+ Misses        4705     4704       -1     
+ Partials       446      445       -1     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants