fix(tools): add thread-safe lock to MCPManager singleton#845
Open
abdelhadi703 wants to merge 1 commit intoQwenLM:mainfrom
Open
fix(tools): add thread-safe lock to MCPManager singleton#845abdelhadi703 wants to merge 1 commit intoQwenLM:mainfrom
abdelhadi703 wants to merge 1 commit intoQwenLM:mainfrom
Conversation
The MCPManager singleton uses __new__ without a mutex lock. In a multi-threaded environment, two threads can simultaneously evaluate 'cls._instance is None' before either creates the instance, resulting in two instances being created and breaking the singleton pattern. Add threading.Lock() to ensure thread-safe singleton creation. Fixes QwenLM#812
d957c23 to
d01ff2a
Compare
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
Add thread-safety to the
MCPManagersingleton pattern.Problem: The singleton used
__new__without a mutex lock. In multi-threaded environments, two threads can simultaneously evaluatecls._instance is Nonebefore either creates the instance, resulting in two instances being created and breaking the singleton pattern.Fix: Add
threading.Lock()as a class variable and wrap the instance creation in awith cls._lock:block.Changes
qwen_agent/tools/mcp_manager.py:_lock = threading.Lock()class variablewith cls._lock:Security/Reliability
threadingis part of Python stdlibthreadingis already imported in the fileContribution by abdelhadisalmaoui0909@outlook.fr