Skip to content

fix(go-policy): use RLock for rule scan and check threshold before incrementing - #2232

Merged
Imran Siddique (imran-siddique) merged 1 commit into
microsoft:mainfrom
imran-siddique:fix/go-policy-rlock-and-ratelimit
May 13, 2026
Merged

Imran Siddique (imran-siddique) merged 1 commit into
microsoft:mainfrom
imran-siddique:fix/go-policy-rlock-and-ratelimit

Conversation

@imran-siddique

Copy link
Copy Markdown
Collaborator

Applies finnoybu patterns #2019 and #2021:

  • Evaluate() held an exclusive lock for the entire rule scan, serializing all concurrent callers. Fix: RLock for the read-only scan, write lock only in checkRateLimit().
  • checkRateLimit incremented count then compared, so once over the limit the counter grew unboundedly. Fix: check >= MaxCalls before incrementing.

Supersedes conflicted PRs #2019 and #2021.

…crementing

- Split Evaluate() lock: RLock for read-only rule scan and backend
  snapshot, write lock only in checkRateLimit() for mutation
- Check count >= MaxCalls BEFORE incrementing so counter stays
  pinned at the limit instead of growing unboundedly on denied calls
- Add TestRateLimitCounterDoesNotGrowAfterDeny regression test

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@imran-siddique
Imran Siddique (imran-siddique) merged commit 1e52b64 into microsoft:main May 13, 2026
27 of 28 checks passed
@imran-siddique
Imran Siddique (imran-siddique) deleted the fix/go-policy-rlock-and-ratelimit branch May 13, 2026 01:05
@github-actions github-actions Bot added the size/M Medium PR (< 200 lines) label May 13, 2026
@github-actions

Copy link
Copy Markdown
🤖 AI Agent: docs-sync-checker — Docs Sync

Docs Sync

  • Documentation is in sync.

@github-actions

Copy link
Copy Markdown
🤖 AI Agent: breaking-change-detector — API Compatibility

API Compatibility

Severity Change Impact
High Changed Evaluate method to use RLock for rule scanning instead of Lock. Potentially breaking for concurrent use cases if external code relied on the previous locking behavior.
High Modified checkRateLimit to check >= MaxCalls before incrementing the counter. May alter behavior for cases where external code depended on the previous counter behavior.

@github-actions

Copy link
Copy Markdown
🤖 AI Agent: test-generator — `agentmesh/policy.go`

agentmesh/policy.go

  • TestEvaluateConcurrentAccess -- Validate that Evaluate correctly handles concurrent access with RLock and Lock.
  • TestCheckRateLimitThreadSafety -- Ensure checkRateLimit maintains thread safety when accessed concurrently.
  • TestEvaluateWithMultipleRules -- Test Evaluate with multiple rules to ensure correct rule matching and decision-making.
  • TestEvaluateWithBackends -- Verify Evaluate correctly handles external policy backends.
  • TestCheckRateLimitEdgeCases -- Test checkRateLimit with edge cases, such as MaxCalls set to 0 or negative values.

@github-actions

Copy link
Copy Markdown
🤖 AI Agent: security-scanner — View details

No security issues found.

@github-actions

Copy link
Copy Markdown
🤖 AI Agent: code-reviewer — Action Items:

TL;DR: 0 blockers, 1 warning. Code improves concurrency and rate-limiting logic, but lacks test coverage for RLock behavior.

# Sev Issue Where
1 Warn Missing test for concurrent RLock behavior policy_test.go

Action Items:

  • None.

Warnings:

# Description Resolution
1 Add tests to validate RLock behavior under concurrent access. Fine as follow-up PR.

@github-actions

Copy link
Copy Markdown

PR Review Summary

Check Status Details
🔍 Code Review ⚠️ Warning See details
🛡️ Security Scan ✅ Passed No issues found
🔄 Breaking Changes ✅ Completed Analysis complete
📝 Docs Sync ✅ Passed No issues found
🧪 Test Coverage ✅ Completed Analysis complete

Verdict: ⚠️ Ready for human review

MohammadHaroonAbuomar pushed a commit to MohammadHaroonAbuomar/agt-acs that referenced this pull request Jun 1, 2026
…crementing (microsoft#2232)

- Split Evaluate() lock: RLock for read-only rule scan and backend
  snapshot, write lock only in checkRateLimit() for mutation
- Check count >= MaxCalls BEFORE incrementing so counter stays
  pinned at the limit instead of growing unboundedly on denied calls
- Add TestRateLimitCounterDoesNotGrowAfterDeny regression test

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Medium PR (< 200 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant