← Back to Pull requests

Reset Network.run state after callback errors #1881

Merged
Arthur031221 merged 2 files (57 lines) on Oct 15, 2026 08:50 UTC
View on GitHub
Arthur031221 Arthur031221 commented on Oct 03, 2026 15:32 UTC
Author

Stopping a simulation with multiple clocks can skip pending ticks on resume. When the stop time comes from a later clock, the next run advances the other clocks past their pending ticks.

The fix uses the earliest pending clock for the stop time. Tests cover stop and resume with and without profiling.

Tested with .venv/bin/python -m pytest -q brian2/tests/test_network.py -k stop_with_multiple_clocks.

Assisted by Claude/Codex.

Diff: 57 lines (+56 / -1) across 2 files Reviewer: mstimberg Review duration: 16877m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.34) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.34 · 57 lines diff across 2 files
0.34
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 4 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
91%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.243
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
5 items
Arthur031221 Arthur031221 commented on Oct 12, 2026 20:55 UTC
Author

Rebased onto master and trimmed the description to the remaining stop/resume fix. The stop-and-resume tests (with and without profiling) and the NumPy suite pass.

mstimberg mstimberg commented on code on Oct 14, 2026 14:58 UTC
Code Review
brian2/tests/test_network.py:1
@@ -1947,6 +1949,59 @@ def test_negative_duration_in_net_run():
         net.run(-1 * second)
 
 
+@pytest.mark.codegen_independent
+@pytest.mark.parametrize("profile", [False, True])
+@pytest.mark.parametrize("multiple_clocks", [False, True])
+def test_running_state_after_error(profile, multiple_clocks):
+    def fail():
+        raise RuntimeError("simulation failed")
+
+    objects = [NetworkOperation(fail, dt=0.1 * ms)]
+    if multiple_clocks:
+        objects.append(NetworkOperation(lambda: None, dt=0.2 * ms))
+    net = Network(objects)
+    handler = _InterruptHandler(signal.default_int_handler)
+    try:
+        with pytest.raises(RuntimeError, match="simulation failed"):
+            net.run(0.2 * ms, profile=profile)
+        with pytest.raises(KeyboardInterrupt):
+            handler(signal.SIGINT, None)
+        assert not Network._globally_running
+        assert not Network._globally_stopped
+    finally:
+        Network._globally_running = False
+        Network._globally_stopped = False

Could you please comment on what this tests, in particular with the interrupt handler?

Arthur031221 Arthur031221 commented on code on Oct 14, 2026 15:04 UTC
Code Review
brian2/tests/test_network.py:1
@@ -1947,6 +1949,59 @@ def test_negative_duration_in_net_run():
         net.run(-1 * second)
 
 
+@pytest.mark.codegen_independent
+@pytest.mark.parametrize("profile", [False, True])
+@pytest.mark.parametrize("multiple_clocks", [False, True])
+def test_running_state_after_error(profile, multiple_clocks):
+    def fail():
+        raise RuntimeError("simulation failed")
+
+    objects = [NetworkOperation(fail, dt=0.1 * ms)]
+    if multiple_clocks:
+        objects.append(NetworkOperation(lambda: None, dt=0.2 * ms))
+    net = Network(objects)
+    handler = _InterruptHandler(signal.default_int_handler)
+    try:
+        with pytest.raises(RuntimeError, match="simulation failed"):
+            net.run(0.2 * ms, profile=profile)
+        with pytest.raises(KeyboardInterrupt):
+            handler(signal.SIGINT, None)
+        assert not Network._globally_running
+        assert not Network._globally_stopped
+    finally:
+        Network._globally_running = False
+        Network._globally_stopped = False

It checks that a failed run does not leave Network._globally_running set. The handler call is there for the user-visible side of that: if the flag stays True after a callback raises, a later Ctrl+C is treated as a stop request for a run that is not happening, and the KeyboardInterrupt is swallowed. So the test calls the handler with SIGINT after the failed run and expects the KeyboardInterrupt to come through, then checks both flags are cleared.

It already passes on current master (4 passed with the fix reverted), since the try/finally from #1752 does the cleanup. It only guards that. With the finally removed, all 4 cases fail with "DID NOT RAISE KeyboardInterrupt". It is unrelated to the multi-clock fix, so I can drop it from this PR if you would rather keep the diff to the stop/resume change.

mstimberg mstimberg commented on code on Oct 14, 2026 15:25 UTC
Code Review
brian2/tests/test_network.py:1
@@ -1947,6 +1949,59 @@ def test_negative_duration_in_net_run():
         net.run(-1 * second)
 
 
+@pytest.mark.codegen_independent
+@pytest.mark.parametrize("profile", [False, True])
+@pytest.mark.parametrize("multiple_clocks", [False, True])
+def test_running_state_after_error(profile, multiple_clocks):
+    def fail():
+        raise RuntimeError("simulation failed")
+
+    objects = [NetworkOperation(fail, dt=0.1 * ms)]
+    if multiple_clocks:
+        objects.append(NetworkOperation(lambda: None, dt=0.2 * ms))
+    net = Network(objects)
+    handler = _InterruptHandler(signal.default_int_handler)
+    try:
+        with pytest.raises(RuntimeError, match="simulation failed"):
+            net.run(0.2 * ms, profile=profile)
+        with pytest.raises(KeyboardInterrupt):
+            handler(signal.SIGINT, None)
+        assert not Network._globally_running
+        assert not Network._globally_stopped
+    finally:
+        Network._globally_running = False
+        Network._globally_stopped = False

Thanks for the explanation. Ok, this is mostly what I understood myself, I was only confused because I did not see a connection to the change about the clock time. But happy to leave this test in, it's a good safeguard.

Reviewers
Assignees
Arthur031221 Arthur031221
Labels
risk: low confidence: 91.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 33.8% (LOW) · Higher = larger blast radius
33.8%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 91.5% · Lower = rushed or shallow review
91.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 2.9% · Safe to merge
2.9%