Skip to content

Fix run_until skipping events scheduled for the current time - #4048

Merged
quaquel merged 3 commits into
mesa:mainfrom
atul-upadhyay-7:fix-run-until-current-time
Nov 9, 2026
Merged

quaquel merged 3 commits into
mesa:mainfrom
atul-upadhyay-7:fix-run-until-current-time

Conversation

@atul-upadhyay-7

@atul-upadhyay-7 atul-upadhyay-7 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

run_until(model.time) skips events scheduled for the current time: it warns that the end time "is not larger than current time" and returns, so the event only fires one step later. schedule_event accepts an event at the current time (it only rejects times in the past), so run_until should process it.

Bug / Issue

Fixes #4047

The early return in Model._advance_time (if until <= self.time:) warns and returns before the event loop runs, so an event due exactly at until == self.time is skipped. That guard was added in #3329 for #3327, but #3327 was about run_until going backward (until < time); the equality case looks unintended.

Implementation

Changed the guard in _advance_time (mesa/model.py) from until <= self.time to until < self.time, so events due exactly at the end time are processed and the warning only fires when the end time is actually in the past.

Testing

  • Added a regression test (test_events_due_at_end_time_run in tests/test_schedule_run.py): it fails on main and passes with this change.
  • Added test_negative_duration in tests/test_schedule_run.py to cover the existing warning path in _advance_time.
  • The reproducer from run_until at the current time skips events scheduled for that time #4047 now fires the event at the current time with no warning.
  • Full test suite passes (tests/visualization and tests/examples excluded; they need the optional solara/playwright dependencies). Python 3.12.15, Ubuntu 22.04.

Additional Notes

None.

@github-actions github-actions Bot added the testing Release notes label label Nov 7, 2026
@atul-upadhyay-7
atul-upadhyay-7 force-pushed the fix-run-until-current-time branch from 7f831b9 to d5e3ccd Compare November 7, 2026 11:40
@atul-upadhyay-7

Copy link
Copy Markdown
Contributor Author

@quaquel ready for a review

@quaquel

quaquel commented Nov 7, 2026

Copy link
Copy Markdown
Member

When introducing the current implementation of event scheduling in mesa 3.5, we had a long discussion on time. I don't have the time atm to look it up but should be findable quite easily. I would appreciate checking and linking to that this discussion.

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor Author

@quaquel found it, it's this one: #2921

read through it. they settled on keeping run_for/run_until simple, stopping conditions go in run_while, and no heartbeat. the case where until equals the current time never came up.

from the code: schedule_event allows an event at the current time, and run_until only complains when end_time is before model.time. the <= check in _advance_time (from #3329, for the rewind bug #3327) is the only thing blocking it. so this pr just changes that check to <.

@EwoutH

EwoutH commented Nov 7, 2026

Copy link
Copy Markdown
Member

Thanks for reaching out and opening a PR. I think you’re right and events scheduled now should be executed without moving time forward. The order and mechanics could be difficult though.

@quaquel, do you agree?

@quaquel

quaquel commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

@quaquel found it, it's this one: #2921

That is not exactly the discussion I had in mind. I tried to find it myself but haven't succeeded so far. Anyway, at some point we discussed when to advance time: before executing an event or after executing an event. I think we agree on before the event and that is also what the code does.

run_until(model.time) skips events scheduled for the current time: it warns that the end time "is not larger than current time" and returns,

So if we advance time before executing, run_until(model.time) should just execute as long as there are outstanding events for the current time. None of these events have a timestamp that is larger than model.time and so should execute.

So yes, I agree with @EwoutH. I also think that the proposed fix is the correct one (and bring the error message in line with the code, which currently is not the case.

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor Author

thanks @quaquel, updated both warnings to say the end time is earlier than the current time. added message checks to the existing tests too.

@AshNicolus AshNicolus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the mechanics against main, since that was the open question. With the model at t=3: an event at now fires under both run_until(now) and run_for(0); an event at now that schedules another at now fires both, in order; a schedule_recurring starting at now fires once and keeps its next at 4; the step at 3 doesn't run again; run_until(now) with nothing queued is now a silent no-op; and on a fresh model an event at 0 fires with run_until(0) without triggering a step. time doesn't emit CHANGED (same value), so a DataRecorder gets no extra snapshot, and its final row at 3 now shows the event's effect. The 3 new tests fail on main, and the full suite passes here (1105).

flowchart LR
    A["run_until(3) at time 3<br/>event queued at 3"] --> B{"_advance_time:<br/>until vs time"}
    B -->|"main: until <= time"| C["warn + return<br/>event waits until t=4"]
    B -->|"PR: until < time"| D["event loop runs"]
    D --> E["event at 3 fires<br/>time stays 3"]
Loading
flowchart TD
    S["run_until(now)"] --> G{"until < time?"}
    G -->|yes| W["warn: end time is earlier<br/>than current time"]
    G -->|no| P["pop next event"]
    P --> Q{"event.time <= until?"}
    Q -->|yes| X["time = event.time (unchanged)<br/>execute"]
    X -->|"may schedule more at now"| P
    Q -->|no| R["push back, stop"]
    R --> T["time = until<br/>(same value: no CHANGED signal)"]
    T --> E["RUN_ENDED: recorder final row"]
Loading
Case (model at t=3) main this PR
event at now, run_until(now) not fired, warning fired at 3
event at now, run_for(0) not fired, warning fired at 3
event at now schedules another at now nothing fires both fire, in order
no events, run_until(now) warning silent no-op
fresh model, event at 0, run_until(0) not fired fired at 0, no step
schedule_recurring starting at now not fired fires at 3, next stays at 4
DataRecorder row at 3 after event sets value = 99 0 99

Side note, not from this PR: model.time is annotated float, but run_until(7) leaves it as int 7 already on main, and with this change run_until(3) at 3.0 also turns it into 3. Maybe worth a float(until) in a follow-up. LGTM.

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor Author

@AshNicolus saw #4052, thanks for picking that up. it covers the int time issue separately, so i'll keep this PR focused on events at the current time.

@EwoutH

EwoutH commented Nov 8, 2026

Copy link
Copy Markdown
Member

Something I’m thinking about: should we add a keyword and make this configurable?

I was thinking of including, defaulting to true, which means it runs up to and including. Turning it to false makes it just run up to.

Would this make sense? Are there use cases for it?

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor Author

@EwoutH stopping before a boundary event could help when a parameter needs to be changed from outside the model. scheduled changes can already use event priorities. one detail is that leaving events pending while advancing time would still trigger RUN_ENDED, so a recorder can write the state before those events run. i'd keep the default true and discuss the option in a follow-up, so we can agree on that behavior without widening this fix. would that work?

@quaquel
quaquel merged commit c9600b8 into mesa:main Nov 9, 2026
16 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
* fix: process events due at the current time in run_until

* fix: align time advancement warnings with the guards

---------

Co-authored-by: Jan Kwakkel <[email protected]>
(cherry picked from commit c9600b8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

run_until at the current time skips events scheduled for that time

4 participants