fix(concurrency): add thread safety to ContextTierService (#7547) #8279

Merged
HAL9000 merged 2 commits from bugfix/issue-7547-context-tier-service-thread-safety into master 2026-04-15 00:47:30 +00:00

2 Commits

Author SHA1 Message Date
HAL9000 b43ba41f6d fix(concurrency): protect validate_fragment_scope with lock and update CONTRIBUTORS
CI / push-validation (pull_request) Successful in 20s
CI / helm (pull_request) Successful in 24s
CI / build (pull_request) Successful in 27s
CI / lint (pull_request) Successful in 29s
CI / quality (pull_request) Successful in 44s
CI / typecheck (pull_request) Successful in 54s
CI / security (pull_request) Successful in 55s
CI / e2e_tests (pull_request) Successful in 3m4s
CI / unit_tests (pull_request) Successful in 9m51s
CI / integration_tests (pull_request) Successful in 9m53s
CI / docker (pull_request) Successful in 1m31s
CI / coverage (pull_request) Successful in 16m31s
CI / status-check (pull_request) Successful in 2s
CI / quality (push) Successful in 17s
CI / push-validation (push) Successful in 17s
CI / helm (push) Successful in 25s
CI / build (push) Successful in 26s
CI / lint (push) Successful in 36s
CI / typecheck (push) Successful in 46s
CI / security (push) Successful in 50s
CI / e2e_tests (push) Successful in 3m19s
CI / integration_tests (push) Successful in 4m23s
CI / unit_tests (push) Successful in 5m7s
CI / docker (push) Successful in 1m19s
CI / coverage (push) Successful in 11m0s
CI / status-check (push) Successful in 1s
- Wrapped validate_fragment_scope() body with self._lock to prevent
  RuntimeError: dictionary changed size during iteration when another
  thread mutates the tier stores during scope validation
- Updated CONTRIBUTORS.md to document HAL 9000's concurrency safety
  contributions including thread-safe context tier management (issue #7547)

Fixes review feedback from PR #8279.

ISSUES CLOSED: #7547
2026-04-15 00:27:51 +00:00
HAL9000 0f75429253 fix(concurrency): add thread safety to ContextTierService
Implemented thread-safety improvements for ContextTierService by
introducing a re-entrant lock and guarding all critical sections
with self._lock. This prevents RuntimeError: dictionary changed
size during iteration under concurrent plan execution.

- Added threading.RLock to ContextTierService.__init__ as self._lock
- Wrapped all public methods (store, get, promote, demote, evict_lru,
  get_metrics, get_all_fragments, get_hot_fragments, get_for_actor,
  get_scoped_view) with with self._lock:
- Added _lock: threading.RLock type stub to TierRuntimeMixin and
  ScopedTierMixin
- Wrapped enforce_staleness in TierRuntimeMixin with self._lock
- Wrapped get_scoped_by_resource and get_scoped_metrics in
  ScopedTierMixin with self._lock
- Extracted settings helpers to new context_tier_settings.py to keep
  context_tiers.py under 500 lines
- Added BDD feature file context_tier_thread_safety.feature with
  10 thread-safety scenarios
- Added step definitions context_tier_thread_safety_steps.py
- Updated CHANGELOG.md with fix entry

ISSUES CLOSED: #7547
2026-04-15 00:24:13 +00:00