mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
- Change threading.Lock() to threading.RLock() in ResourceScheduler.__init__ - Fixes deadlock in allocate_resources() when it calls allocate_cpu/memory/gpu - Each allocate_* method also acquires the same lock, causing re-entrancy issue - RLock allows same thread to re-enter lock without blocking itself - Resolves build_knowledge_base() hanging indefinitely Test fixes and improvements: - Add allocation validation to prevent silent failures - Move progress tracking updates outside lock for better performance - Add comprehensive regression tests - Add explanatory comment for RLock usage Addresses Qodo review concerns: ✅ Silent allocation failure - now raises ValidationError ✅ Lock held during progress updates - moved outside lock ✅ Deadlock prevention - RLock allows re-entrant acquisition Resolves: #299
1.9 KiB
1.9 KiB
Test Fixes and Additional Improvements
I've enhanced this PR with comprehensive test fixes and additional improvements to address the Qodo review concerns:
🔧 Test Fixes
- Fixed isinstance() TypeError in test suite
- Updated validation tests to match actual behavior (partial allocations are allowed)
- Added comprehensive test coverage (6/6 tests passing)
- Added regression test for complete resource allocation failure
✅ Additional Improvements
- Allocation Validation: Added
ValidationErrorwhen absolutely no resources can be allocated - Performance Optimization: Moved progress tracking updates outside the lock to reduce lock hold time
- Documentation: Added explanatory comment for RLock usage
- Comprehensive Testing: Full test suite in
tests/test_resource_scheduler_deadlock.py
🧪 Test Results
============================== 6 passed in 1.24s ==============================
test_rlock_prevents_deadlock PASSED
test_allocation_validation_insufficient_resources PASSED
test_allocation_validation_no_resources PASSED
test_allocation_validation_partial_failure PASSED
test_reentrant_lock_behavior PASSED
test_gpu_allocation_with_rlock PASSED
🎯 Qodo Review Concerns Addressed
- ✅ Silent allocation failure: Now raises
ValidationErrorwhen no resources allocated - ✅ Lock held during progress updates: Progress tracking moved outside lock
- ✅ Deadlock prevention: RLock allows re-entrant lock acquisition
📋 Verification
- Deadlock Fix: ✅ Verified -
build_knowledge_base()completes without hanging - Resource Allocation: ✅ Working correctly with proper validation
- Performance: ✅ Improved with reduced lock contention
- Backward Compatibility: ✅ Maintained
The PR now provides a robust solution that not only fixes the deadlock but also improves overall reliability and testability.