Skip to content

Stop stepping in ModelController once model.running is False - #4024

Merged
quaquel merged 1 commit into
mesa:mainfrom
ShreyanshuGhosh:fix/solara-step-respect-running
Nov 2, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
ShreyanshuGhosh:fix/solara-step-respect-running

Conversation

@ShreyanshuGhosh

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

When Render Interval is set above 1, the Step and Play buttons keep stepping the model after it has set running = False. A single click can run the model many steps past its end condition, so the plots and collected data include steps that should never have happened.

Bug / Issue

No separate issue, the bug is described here.

I came across this while reading through ModelController to understand how stepping works, and then confirmed it in the Schelling example. It affects any model that ends itself through model.running = False, for example when all agents are happy or a population dies out. With Render Interval above 1, the UI keeps calling step() until the full interval is used up, even though the model already reported it was done.

Reproducer

  1. solara run app.py in mesa/examples/basic/schelling
  2. Set Homophily to 0 (every agent is happy immediately, so the model stops after 1 step)
  3. Set Render Interval to 100, click Reset, then click Step once
  4. Expected: Time 1.0. Actual on main: Time 100.0

Cause
In ModelController.do_step() the loop copies model.running into the running reactive on every iteration, but never breaks on it. The Play branch only breaks when playing turns off; the Step branch never breaks.

The running check was added in 4e45300, when do_step advanced a single step. The render_interval loop came later (#2596) and the check was not carried into it.

Implementation

Break out of the loop in both branches as soon as running.value is False. force_update() still runs after the loop, so the UI shows the final state and the buttons get disabled as before.

Testing

  • Added test_step_button_stops_when_model_stops_running in tests/visualization/test_solara_viz.py. It renders ModelController with render_interval=10, clicks Step on a model that stops after 3 steps, and checks that step() ran exactly 3 times. Fails on main (assert 10 == 3), passes with this change.
  • Full suite: pytest tests/ -Werror -Wdefault::PendingDeprecationWarning → 1022 passed. ruff check passes.
  • Manual check in the Schelling example (Homophily 0, Render Interval 100, Reset, one Step click):

Before (main): Time 100.0

pr_before_main

After (this PR): Time 1.0

pr_after_fix

Additional Notes

I used Claude Code while learning the Mesa codebase. It helped me find this bug, check that no existing issue covers it, and write the test . I reproduced the bug in the Schelling example myself, reviewed the changes, and ran the full test suite locally.

@github-actions github-actions Bot added testing Release notes label visualization labels Oct 30, 2026
@quaquel quaquel added the bug Release notes label label Nov 2, 2026
@quaquel
quaquel merged commit b0c4ffd into mesa:main Nov 2, 2026
18 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants