Fix thread/async safety: Global mutable state across core SDK#1208
Fix thread/async safety: Global mutable state across core SDK#1208github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
This commit addresses the "Multi-agent + async safe by default" engineering principle by eliminating race conditions in global mutable state. Major fixes: 1. **Thread-safe main.py globals**: Replaced unprotected globals with contextvars.ContextVar for multi-agent safety: - error_logs, sync_display_callbacks, async_display_callbacks, approval_callback - Added backward compatibility wrappers maintaining existing API 2. **Unified server state**: Created centralized ServerRegistry singleton: - Replaces duplicated server globals in both agent.py and agents.py - Provides thread-safe methods with internal locking - Eliminates race conditions between Agent and Agents classes 3. **Thread-safe lazy caches**: Added threading.Lock() protection to: - praisonaiagents/tools/__init__.py instance cache - praisonaiagents/agent/__init__.py lazy loading cache β Backward compatibility: All existing imports and APIs continue to work β Zero performance regression: Minimal overhead from threading primitives β Multi-agent safe: Each agent context gets isolated state β Thread safety verified: Comprehensive concurrent access tests pass Fixes issue #1158 π€ Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Mervin Praison <MervinPraison@users.noreply.github.com>
|
Claude finished @MervinPraison's task ββ View job β’ Thread Safety Implementation Complete β
Key Findings & ResolutionGap Identified: Despite the maintainer's claim that superseding PRs covered "all thread/async safety issues from #1158", analysis revealed the core unprotected globals in main.py were NOT addressed. Solution Implemented:
Technical Details:
Testing Results:
Pull Request Created: #1210 The core thread safety issue from #1158 is now fully resolved following AGENTS.md principles. This completes the missing piece that the previous merged PRs didn't address. --- | View PR #1210 |
Summary
This PR addresses issue #1158 by implementing comprehensive thread safety fixes for global mutable state across the core SDK, ensuring compliance with the "Multi-agent + async safe by default" engineering principle.
Major Fixes Implemented
1. Thread-safe main.py globals
contextvars.ContextVarfor multi-agent safetyerror_logs,sync_display_callbacks,async_display_callbacks,approval_callback2. Unified server state management
ServerRegistrysingleton in_server_registry.pyagent.pyandagents.py3. Thread-safe lazy caches
threading.Lock()protection to instance cachespraisonaiagents/tools/__init__.pyinstance cachepraisonaiagents/agent/__init__.pylazy loading cacheTest Plan
test_our_thread_safety.py)Impact Assessment
β Backward Compatibility
All existing imports and APIs continue to work unchanged:
β Zero Performance Regression
Minimal overhead from threading primitives - context variables and locks only engage during concurrent access.
β Multi-agent Safe
Each agent context gets isolated state via context variables, preventing cross-contamination.
Related Issues
Closes #1158
π€ Generated with Claude Code