-
-
Notifications
You must be signed in to change notification settings - Fork 12
fix(consensus): replace non-reentrant Lock with RLock in CircuitBreaker (#332) #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Rahul Dass (rahuldass19)
merged 3 commits into
main
from
fix/circuit-breaker-self-deadlock-332
Aug 27, 2026
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ffb62ee
fix(consensus): replace non-reentrant Lock with RLock in CircuitBreaker
rahuldass19 38bb233
fix(consensus): address review feedback on CircuitBreaker lock synchr…
rahuldass19 bc3da53
test(consensus): bound deadlock probes in worker threads with timeout
rahuldass19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| """Regression test for Issue #332: CircuitBreaker self-deadlock fix.""" | ||
| import threading | ||
|
|
||
| from qwed_new.core.consensus_verifier import CircuitBreaker, ConsensusVerifier, EngineState | ||
|
|
||
|
|
||
| def test_circuit_breaker_record_success_no_deadlock(): | ||
| """record_success acquires lock and calls get_health() which re-enters lock.""" | ||
| cb = CircuitBreaker() | ||
| # Prior to fix with non-reentrant Lock, this deadlocks on the first call | ||
| cb.record_success("SymPy", latency_ms=12.5) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| health = cb.get_health("SymPy") | ||
| assert health.total_calls == 1 | ||
| assert health.consecutive_failures == 0 | ||
| assert health.avg_latency_ms == 12.5 | ||
|
|
||
|
|
||
| def test_circuit_breaker_record_failure_no_deadlock(monkeypatch): | ||
| """record_failure acquires lock and calls get_health() which re-enters lock.""" | ||
| clock = [100.0] | ||
| monkeypatch.setattr( | ||
|
Check warning on line 22 in tests/test_circuit_breaker_deadlock_332.py
|
||
|
rahuldass19 marked this conversation as resolved.
|
||
| "qwed_new.core.consensus_verifier.time.time", | ||
| lambda: clock[0], | ||
| ) | ||
| cb = CircuitBreaker(failure_threshold=3, recovery_time_seconds=1.0) | ||
|
|
||
| # 1st failure | ||
| cb.record_failure("Z3") | ||
| assert cb.get_health("Z3").consecutive_failures == 1 | ||
| assert cb.is_available("Z3") is True | ||
|
|
||
| # 2nd failure | ||
| cb.record_failure("Z3") | ||
| assert cb.get_health("Z3").consecutive_failures == 2 | ||
| assert cb.is_available("Z3") is True | ||
|
|
||
| # 3rd failure -> trips OPEN | ||
| cb.record_failure("Z3") | ||
| assert cb.get_health("Z3").state == EngineState.OPEN | ||
| assert cb.is_available("Z3") is False | ||
|
|
||
| # Advance clock past recovery threshold deterministically without time.sleep | ||
| clock[0] = 102.0 | ||
|
|
||
| # is_available transitions to DEGRADED | ||
| assert cb.is_available("Z3") is True | ||
| assert cb.get_health("Z3").state == EngineState.DEGRADED | ||
|
|
||
| # Success resets to HEALTHY | ||
| cb.record_success("Z3", latency_ms=5.0) | ||
| assert cb.get_health("Z3").state == EngineState.HEALTHY | ||
|
|
||
|
|
||
| def test_circuit_breaker_get_all_health_thread_safe(): | ||
| """get_all_health returns complete statistics without raising under concurrency.""" | ||
| cb = CircuitBreaker() | ||
| cb.record_success("SymPy", 10.0) | ||
| cb.record_failure("Python") | ||
|
|
||
| stats = cb.get_all_health() | ||
| assert "SymPy" in stats | ||
| assert "Python" in stats | ||
| assert stats["SymPy"]["calls"] == 1 | ||
| assert stats["Python"]["failures"] == 1 | ||
|
|
||
|
|
||
| def test_circuit_breaker_reset_thread_safe(): | ||
| """reset() clears engine statistics under lock.""" | ||
| cb = CircuitBreaker() | ||
| cb.record_success("SymPy", 10.0) | ||
| assert len(cb.get_all_health()) == 1 | ||
|
|
||
| cb.reset() | ||
| assert len(cb.get_all_health()) == 0 | ||
|
|
||
| verifier = ConsensusVerifier(enable_circuit_breaker=True) | ||
| verifier.circuit_breaker.record_success("Z3", 5.0) | ||
| assert len(verifier.get_engine_health()) == 1 | ||
| verifier.reset_circuit_breakers() | ||
| assert len(verifier.get_engine_health()) == 0 | ||
|
|
||
|
|
||
| def test_circuit_breaker_concurrent_access(): | ||
| """Verify concurrent threads recording success/failure do not deadlock.""" | ||
| cb = CircuitBreaker(failure_threshold=5, recovery_time_seconds=1.0) | ||
| errors = [] | ||
|
|
||
| def worker(engine: str): | ||
| try: | ||
| for _ in range(50): | ||
| cb.record_success(engine, 10.0) | ||
| cb.record_failure(engine) | ||
| cb.is_available(engine) | ||
| cb.get_all_health() | ||
| if engine.endswith("0"): | ||
| cb.reset() | ||
| except Exception as e: | ||
| errors.append(e) | ||
|
|
||
| threads = [ | ||
| threading.Thread(target=worker, args=(f"Engine-{i % 3}",)) | ||
| for i in range(10) | ||
| ] | ||
|
|
||
| for t in threads: | ||
| t.start() | ||
| for t in threads: | ||
| t.join(timeout=5.0) | ||
| assert not t.is_alive(), "Thread deadlocked in CircuitBreaker!" | ||
|
|
||
| assert not errors, f"Errors encountered during concurrent execution: {errors}" | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.