Skip to content

Don't schedule an EventGenerator's first event past schedule.end - #4084

Merged
EwoutH merged 3 commits into
mesa:mainfrom
RITVIKKAMASETTY:fix/schedule-end-before-first-event
Nov 15, 2026
Merged

EwoutH merged 3 commits into
mesa:mainfrom
RITVIKKAMASETTY:fix/schedule-end-before-first-event

Conversation

@RITVIKKAMASETTY

@RITVIKKAMASETTY RITVIKKAMASETTY commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Note

Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.

Pre-PR Checklist

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

Summary

A recurring event scheduled with Schedule(end=...) and no start could fire once after end. This PR makes EventGenerator.start() respect end for the first event, so the generator never fires outside its window.

Bug / Issue

Fixes #4083

When schedule.start is None, EventGenerator.start() schedules the first event at model.time + interval without checking it against schedule.end. Every later event goes through _should_stop(), and resume() does the same check, but the first event in start() did not. So, for example, starting Schedule(interval=5, end=12) at t=10 fired the callback at t=15.

Implementation

In EventGenerator.start(), after the first event time is computed, call self._should_stop(start_time). If it returns True, return without activating the generator, registering it with the model, or scheduling an event. This matches how resume() handles the same situation.

The check also covers count. execution_count isn't reset by stop()/start(), so restarting a generator whose count is already used up now leaves it inactive. Before, it fired one extra time past count. Restarting after stop() before the count is reached still runs the remaining executions.

Testing

Additional Notes

No public API changes. schedule_recurring() still returns the generator. In this case it is just inactive (is_active == False, next_scheduled_time is None).

I used an AI assistant to help me check if my fix was correct

@github-actions github-actions Bot added the testing Release notes label label Nov 13, 2026
When schedule.start is None, EventGenerator.start() schedules the first
event at model.time + interval without checking it against schedule.end.
If that time is already past end, the generator fires once outside its
window. Check _should_stop() before scheduling the first event, matching
what resume() already does.

Fixes mesa#4083
@RITVIKKAMASETTY
RITVIKKAMASETTY force-pushed the fix/schedule-end-before-first-event branch from 8e4685b to b726f08 Compare November 13, 2026 12:19

@arzaanxeng arzaanxeng 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.

The fix looks correct overall. However, I noticed a small edge case regarding _should_stop().

The PR description mentions that the count condition cannot trigger inside start(). This may not always be true.

If a generator has already reached its execution count and becomes inactive, calling start() again will trigger the new _should_stop(start_time) check because _execution_count is not reset.

This appears to be reasonable behavior, but it would be useful to add a regression test for restarting a generator after its execution count has been exhausted and clarify this behavior in the PR description.

@RITVIKKAMASETTY

Copy link
Copy Markdown
Contributor Author

The fix looks correct overall. However, I noticed a small edge case regarding _should_stop().

The PR description mentions that the count condition cannot trigger inside start(). This may not always be true.

If a generator has already reached its execution count and becomes inactive, calling start() again will trigger the new _should_stop(start_time) check because _execution_count is not reset.

This appears to be reasonable behavior, but it would be useful to add a regression test for restarting a generator after its execution count has been exhausted and clarify this behavior in the PR description.

Thanks, good catch. You're right that the description was wrong about that.

I checked the behavior before and after. On main, if a generator with count=2 runs out and you call start() again, it fires a third time (execution_count becomes 3), so it goes past its own count. With this change, _should_stop(start_time) sees that the count is already reached, and the generator stays inactive without firing. I think that's the correct behavior, since count is a limit on total executions and _execution_count isn't reset on restart.

Restarting after stop() before the count is reached still works as before: the remaining executions run.

I'll add a regression test for restarting after the count is exhausted, and update the description to cover both cases.

execution_count is kept across stop/start, so the _should_stop() check in
start() also keeps a generator whose count is used up from firing again.
Before the fix it fired one extra time past count. Also cover restarting
after stop() before count is reached, which still runs the remaining
executions.
@EwoutH

EwoutH commented Nov 14, 2026

Copy link
Copy Markdown
Member

Thanks, checked on 670033e: the #4083 reproducer now gives [] (main: [15.0]), and an exhausted count=2 generator no longer fires a third time when restarted. A silent no-op when there's nothing left to schedule is fine; it matches resume().

Two small requests:

  • Trim the tests to the two regression cases (first event after end, restart after count is exhausted), each asserting that it didn't fire and isn't active. Please don't assert on model._event_generators. test_restart_after_stop_runs_remaining_count passes on main, so it can go.
  • The inline comment can be one line, or dropped.

Whether start() after stop() should reset count is a separate question, which I left in #2921.

@RITVIKKAMASETTY

Copy link
Copy Markdown
Contributor Author

Thanks, checked on 670033e: the #4083 reproducer now gives [] (main: [15.0]), and an exhausted count=2 generator no longer fires a third time when restarted. A silent no-op when there's nothing left to schedule is fine; it matches resume().

Two small requests:

  • Trim the tests to the two regression cases (first event after end, restart after count is exhausted), each asserting that it didn't fire and isn't active. Please don't assert on model._event_generators. test_restart_after_stop_runs_remaining_count passes on main, so it can go.
  • The inline comment can be one line, or dropped.

Whether start() after stop() should reset count is a separate question, which I left in #2921.

Thanks for checking. Done in e444db2: the tests are now just the two regression cases, each asserting that the generator didn't fire and isn't active, with no asserts on model._event_generators. I removed test_restart_after_stop_runs_remaining_count and the inline comment. I've updated the description to match. Noted on #2921, I'll leave the count-reset question there.

@arzaanxeng arzaanxeng 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.

Thanks for addressing the count-exhaustion edge case and adding the regression test. I've reviewed the latest changes, and both cases appear to be handled correctly. The updated tests also look good to me.

@EwoutH EwoutH left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@EwoutH EwoutH added the bug Release notes label label Nov 15, 2026
@EwoutH
EwoutH merged commit 6319f9b into mesa:main Nov 15, 2026
18 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
When schedule.start is None, EventGenerator.start() schedules the first
event at model.time + interval without checking it against schedule.end.
If that time is already past end, the generator fires once outside its
window. Check _should_stop() before scheduling the first event, matching
what resume() already does.

(cherry picked from commit 6319f9b)
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EventGenerator fires once after schedule.end when start is not set

3 participants