fix: prevent stale ORM cache reads in load_policy / load_filtered_policy#30
Closed
fix: prevent stale ORM cache reads in load_policy / load_filtered_policy#30
Conversation
…cy to prevent stale ORM cache reads Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Investigate multi-worker behavior with PyCasbin and async SQLAlchemy
fix: prevent stale ORM cache reads in load_policy / load_filtered_policy
Feb 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SQLAlchemy's ORM identity map can serve cached (stale) objects when a long-lived or external session is reused across multiple
load_policycalls. Any rows whose primary keys are already in the identity map are returned from the in-process cache rather than re-read from the database — so rules committed by another worker or a direct DB write are silently invisible until the session is replaced.Changes
adapter.py— add.execution_options(populate_existing=True)to theSELECTin bothload_policyandload_filtered_policy. This forces SQLAlchemy to overwrite identity-map entries with the values returned by the query, guaranteeing fresh data on every call regardless of session lifetime. No-op for the default internal session (empty identity map each call).tests/test_external_session.py— addstest_load_policy_returns_fresh_data_with_external_session: seeds data, performs an initial load (populating the identity map), commits a new rule via a separate session (simulating another worker), then asserts the reused external session sees the new rule afterload_policyis called again.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.