Repository navigation
fix(async-evaluator): drop redundant asyncio.Lock from evaluate (+ thread-safe RW lock prerequisite) - #1921
Merged
Imran Siddique (imran-siddique) merged 2 commits intoMay 10, 2026
Merged
fix(async-evaluator): drop redundant asyncio.Lock from evaluate (+ thread-safe RW lock prerequisite)#1921Imran Siddique (imran-siddique) merged 2 commits into
Imran Siddique (imran-siddique) merged 2 commits into
Conversation
The previous _ReadWriteLock acquired the inner threading.RLock on the first reader thread that took the count from 0 to 1, and released it on whichever reader thread later took the count back to 0. Under true parallel execution those are different threads, and RLock.release raises RuntimeError when called from a thread other than the one that acquired it. The bug was masked while AsyncPolicyEvaluator.evaluate held an asyncio.Lock for the duration of the executor call (forcing strict-serial evaluation), but it surfaces as soon as concurrent threads actually overlap inside _evaluate_with_read_lock. Replace the two nested RLocks with a single threading.Condition that counts active readers and writers. Acquire/release are no longer tied to any particular thread; readers and writers signal each other via notify_all when their counts drop to zero. This is a prerequisite for dropping the redundant asyncio.Lock from evaluate (REVIEW.md HIGH item #7) — landed first so the parallel- evaluation regression test in the next commit doesn't surface this latent bug intermittently. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
) AsyncPolicyEvaluator.evaluate held an asyncio.Lock for the duration of the run_in_executor call, fully serializing every async evaluation across the event loop. The lock served no correctness purpose — the underlying _ReadWriteLock and ConcurrencyStats updates are already thread-safe — but it forced concurrent ``await evaluate(...)`` calls to run one at a time, defeating the entire reason for using an executor. Drop the ``async with self._async_lock:`` wrapper so concurrent evaluations actually run in parallel. The asyncio.Lock is retained for reload_policies, where coroutine-level serialization across reload requests is still useful. Update the class- and module-level docstrings to reflect that the asyncio.Lock now applies to reloads only. Adds a regression test that runs 8 concurrent slow evaluations and asserts the recorded concurrent_peak exceeds 1, which can only hold when the executor pool is genuinely running them in parallel. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
🤖 AI Agent: breaking-change-detector — API CompatibilityAPI Compatibility
|
🤖 AI Agent: security-scanner — View detailsNo security issues found. |
🤖 AI Agent: code-reviewer — Action Items:TL;DR: 0 blockers, 1 warning. Fixes critical concurrency and thread-safety issues effectively.
Action Items:
Warnings:
|
🤖 AI Agent: test-generator — `agent_os/policies/async_evaluator.py`
|
🤖 AI Agent: docs-sync-checker — Docs SyncDocs Sync
|
|
🟡 Contributor Check: MEDIUM
Automated check by AGT Contributor Check. |
PR Review Summary
Verdict: |
Imran Siddique (imran-siddique)
approved these changes
May 10, 2026
Imran Siddique (imran-siddique)
left a comment
Collaborator
There was a problem hiding this comment.
LGTM — Condition-based RW lock fixes cross-thread RLock release; async lock removal is correct.
Imran Siddique (imran-siddique)
enabled auto-merge (squash)
May 10, 2026 20:14
Imran Siddique (imran-siddique)
merged commit May 10, 2026
03d630d
into
microsoft:main
13 of 14 checks passed
MohammadHaroonAbuomar
pushed a commit
to MohammadHaroonAbuomar/agt-acs
that referenced
this pull request
Jun 1, 2026
…read-safe RW lock prerequisite) (microsoft#1921) * fix(async-evaluator): make _ReadWriteLock writer release thread-safe The previous _ReadWriteLock acquired the inner threading.RLock on the first reader thread that took the count from 0 to 1, and released it on whichever reader thread later took the count back to 0. Under true parallel execution those are different threads, and RLock.release raises RuntimeError when called from a thread other than the one that acquired it. The bug was masked while AsyncPolicyEvaluator.evaluate held an asyncio.Lock for the duration of the executor call (forcing strict-serial evaluation), but it surfaces as soon as concurrent threads actually overlap inside _evaluate_with_read_lock. Replace the two nested RLocks with a single threading.Condition that counts active readers and writers. Acquire/release are no longer tied to any particular thread; readers and writers signal each other via notify_all when their counts drop to zero. This is a prerequisite for dropping the redundant asyncio.Lock from evaluate (REVIEW.md HIGH item microsoft#7) — landed first so the parallel- evaluation regression test in the next commit doesn't surface this latent bug intermittently. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(async-evaluator): drop redundant asyncio.Lock from evaluate (HIGH microsoft#7) AsyncPolicyEvaluator.evaluate held an asyncio.Lock for the duration of the run_in_executor call, fully serializing every async evaluation across the event loop. The lock served no correctness purpose — the underlying _ReadWriteLock and ConcurrencyStats updates are already thread-safe — but it forced concurrent ``await evaluate(...)`` calls to run one at a time, defeating the entire reason for using an executor. Drop the ``async with self._async_lock:`` wrapper so concurrent evaluations actually run in parallel. The asyncio.Lock is retained for reload_policies, where coroutine-level serialization across reload requests is still useful. Update the class- and module-level docstrings to reflect that the asyncio.Lock now applies to reloads only. Adds a regression test that runs 8 concurrent slow evaluations and asserts the recorded concurrent_peak exceeds 1, which can only hold when the executor pool is genuinely running them in parallel. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
AsyncPolicyEvaluator.evaluate(agent_os/policies/async_evaluator.py:172-175) wrapped itsrun_in_executorcall inasync with self._async_lock, fully serializing every async evaluation across the event loop. The lock served no correctness purpose — the underlying_ReadWriteLockandConcurrencyStatsupdates are already thread-safe — but it forced concurrentawait evaluate(...)calls to run one at a time, defeating the whole reason for using an executor.While building the regression test for that fix, the executor's actual concurrent execution exposed a latent thread-affinity bug in
_ReadWriteLock: it acquired the innerthreading.RLockwriter lock on the first reader thread that took the count from 0 to 1, and released it on whichever reader thread later took the count back to 0. With true parallel execution those are different threads, andRLock.releaseraisesRuntimeErrorfrom any thread other than the one that acquired it. The asyncio.Lock had been masking this by serializing everything to the same executor thread at any given moment.This PR is two commits: the prerequisite RW-lock fix, then the named HIGH item.
Commit 1 —
fix(async-evaluator): make _ReadWriteLock writer release thread-safeReplace the two nested
RLocks with a singlethreading.Conditionthat counts active readers and writers. Acquire/release are no longer thread-affine; readers and writers signal each other vianotify_allwhen their counts drop to zero. This is the standard readers-writer-lock pattern and unblocks landing the asyncio.Lock removal without intermittent crashes.Commit 2 —
fix(async-evaluator): drop redundant asyncio.Lock from evaluate (HIGH #7)async with self._async_lock:fromevaluate. The executor call now stands alone; concurrency is bounded by the executor pool size and the inner RW lock.reload_policies, where coroutine-level serialization across reload requests is still useful.Tests
Adds
test_async_evaluations_run_concurrently— 8 concurrentawait evaluate(...)calls against an evaluator artificially slowed by a 50ms sleep. Assertsconcurrent_peak > 1. Before this PR (with the asyncio.Lock holding the executor in serial),concurrent_peaktopped out at 1; after, it comfortably exceeds 1 in the default thread pool.(31 existing + 1 new.)
Test plan
test_async_evaluator.pytests pass on Python 3.14 (Windows).evaluate_syncorreload_policiessemantics.Reported via the AEGIS Initiative review of
microsoft/agent-governance-toolkit.Signed-off-by: Kenneth Tannenbaum ktannenbaum@aegis-initiative.com
🤖 Generated with Claude Code