Repository navigation
fix(core): serialize concurrent agent instantiation per ID - #8353
Open
Winoren (Winoren) wants to merge 1 commit into
Open
Winoren (Winoren) wants to merge 1 commit into
Winoren (Winoren) wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Why are these changes needed?
SingleThreadedAgentRuntime._get_agent()checks its instance cache before awaiting the registered factory, then stores the result. If an async factory yields, concurrent lookups of the sameAgentIdall miss the cache and each construct a separate agent. Earlier returned instances are then replaced in the cache, splitting agent state and leaving those instances outside the runtime's normal lifecycle tracking.Serialize construction per
AgentIdwith an asyncio lock and recheck the cache after acquiring it. Different agent IDs can still initialize concurrently. Factory exceptions or cancellation release the lock, allowing a later lookup to retry; successful lookups continue to use the existing cache fast path.Regression tests use the real runtime and agent factories to check:
Related issue number
Found while inspecting the current runtime implementation; no linked issue.
Validation
Based on upstream
027ecf0a379bcc1d09956d46d12d44a3ad9cee14, using Python 3.11 in an isolateduvenvironment with editable core/test-utils packages:python -m pytest python/packages/autogen-core/tests/test_runtime.py python/packages/autogen-core/tests/test_closure_agent.py python/packages/autogen-core/tests/test_cancellation.py python/packages/autogen-core/tests/test_intervention.py -q: 28 passed.--config-file python/pyproject.toml --follow-imports=silenton both changed files: passed.git diff --check: passed.Full workspace checks, documentation builds and distributed-runtime integration tests were not run. This change addresses instance construction in the single-threaded runtime; it does not serialize message-handler execution.
Checks
Prepared with Codex assistance.