Skip to content

Do not double-schedule when an EventGenerator is restarted from its callback - #3922

Merged
quaquel merged 3 commits into
mesa:mainfrom
AshNicolus:fix-eventgenerator-restart-in-callback
Oct 20, 2026
Merged

quaquel merged 3 commits into
mesa:mainfrom
AshNicolus:fix-eventgenerator-restart-in-callback

Conversation

@AshNicolus

@AshNicolus AshNicolus commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3921.

Follow-up to #3874. That change handled stop() and pause() from within a generator's callback, but not stop-then-restart: if the callback calls gen.stop() then gen.start(), start() schedules the next event and then _execute_and_reschedule falls through and schedules a second one, so the callback then fires twice per step.

Repro on main:

def cb():
    calls.append(model.time)
    if len(calls) == 1:
        gen.stop(); gen.start()
gen = model.schedule_recurring(cb, Schedule(interval=1.0))
model.run_for(4)
calls   # [1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0], expected one per step

The same thing happens when the callback calls gen.pause() then gen.resume(), since resume() also schedules a new event:

pause + resume on main:   [1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0]
pause + resume here:      [1.0, 2.0, 3.0, 4.0]

Fix: capture _current_event before 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.

@github-actions github-actions Bot added the testing Release notes label label Oct 6, 2026
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.1% [-2.5%, -1.5%] 🔵 +2.0% [+1.7%, +2.3%]
BoltzmannWealth large 🔵 -1.3% [-2.0%, -0.6%] 🔵 -4.2% [-6.4%, -2.0%]
Schelling small 🔵 +2.3% [+1.8%, +2.8%] 🔵 +1.6% [+0.8%, +2.4%]
Schelling large 🔵 +1.6% [+0.9%, +2.4%] 🔵 +3.4% [+1.4%, +5.6%]
WolfSheep small 🔵 -0.1% [-0.8%, +0.5%] 🔵 +5.3% [+1.4%, +9.2%]
WolfSheep large 🔵 +1.8% [+0.4%, +3.2%] 🔵 +2.5% [+1.8%, +3.4%]
SugarscapeG1mt small 🔵 -0.3% [-1.0%, +0.3%] 🔵 -1.2% [-2.6%, +0.3%]
SugarscapeG1mt large 🔵 +0.7% [-0.7%, +1.8%] 🔵 +1.6% [+0.4%, +3.0%]
BoidFlockers small 🔵 -1.6% [-2.2%, -1.0%] 🔵 +0.5% [+0.1%, +0.9%]
BoidFlockers large 🔵 -1.0% [-1.7%, -0.2%] 🔵 +0.2% [-0.1%, +0.4%]
MultiLevelAllianceModel small 🔵 +0.1% [-1.4%, +1.7%] 🔵 +0.8% [+0.1%, +1.4%]
MultiLevelAllianceModel large 🔵 -1.7% [-3.6%, +0.5%] 🔵 +4.3% [+2.0%, +7.0%]

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

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.

Comment thread mesa/time/events.py
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:

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.

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.

Comment thread tests/time/test_events.py
model.run_for(2.0)
assert calls == [1.0, 6.0, 7.0]

def test_restart_from_within_callback(self, setup):

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.

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

Copy link
Copy Markdown
Contributor Author

Thanks @lahari936, good catch on resume(). I hadn't realised it went through the same path. Added the pause/resume test (it fails on main and passes here), widened the comment to say restarted or resumed, and mentioned the pause/resume case in the description.

@quaquel quaquel added the bug Release notes label label Oct 20, 2026
@quaquel
quaquel merged commit d0124f8 into mesa:main Oct 20, 2026
15 of 16 checks passed
@lahari936

lahari936 commented Oct 21, 2026 via email

Copy link
Copy Markdown
Contributor

EwoutH pushed a commit that referenced this pull request Oct 28, 2026
…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)
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
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 double-schedules if restarted from within its own callback

3 participants