Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential race condition in rule type engine cache #3918

Merged
merged 3 commits into from
Jul 17, 2024

Conversation

dmjb
Copy link
Contributor

@dmjb dmjb commented Jul 17, 2024

This addresses a potential race condition I discovered while working on another task. If a rule instance with a new rule type is added to a profile after the rule type cache is populated, but before the profiles and rule instances are queried, the evaluation will fail because the new rule type will be missing from the cache.

This changes the logic to query for rule types which are not found in the cache, and create a new rule type engine instance and cache it if we get a result.

Alternatives Considered

I could simplify this to remove the pre-population and query the database for each rule type when we encounter it first. This would simplify the logic, but at the expense of throwing away the performance gains from loading most of the rules we need in one query.

Summary

Provide a brief overview of the changes and the issue being addressed.
Explain the rationale and any background necessary for understanding the changes.
List dependencies required by this change, if any.

Fixes #(related issue)

Change Type

Mark the type of change your PR introduces:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

This addresses a potential race condition I discovered while working on
another task. If a rule instance with a new rule type is added to a
profile after the rule type cache is populated, but before the profiles
and rule instances are queried, the evaluation will fail because the new
rule type will be missing from the cache.

This changes the logic to query for rule types which are not found in
the cache, and create a new rule type engine instance and cache it if we
get a result.

*Alternatives Considered*

I could simplify this to remove the pre-population and query the
database for each rule type when we encounter it first. This would
simplify the logic, but at the expense of throwing away the performance
gains from loading most of the rules we need in one query.
@dmjb dmjb requested a review from a team as a code owner July 17, 2024 12:09
@coveralls
Copy link

coveralls commented Jul 17, 2024

Coverage Status

coverage: 54.393% (+0.08%) from 54.311%
when pulling 163b9d3 on rule-cache-race-condition
into e0b91af on main.

@dmjb dmjb merged commit 8a27510 into main Jul 17, 2024
21 checks passed
@dmjb dmjb deleted the rule-cache-race-condition branch July 17, 2024 14:04
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.

3 participants