Skip to content

Honor stop and pause called from an EventGenerator callback - #3874

Merged
quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:fix-eventgenerator-stop-in-callback
Sep 21, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:fix-eventgenerator-stop-in-callback

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

Closes #3873.

EventGenerator._execute_and_reschedule ran the callback and then scheduled the next event without re-checking whether the callback had stopped or paused the generator. So gen.stop() or gen.pause() called from inside the generator's own callback was only partly honored: a next event was still scheduled, and next_scheduled_time kept reporting a time on a stopped generator.

Repro on main:

def cb():
    calls.append(model.time)
    gen.stop()
gen = model.schedule_recurring(cb, Schedule(interval=1.0, start=1.0))
model.run_for(5)
gen.next_scheduled_time   # 2.0, expected None

Fix: after running the callback, return early if the generator is no longer active or is paused, before scheduling the next event.

Added tests for stopping and pausing from within the callback.

Verified locally: the event tests pass and ruff check and format are clean.

@github-actions github-actions Bot added the testing Release notes label label Sep 19, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -4.0% [-4.3%, -3.8%] 🔵 +1.5% [+1.3%, +1.7%]
BoltzmannWealth large 🟢 -3.6% [-4.0%, -3.3%] 🔵 +0.5% [+0.2%, +0.8%]
Schelling small 🔵 -2.6% [-3.0%, -2.3%] 🔵 +0.9% [+0.2%, +1.7%]
Schelling large 🔵 -0.8% [-1.3%, -0.3%] 🔵 +1.6% [+0.2%, +2.8%]
WolfSheep small 🔵 -1.7% [-2.1%, -1.3%] 🔵 +1.1% [-3.1%, +5.7%]
WolfSheep large 🔵 -1.3% [-1.9%, -0.8%] 🔵 +0.7% [-0.1%, +1.5%]
SugarscapeG1mt small 🔵 -2.5% [-2.9%, -2.1%] 🔵 -1.9% [-3.4%, -0.5%]
SugarscapeG1mt large 🔵 -1.6% [-2.2%, -1.1%] 🔵 -1.6% [-3.2%, +0.5%]
BoidFlockers small 🔵 -1.2% [-1.4%, -0.9%] 🔵 +1.2% [+0.8%, +1.7%]
BoidFlockers large 🔵 -0.3% [-0.6%, -0.0%] 🔵 +0.8% [+0.5%, +1.1%]
MultiLevelAllianceModel small 🔵 -2.3% [-3.1%, -1.7%] 🔵 -0.2% [-0.8%, +0.6%]
MultiLevelAllianceModel large 🔵 -1.4% [-2.1%, -0.6%] 🔵 +0.4% [-0.0%, +0.8%]

@quaquel quaquel added the bug Release notes label label Sep 21, 2026
@quaquel
quaquel merged commit 6df1f43 into mesa:main Sep 21, 2026
15 of 16 checks passed
@EwoutH

EwoutH commented Sep 22, 2026

Copy link
Copy Markdown
Member

Thanks for catching this!

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 ignores stop() or pause() called from its own callback

3 participants