Skip to content

fix: improve thread safety in tool registry#1184

Merged
MervinPraison merged 1 commit intomainfrom
claude/remaining-thread-safety-fixes
Mar 31, 2026
Merged

fix: improve thread safety in tool registry#1184
MervinPraison merged 1 commit intomainfrom
claude/remaining-thread-safety-fixes

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

This PR addresses remaining thread safety issues from the original PR #1182 that were NOT covered by the merged PRs #1175 and #1176.

Issues Fixed

1. Thread-unsafe tool registry access 🚨

Problem: Module-level functions remove_tool() and list_tools() bypassed thread-safe registry methods by directly accessing registry._tools and registry._functions dictionaries.

Solution:

  • remove_tool() now uses registry.unregister() (uses internal locking)
  • list_tools() now uses registry.list_tools() (uses internal locking)

2. Missing thread safety in registry singleton

Problem: get_registry() singleton creation lacked proper double-checked locking for concurrent initialization.

Solution: Added double-checked locking pattern with _registry_lock to prevent race conditions during singleton creation.

Code Changes

# BEFORE (NOT THREAD-SAFE):
def remove_tool(name: str) -> bool:
    registry = get_registry()
    if name in registry._tools:        # Direct access - race condition risk
        del registry._tools[name]
        return True
    # ...

# AFTER (THREAD-SAFE):  
def remove_tool(name: str) -> bool:
    return get_registry().unregister(name)  # Uses internal locking

Testing

  • βœ… Basic syntax validation passes
  • βœ… Thread-safe operations validated with test registration/removal
  • βœ… No breaking changes to public API

Context

This addresses the remaining issues from PR #1182 that were identified by code reviewers (Qodo, CodeRabbit) but not covered by the merged thread safety fixes in PRs #1175 and #1176.

Compliance with AGENTS.md

  • βœ… Protocol-driven: No heavy implementations added to core
  • βœ… Backward compatible: No public API changes
  • βœ… Performance: Minimal overhead, proper locking only where needed
  • βœ… Multi-agent safe: Eliminates race conditions in tool registry
  • βœ… DRY: Reuses existing thread-safe methods

Fixes remaining issues from #1167
Related to closed PR #1182

πŸ€– Generated with Claude Code

- 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>
@MervinPraison MervinPraison merged commit 0f5849b into main Mar 31, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant