Skip to content

Commit 9bb149a

Browse files
fix: improve thread safety in tool registry
- Use thread-safe registry.unregister() in remove_tool() - Use thread-safe registry.list_tools() in list_tools() - Add double-checked locking to get_registry() singleton - Prevents race conditions in multi-agent deployments Related to #1167 and closed PR #1182 Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent 6a90cc0 commit 9bb149a

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

src/praisonai-agents/praisonaiagents/tools/registry.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,19 @@ def __repr__(self) -> str:
249249
return f"ToolRegistry(tools={len(self._tools)}, functions={len(self._functions)})"
250250

251251

252-
# Global registry instance
252+
# Global registry instance (protected by _registry_lock for thread safety)
253+
_registry_lock = threading.Lock()
253254
_global_registry: Optional[ToolRegistry] = None
254255

255256

256257
def get_registry() -> ToolRegistry:
257-
"""Get the global tool registry instance."""
258+
"""Get the global tool registry instance. Thread-safe singleton."""
258259
global _global_registry
259260
if _global_registry is None:
260-
_global_registry = ToolRegistry()
261+
with _registry_lock:
262+
# Double-checked locking pattern
263+
if _global_registry is None:
264+
_global_registry = ToolRegistry()
261265
return _global_registry
262266

263267

@@ -309,14 +313,7 @@ def remove_tool(name: str) -> bool:
309313
Returns:
310314
True if tool was found and removed, False otherwise
311315
"""
312-
registry = get_registry()
313-
if name in registry._tools:
314-
del registry._tools[name]
315-
return True
316-
if name in registry._functions:
317-
del registry._functions[name]
318-
return True
319-
return False
316+
return get_registry().unregister(name)
320317

321318

322319
def list_tools() -> List[str]:
@@ -325,8 +322,7 @@ def list_tools() -> List[str]:
325322
Returns:
326323
List of tool names
327324
"""
328-
registry = get_registry()
329-
return list(registry._tools.keys()) + list(registry._functions.keys())
325+
return get_registry().list_tools()
330326

331327

332328
def discover_plugins() -> int:

0 commit comments

Comments
 (0)