Fix: Complete thread safety for global mutable state (#1158)#1211
Closed
github-actions[bot] wants to merge 1 commit intomainfrom
Closed
Fix: Complete thread safety for global mutable state (#1158)#1211github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
- Replace unprotected globals in main.py with context variables for multi-agent safety - Add unified ServerRegistry to eliminate duplicate server state management - Maintain full backward compatibility with existing APIs - Use fallback thread-safe globals when context variables unavailable - Eliminate race conditions between Agent and Agents classes Fixes #1158 π€ Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
Owner
Contributor
Author
|
Claude encountered an error ββ View job Thread Safety Analysis Complete - Issues Confirmed βTasks:
β Thread Safety Violations Still PresentDespite claims that issue #1158 was "fully resolved," my analysis confirms the core problems remain unaddressed: 1. Unprotected Global State in main.py:27-34 error_logs = [] # β Global list - no thread protection
sync_display_callbacks = {} # β Global dict - no thread protection
async_display_callbacks = {} # β Global dict - no thread protection
approval_callback = None # β Global var - no thread protection2. Duplicate Server State Management
Multiple agents using both classes can conflict when sharing ports. Implementation PlanFollowing AGENTS.md guidelines, I'll implement:
Proceeding with implementation... |
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
This PR implements comprehensive thread safety fixes for global mutable state across the core SDK, addressing the remaining thread safety violations identified in issue #1158.
Major Changes
π‘οΈ Thread-safe main.py globals
error_logs,sync_display_callbacks,async_display_callbacks, andapproval_callbackwithcontextvars.ContextVarthreading.Lockwhen no context availableποΈ Unified ServerRegistry
ServerRegistrysingleton to replace duplicate server stateAgentandAgentsclassesregister_server(),mark_started(),register_agent_endpoint()β Zero performance regression
Testing
β Thread safety verified - comprehensive concurrent access tests pass
β Context isolation - multiple agents get separate callback/error state
β ServerRegistry - concurrent server registration without conflicts
β Backward compatibility - all existing imports and APIs unchanged
Compliance
Files Changed
praisonaiagents/main.py- Context variable based thread safetypraisonaiagents/agent/agent.py- Integrated with ServerRegistrypraisonaiagents/agents/agents.py- Integrated with ServerRegistrypraisonaiagents/core/server_registry.py- New unified server state managementFixes #1158
π€ Generated with Claude Code