Repository navigation
Do not double-schedule when an EventGenerator is restarted from its callback - #3922
Conversation
|
Performance benchmarks:
|
lahari936
left a comment
There was a problem hiding this comment.
Checked this out and ran it rather than reading only. The fix is correct, the test is a real regression test, and I think it is doing more good than the description claims — details below.
Verification. test_restart_from_within_callback passes on the branch. I then reverted the condition on line 312 back to main's if not self._active or self._paused: and reproduced the bug exactly as #3921 describes:
restart [1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0] <- without the fix
restart [1.0, 2.0, 3.0, 4.0] <- with it
The logic holds. _current_event is not cleared before the callback runs, so firing_event genuinely captures the event being fired. stop() cancels it and sets _current_event = None; start() goes through _schedule_next(), which builds a fresh Event and assigns it. So an identity change means precisely "the callback rescheduled me", which is the condition you want. In the ordinary path nothing reassigns _current_event, so the check is false and scheduling proceeds as before.
I also went looking for ways to fool it and did not find one. A callback calling start() without stop() hits the if self._active: return guard, leaves _current_event untouched, and reschedules normally. stop() alone and pause() alone still short-circuit on the first two clauses, so #3874's behaviour is unchanged.
One finding worth acting on. resume() also calls _schedule_next(), so a callback that does pause() then resume() double-schedules on main for exactly the same reason, and this PR silently fixes that too:
pause+resume [1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0] <- main
pause+resume [1.0, 2.0, 3.0, 4.0] <- this branch
That is a second bug fixed without a test covering it, which is how fixes quietly regress. I would add a sibling test — the body is your existing one with gen.stop(); gen.start() swapped for gen.pause(); gen.resume() — and mention the pause/resume case in the PR description, since it makes the change more valuable than it currently reads.
Nothing blocking from me. Looks good.
| if not self._active or self._paused: | ||
| # The callback may have stopped, paused, or restarted this generator; a | ||
| # restart replaces _current_event, so do not schedule a second one. | ||
| if not self._active or self._paused or self._current_event is not firing_event: |
There was a problem hiding this comment.
This clause is doing more work than the comment above it says.
resume() also routes through _schedule_next() and so replaces _current_event. That means a callback doing pause() then resume() double-schedules on main for the same reason a restart does, and this identity check fixes that case too. I confirmed by reverting just this line:
pause+resume [1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0] <- main's condition
pause+resume [1.0, 2.0, 3.0, 4.0] <- this branch
Worth widening the comment to say "stopped, paused, restarted, or resumed", since the next person reading it will otherwise assume restart is the only case covered.
| model.run_for(2.0) | ||
| assert calls == [1.0, 6.0, 7.0] | ||
|
|
||
| def test_restart_from_within_callback(self, setup): |
There was a problem hiding this comment.
Good test -- it fails on main with the doubled list from #3921 and passes here, so it pins the behaviour rather than just describing it.
Suggestion: add a sibling for the pause/resume path, which is the same bug through a different door and is currently fixed but untested:
def test_pause_resume_from_within_callback(self, setup):
"""Pausing and resuming from the callback must not double-schedule either."""
model, _ = setup
calls = []
gen = None
def cb():
calls.append(model.time)
if len(calls) == 1:
gen.pause()
gen.resume()
gen = EventGenerator(model, cb, Schedule(interval=1.0))
gen.start()
model.run_for(4.0)
assert calls == [1.0, 2.0, 3.0, 4.0]I ran that against both arms: doubled on main, clean here.
resume() schedules a new event just like start(), so a callback that pauses and then resumes its generator double-scheduled on main for the same reason as a restart. The identity check already fixes it; add a test for it and say so in the comment.
|
Thanks @lahari936, good catch on |
|
Thanks Ash!!
…On Wed, Oct 21, 2026 at 1:46 AM Jan Kwakkel ***@***.***> wrote:
Merged #3922 <#3922> into main.
—
Reply to this email directly, view it on GitHub
<#3922?email_source=notifications&email_token=BL3P7ADQSGIJJYYV6AUDXG35U7B3RA5CNFSNUABQM5UWIORPF5TWS5BNNB2WEL2JONZXKZKFOZSW45CON52GSZTJMNQXI2LPNYXTGMZXHAYTIOJQGUYTHJTSMVQXG33OU5WWK3TUNFXW5JLFOZSW45FMMZXW65DFOJPWG3DJMNVQ#event-33781490513>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BL3P7AE5XVHL7BU4WJAXNMT5U7B3RAVCNFSNUABEKJSXA33TNF2G64TZHMZDIMRUGU3TGNZ3JFZXG5LFHM2TOMRWGE4DCOBTGWQXMAQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
…allback (#3922) * Do not double-schedule when an EventGenerator is restarted from its callback * Cover pause and resume from inside the callback resume() schedules a new event just like start(), so a callback that pauses and then resumes its generator double-scheduled on main for the same reason as a restart. The identity check already fixes it; add a test for it and say so in the comment. (cherry picked from commit d0124f8)
Closes #3921.
Follow-up to #3874. That change handled
stop()andpause()from within a generator's callback, but not stop-then-restart: if the callback callsgen.stop()thengen.start(),start()schedules the next event and then_execute_and_reschedulefalls through and schedules a second one, so the callback then fires twice per step.Repro on main:
The same thing happens when the callback calls
gen.pause()thengen.resume(), sinceresume()also schedules a new event:Fix: capture
_current_eventbefore running the callback, and skip rescheduling if the callback replaced it (a restart or a resume already scheduled the next event), alongside the existing active/paused checks.Added tests for both restarting and pausing then resuming from within the callback. Both fail on main and pass here.
Verified locally: the full suite passes with
pytest tests/ -W error, and pre-commit is clean.