Skip to content

fix: prevent EnforcerServer crash on duplicate casbin_rule insert#64

Open
tispratik wants to merge 1 commit intoapache:masterfrom
EnthuZiastic:master
Open

fix: prevent EnforcerServer crash on duplicate casbin_rule insert#64
tispratik wants to merge 1 commit intoapache:masterfrom
EnthuZiastic:master

Conversation

@tispratik
Copy link
Copy Markdown
Contributor

The casbin_rule table has a unique index casbin_rule_content_unique on (ptype, v0..v6) NULLS NOT DISTINCT. A bare repo.insert raises Ecto.ConstraintError when a duplicate rule is inserted — e.g. two nodes racing after an EnforcerServer restart, or in-memory and DB state diverging. That exception propagates out of handle_call and crashes the EnforcerServer GenServer, a critical process that is slow to recover.

Patched add_policy/2 and insert_policy/3 (used by save_policies/2) to use on_conflict: :nothing, which silently skips the duplicate and returns {:ok, struct}. No data is lost — the rule already exists in the DB.

The casbin_rule table has a unique index casbin_rule_content_unique on
(ptype, v0..v6) NULLS NOT DISTINCT. A bare repo.insert raises
Ecto.ConstraintError when a duplicate rule is inserted — e.g. two nodes
racing after an EnforcerServer restart, or in-memory and DB state
diverging. That exception propagates out of handle_call and crashes the
EnforcerServer GenServer, a critical process that is slow to recover.

Patched add_policy/2 and insert_policy/3 (used by save_policies/2) to
use on_conflict: :nothing, which silently skips the duplicate and returns
{:ok, struct}. No data is lost — the rule already exists in the DB.

Fixes Sentry issue BACKEND-2H2.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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