Skip to content

Cancel a removed agent's scheduled events - #4118

Closed
EwoutH wants to merge 1 commit into
mesa:mainfrom
EwoutH:cancel-removed-agent-events
Closed

EwoutH wants to merge 1 commit into
mesa:mainfrom
EwoutH:cancel-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

Agent.remove() now cancels the events, recurring events and Threshold events scheduled on the agent, so a removed agent stops acting, and results no longer depend on garbage collection. This is option 1 from #4117. Draft until we agree on the direction there.

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

  • Model.schedule_event and schedule_recurring record the event or generator on the agent when the callback is one of its methods (function.__self__ is an Agent), in a new Agent._scheduled dict.
  • Agent.remove() cancels the events and stops the generators in that dict.
  • An event leaves the dict when it runs or is canceled, and a generator when it stops or runs out, so long-lived agents don't accumulate entries.
  • Threshold schedules a closure, not a method, so it records its event explicitly.

Weak references stay as they are, against memory leaks.

Testing

  • Two tests in test_schedule_run.py (events and generators of a still-referenced agent are canceled, finished events are released) and one in test_states.py (a removed wolf doesn't starve). All three 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 (9/9 runs).
  • Interleaved benchmark, 2000 agents whose callbacks do no work, so this is the worst case:
Workload main this PR
One-off events rescheduling themselves 1106 ms 1157 ms +4.6%
schedule_recurring 874 ms 887 ms +1.5%
Threshold agents dying and respawning 659 ms 711 ms +7.9%
5% of agents replaced per time unit 1133 ms 1230 ms +8.6%

Additional Notes

  • Behaviour change: any event on an agent's method is canceled when that agent is removed, whoever scheduled it. Probably needs a migration guide entry.
  • Not covered: events scheduled on an agent after it was removed still run, and so does a Threshold that re-arms because a removed agent's state changed. Callbacks that aren't bound methods (functools.partial, plain functions) aren't tracked either.
  • Memory: one dict per agent (64 bytes empty).

AI-assisted: implemented and benchmarked with Claude Code.

Agent.remove() didn't cancel the events, recurring events or Threshold
events scheduled on the agent's methods. They only stopped once Python
freed the agent, which happens late or never: a removed agent that is
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.

The model now records events and generators scheduled on an agent's
method on that agent, and remove() cancels them. Entries are dropped when
an event runs or is canceled, or a generator stops. Threshold schedules a
closure, so it records its event explicitly.

Fixes mesa#4117

Assisted-by: Claude Code
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 15, 2026
@EwoutH EwoutH added bug Release notes label and removed testing Release notes label labels Nov 15, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.9% [-4.0%, -1.8%] 🔵 -0.8% [-0.9%, -0.6%]
BoltzmannWealth large 🔵 -0.5% [-1.1%, +0.1%] 🔵 +0.2% [-0.5%, +0.8%]
Schelling small 🔵 -0.9% [-1.4%, -0.5%] 🔵 -0.1% [-1.0%, +0.6%]
Schelling large 🔵 -1.2% [-1.7%, -0.8%] 🔵 -1.1% [-2.2%, +0.1%]
WolfSheep small 🔵 +1.2% [+0.7%, +1.6%] 🔵 +4.2% [+0.6%, +8.1%]
WolfSheep large 🔴 +5.4% [+4.3%, +6.5%] 🔴 +9.4% [+7.2%, +11.8%]
SugarscapeG1mt small 🔵 +2.9% [+2.5%, +3.4%] 🔵 -0.2% [-1.9%, +1.6%]
SugarscapeG1mt large 🔵 +2.0% [+0.8%, +3.3%] 🔵 -1.2% [-2.1%, -0.2%]
BoidFlockers small 🔵 +0.4% [-0.1%, +1.0%] 🔵 -0.1% [-0.6%, +0.3%]
BoidFlockers large 🔵 +1.6% [+1.0%, +2.2%] 🔵 -0.1% [-0.5%, +0.2%]
MultiLevelAllianceModel small 🔴 +18.2% [+14.4%, +22.0%] 🔵 -0.2% [-0.4%, +0.0%]
MultiLevelAllianceModel large 🔴 +6.8% [+5.1%, +8.4%] 🔵 +0.2% [+0.0%, +0.5%]

@EwoutH

EwoutH commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Closing in favour of #4119, which is smaller and covers more cases (see #4117).

@EwoutH EwoutH closed this Nov 15, 2026
@github-actions github-actions Bot added the testing Release notes label label Nov 15, 2026
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