fix(concurrency): add thread safety to InvariantService #11001

Open
HAL9000 wants to merge 3 commits from fix/invariant-service-thread-safety into master

3 Commits

Author SHA1 Message Date
controller-ci-rerun 421d073ba9 chore: re-trigger CI [controller]
CI / lint (pull_request) Failing after 45s
CI / typecheck (pull_request) Successful in 1m17s
CI / security (pull_request) Successful in 1m7s
CI / helm (pull_request) Successful in 35s
CI / build (pull_request) Successful in 47s
CI / push-validation (pull_request) Successful in 31s
CI / quality (pull_request) Successful in 1m2s
CI / unit_tests (pull_request) Failing after 1m50s
CI / coverage (pull_request) Has been skipped
CI / docker (pull_request) Has been skipped
CI / integration_tests (pull_request) Failing after 4m14s
CI / status-check (pull_request) Failing after 3s
2026-06-12 23:43:11 -04:00
HAL9000 4907a0e2ad fix(concurrency): fix Behave step definitions and logic bug in InvariantService thread safety tests
CI / security (pull_request) Failing after 3s
CI / unit_tests (pull_request) Failing after 3s
CI / lint (pull_request) Failing after 3s
CI / typecheck (pull_request) Failing after 4s
CI / quality (pull_request) Failing after 4s
CI / coverage (pull_request) Has been skipped
CI / docker (pull_request) Has been skipped
CI / integration_tests (pull_request) Failing after 2s
CI / build (pull_request) Failing after 3s
CI / helm (pull_request) Failing after 3s
CI / push-validation (pull_request) Failing after 4s
CI / status-check (pull_request) Failing after 4s
Fixed 5 blocking issues identified during peer review:

1. Renamed ambiguous variable 'l' to 'list_threads' (ruff E741) to resolve lint failure.
2. Added missing @given step definition for "N invariants pre-added to the service"
   (matched scenario line in feature file).
3. Added missing @given step definition for "N invariants pre-added to the service from
   different scopes" (matched concurrent enforce scenario).
4. Added short @then handler "the service invariant count should be non-negative"
   to match singleton safety scenario; existing long handler covers other needs.
5. Fixed scope expression logic bug in step_when_concurrent_mixed: replaced broken
   integer ternary with proper enum cycling [GLOBAL, PROJECT, PLAN][thread_idx % 3].

Closes #7524
2026-05-15 01:44:04 +00:00
HAL9000 dbccf6e077 fix(concurrency): add thread safety to InvariantService
Add a threading.RLock (re-entrant lock) to InvariantService that guards
all shared mutable state (_invariants dict and _enforcement_records list).
Every public method acquires the lock before reading or writing shared data.

This prevents RuntimeError: dictionary changed size during iteration and
data corruption under concurrent plan execution when the service is registered
as a DI Singleton shared across threads.

PR Compliance Checklist:
- [x] CHANGELOG.md - added entry under [Unreleased]/Fixed section
- [x] CONTRIBUTORS.md - updated HAL 9000 contribution with thread safety mention
- [x] BDD tests - new feature file and step definitions for thread safety (7 scenarios)

ISSUES CLOSED: #8209
2026-05-15 01:44:04 +00:00