Skip to content

Skip the scheduled events of removed agents - #4119

Merged
EwoutH merged 1 commit into
mesa:mainfrom
EwoutH:skip-removed-agent-events
Nov 16, 2026
Merged

EwoutH merged 1 commit into
mesa:mainfrom
EwoutH:skip-removed-agent-events

Conversation

@EwoutH

@EwoutH EwoutH commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Pre-PR Checklist

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

Summary

A removed agent no longer acts through the scheduler: its pending events are skipped, its recurring events stop and its Threshold doesn't fire. Results no longer depend on garbage collection. This is option 2 from #4117, which was preferred over the bookkeeping approach in #4118.

It also makes the documentation true: the DES tutorial and the Event docstring already say a removed agent's events do nothing, but that only held once Python had freed the agent.

Bug / Issue

Fixes #4117. Scheduled events only stopped once Python freed the removed agent: never if it was still referenced, and at a GC-dependent moment if it was in a reference cycle.

Implementation

  • Agent gets a class attribute _removed = False. remove() sets it to True.
  • Event.execute skips the callback when it's a method of an object whose _removed is true. EventGenerator stops in that case.
  • Threshold.execute returns early for a removed agent, because its callback is a closure, not a method.

Testing

  • One test in test_schedule_run.py (one-off, recurring and post-removal events of a still-referenced agent don't run) and one in test_states.py (a removed wolf doesn't starve). Both fail on main.
  • The same-seed script from Removed agents keep running their scheduled events until Python garbage-collects them #4117 gives the same result with GC disabled, default and eager.
  • Interleaved benchmark, 2000 agents whose callbacks do no work, so this is the worst case:
Workload main #4118 this PR
One-off events rescheduling themselves 1091 ms +6.0% +2.2%
schedule_recurring 882 ms −0.1% +0.8%
Threshold agents dying and respawning 670 ms +4.2% +1.1%
5% of agents replaced per time unit 1152 ms +6.1% +0.0%

Additional Notes

Compared to #4118:

  • Smaller: 13 changed lines in mesa/, no per-agent bookkeeping.
  • Also covers events scheduled on an agent after it was removed, and a Threshold that re-arms because a removed agent's state changed. Cancel a removed agent's scheduled events #4118 misses both.
  • A removed agent's pending events stay in the event list until their time comes, rather than being canceled. They don't keep the agent alive, and recurring events stop at their next run. This is the same as on main.
  • A skipped event isn't marked CANCELED.

Like #4118, this changes behaviour: any event on a removed agent's method is skipped, whoever scheduled it. functools.partial callbacks aren't covered.

AI-assisted: implemented and benchmarked with Claude Code.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -4.5% [-5.0%, -4.0%] 🔵 -0.7% [-0.9%, -0.5%]
BoltzmannWealth large 🔵 -1.9% [-2.5%, -1.2%] 🟢 -7.6% [-9.2%, -6.0%]
Schelling small 🔵 -3.0% [-3.8%, -2.3%] 🔵 -1.0% [-1.9%, -0.0%]
Schelling large 🟢 -4.0% [-4.6%, -3.4%] 🟢 -33.1% [-35.1%, -31.2%]
WolfSheep small 🟢 -6.4% [-7.0%, -5.9%] 🔵 -5.6% [-9.8%, -1.7%]
WolfSheep large 🔵 -4.0% [-5.7%, -2.3%] 🟢 -10.2% [-13.3%, -7.4%]
SugarscapeG1mt small 🔵 -3.3% [-3.8%, -2.8%] 🔵 -2.6% [-4.0%, -1.2%]
SugarscapeG1mt large 🔵 -2.1% [-3.6%, -0.5%] 🔵 -0.5% [-2.4%, +1.8%]
BoidFlockers small 🔵 +1.3% [+0.6%, +1.9%] 🔵 +1.7% [+1.1%, +2.4%]
BoidFlockers large 🔵 +0.5% [-0.6%, +1.4%] 🔵 -0.1% [-1.1%, +0.5%]
MultiLevelAllianceModel small 🔵 -4.0% [-5.9%, -2.5%] 🔵 -0.2% [-0.5%, +0.0%]
MultiLevelAllianceModel large 🔵 +1.8% [+0.0%, +3.5%] 🔵 -1.1% [-3.8%, +2.0%]

@EwoutH EwoutH added the bug Release notes label label Nov 15, 2026
@EwoutH
EwoutH marked this pull request as ready for review November 15, 2026 21:50
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
1 task done
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.8% [-1.1%, -0.5%] 🔵 -0.8% [-1.0%, -0.6%]
BoltzmannWealth large 🔵 +0.1% [-0.3%, +0.6%] 🔵 +0.1% [-0.3%, +0.4%]
Schelling small 🔵 -0.2% [-0.6%, +0.1%] 🔵 -0.6% [-1.2%, +0.0%]
Schelling large 🔵 -1.2% [-2.1%, -0.3%] 🔵 +1.3% [+0.3%, +2.4%]
WolfSheep small 🔵 -0.4% [-0.8%, +0.0%] 🔵 +3.0% [-1.3%, +7.4%]
WolfSheep large 🔵 -0.5% [-0.8%, -0.2%] 🔵 +0.4% [-0.7%, +1.5%]
SugarscapeG1mt small 🔵 -0.1% [-0.3%, +0.2%] 🔵 -0.4% [-1.8%, +1.1%]
SugarscapeG1mt large 🔵 -0.4% [-1.0%, +0.1%] 🔵 -2.9% [-4.2%, -1.5%]
BoidFlockers small 🔵 +1.4% [+1.2%, +1.6%] 🔵 +1.1% [+0.7%, +1.5%]
BoidFlockers large 🔵 +1.4% [+1.0%, +1.8%] 🔵 +1.5% [+1.3%, +1.7%]
MultiLevelAllianceModel small 🔵 -0.3% [-0.9%, +0.3%] 🔵 -0.8% [-1.2%, -0.4%]
MultiLevelAllianceModel large 🔵 -0.3% [-0.8%, +0.2%] 🔵 -1.3% [-1.6%, -1.0%]

A removed agent's events, recurring events and Threshold events only
stopped once Python freed the agent, which happens late or never: an
agent that was still referenced kept acting, and one in a reference cycle
acted until the cyclic garbage collector ran, so results for a given seed
depended on garbage collection.

Agent.remove() now marks the agent as removed. An event on a removed
agent's method is skipped when it comes up, a generator stops, and a
Threshold doesn't fire.

Fixes mesa#4117

Assisted-by: Claude Code
Assisted-by: GPT-6
@EwoutH
EwoutH force-pushed the skip-removed-agent-events branch from 7c1f830 to f402539 Compare November 16, 2026 21:56
@EwoutH
EwoutH merged commit eac7447 into mesa:main Nov 16, 2026
14 of 15 checks passed
EwoutH added a commit that referenced this pull request Nov 22, 2026
Adapted for 3.5.x: the Agent.remove() and Event/EventGenerator parts only. 3.5 has no experimental states (Threshold) or locations, so those hunks and their tests are dropped.

(cherry picked from commit eac7447)

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Removed agents keep running their scheduled events until Python garbage-collects them

1 participant