Repository navigation
Don't schedule an EventGenerator's first event past schedule.end - #4084
Conversation
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
8e4685b to
b726f08
Compare
arzaanxeng
left a comment
There was a problem hiding this comment.
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.
|
Thanks, checked on 670033e: the #4083 reproducer now gives Two small requests:
Whether |
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
left a comment
There was a problem hiding this comment.
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.
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)
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
Summary
A recurring event scheduled with
Schedule(end=...)and nostartcould fire once afterend. This PR makesEventGenerator.start()respectendfor the first event, so the generator never fires outside its window.Bug / Issue
Fixes #4083
When
schedule.startisNone,EventGenerator.start()schedules the first event atmodel.time + intervalwithout checking it againstschedule.end. Every later event goes through_should_stop(), andresume()does the same check, but the first event instart()did not. So, for example, startingSchedule(interval=5, end=12)att=10fired the callback att=15.Implementation
In
EventGenerator.start(), after the first event time is computed, callself._should_stop(start_time). If it returnsTrue, return without activating the generator, registering it with the model, or scheduling an event. This matches howresume()handles the same situation.The check also covers
count.execution_countisn't reset bystop()/start(), so restarting a generator whose count is already used up now leaves it inactive. Before, it fired one extra time pastcount. Restarting afterstop()before the count is reached still runs the remaining executions.Testing
test_first_event_after_end_does_not_fireintests/time/test_events.py. It fails onmainand passes with this change.[]/False(it printed[15.0]/Falsebefore).ruff checkandruff formatare clean, and the pre-commit hooks pass.pytest tests/ -Werror -Wdefault::PendingDeprecationWarning): alltests/timetests pass. Locally, the only failures are the same ones as on unchangedmain: Playwright browsers aren't installed, a stale localmesa/experimental/devsfolder, and the order-dependent logging test tracked in Importing the Boltzmann example app disables MESA log propagation, breaking caplog in later tests #3935.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