perf: Replace exclusive lock with ReaderWriterLockSlim in ModuleScheduler#1469
Conversation
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…uler Refactor ModuleScheduler locking strategy to reduce contention in the hot execution path. The previous implementation held an exclusive lock during read-only operations like querying module states and statistics. Changes: - Replace `object _stateLock` with `ReaderWriterLockSlim` - Use read locks for query operations (GetStatistics, GetPendingModules, GetExecutingModules, ShouldExitScheduler snapshot creation) - Use write locks for state mutations (MarkModuleStarted, MarkModuleCompleted, AddModule, FindReadyModules, CancelPendingModules) - Move LINQ processing and logging operations outside of locks where possible - Add proper disposal of the ReaderWriterLockSlim This allows concurrent reads while maintaining exclusive access for writes, improving performance in scenarios with many parallel module queries. Fixes #1458 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
SummaryReplaces exclusive lock with ReaderWriterLockSlim in ModuleScheduler to reduce contention by allowing concurrent reads. Critical Issues1. Race condition in
|
1. ShouldExitScheduler: Keep read lock held while evaluating exit conditions to prevent state changes between snapshot and decision. The isDeadlocked check is now captured under the read lock before upgrading to write lock. 2. CancelPendingModules: Fix copy-paste bug where logging statement was hardcoded to log ModuleExecutionState.Completed instead of the original state before modification. Now captures original state before modifying and logs that value. 3. AddModule: Move dependency resolution inside the write lock to prevent race conditions where _moduleStates could change between resolution and processing. Logging of unregistered dependencies is done outside the lock to avoid holding the lock during I/O. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
SummaryThis PR refactors ModuleScheduler to use ReaderWriterLockSlim instead of an exclusive lock, enabling concurrent reads while maintaining exclusive writes for better performance. Critical IssuesNone found ✅ All three critical issues from the previous review have been properly addressed in commit a004751:
Suggestions
Previous Review StatusAll critical issues from the previous review (race conditions and logging bug) have been properly addressed in commit a004751. Verdict✅ APPROVE - No critical issues, all previous concerns resolved |
Summary
ModuleSchedulerlocking strategy to reduce contention in the hot execution pathobject _stateLockwithReaderWriterLockSlimto allow concurrent readsReaderWriterLockSlimThis allows multiple threads to read scheduler state concurrently while maintaining exclusive access for writes, improving performance in scenarios with many parallel module queries.
Fixes #1458
Test plan
🤖 Generated with Claude Code