fix: prevent time rewind in _advance_time by adding early return#3329
Merged
fix: prevent time rewind in _advance_time by adding early return#3329
Conversation
|
Performance benchmarks:
|
Member
Note that I am working on a bigger PR with a larger set of bug fixes and other improvements for |
Contributor
Author
8a2d75e to
fff3bfe
Compare
Contributor
Author
quaquel
reviewed
Feb 17, 2026
quaquel
approved these changes
Feb 17, 2026
Member
quaquel
left a comment
There was a problem hiding this comment.
1 minor change still open. I will merge afterwards
EwoutH
approved these changes
Feb 17, 2026
quaquel
reviewed
Feb 17, 2026
quaquel
reviewed
Feb 17, 2026
quaquel
reviewed
Feb 17, 2026
quaquel
reviewed
Feb 17, 2026
Krishsharma179
added a commit
to Krishsharma179/mesa
that referenced
this pull request
Feb 21, 2026
EwoutH
pushed a commit
that referenced
this pull request
Mar 15, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes critical bug where model.run_until() incorrectly rewinds simulation time when target time is less than current time. Time must be monotonic in simulations (never decrease) to maintain causality and data integrity.
Bug / Issue
Related Issue: #3327
Context: When calling model.run_until(target) where target < model.time, the method overwrites model.time = target, causing time to jump backward.
Expected: Time should remain unchanged (no-op) when target is in the past.
Actual: Time rewinds (e.g., run_for(10) → time=10, then run_until(5.0) → time=5.0 (wrong)).
Implementation
Modified mesa/model.py in _advance_time() method:
Testing
output:
✅ Time remains 10.0 after run_until(5.0) (no rewind)
✅ Forward advancement to 15.0 works correctly
✅ All existing Mesa tests pass (verified locally)
If you're fixing the visualisation, add before/after screenshots. -->
Additional Notes
Minimal change: Only 2 lines added, no logic modifications elsewhere
Critical for: Time-series data integrity, agent state consistency, reproducibility
No side effects: Guard clause exits early before any event processing
Standards compliance: Aligns with universal simulation principle: "Time never decreases"
Future-proof: Prevents subtle bugs in complex models using event scheduling