Skip to content

fix: skip removed agents mid-iteration in AgentSet.do/shuffle_do/map - #3918

Merged
quaquel merged 1 commit into
mesa:mainfrom
0xnithinmys:fix/agentset-removed-agent-mid-iteration
Oct 6, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
0xnithinmys:fix/agentset-removed-agent-mid-iteration

Conversation

@0xnithinmys

Copy link
Copy Markdown
Contributor

AgentSet.do, shuffle_do, and map only checked agentref() is not None
(i.e. not garbage-collected). They never re-checked agent in self._agents,
so an agent removed by an earlier agent's call in the same loop was still
invoked.

_HardKeyAgentSet (what model.agents returns) already has this guard.
This PR makes the weak-ref AgentSet consistent with it.

Impact

Any model using a derived AgentSet — from .select(), .copy(),
non-inplace .shuffle(), .groupby(...).do(...) — where one agent's step
removes another (predator-prey, epidemic removal, combat) was exposed to:

  • Crash — removed agent accesses self.cell / self.space which is None
  • Silent wrong results — dead agent takes an extra turn
  • Non-reproducibility — outcome depends on GC timing

Fix

Added and agent in self._agents to the iteration guard in all three methods.

Test

Added test_agentset_do_skips_agent_removed_mid_iteration mirroring the
existing test_hardkeyagentset_str test for _HardKeyAgentSet.

Fixes #3917

@github-actions github-actions Bot added the testing Release notes label label Oct 5, 2026
@0xnithinmys
0xnithinmys force-pushed the fix/agentset-removed-agent-mid-iteration branch from f84609b to 3d698e0 Compare October 5, 2026 09:58
Weak-ref AgentSet was only checking �gentref() is not None (i.e. not
garbage-collected), but never re-checked �gent in self._agents.
An agent removed from the set by an earlier agent's method call in the
same loop was still invoked — causing crashes, silent wrong results, or
non-reproducibility depending on GC timing.

Add the same �gent in self._agents membership guard that
_HardKeyAgentSet.do/shuffle_do/map already uses, keeping both
implementations consistent.

Fixes mesa#3917
@0xnithinmys
0xnithinmys force-pushed the fix/agentset-removed-agent-mid-iteration branch from 3d698e0 to 92a6392 Compare October 5, 2026 10:08
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -4.0% [-4.6%, -3.5%] 🔵 -0.0% [-0.3%, +0.2%]
BoltzmannWealth large 🔵 -1.8% [-2.7%, -0.9%] 🟢 -10.0% [-12.2%, -7.9%]
Schelling small 🟢 -3.9% [-4.3%, -3.6%] 🔵 -0.6% [-1.3%, +0.1%]
Schelling large 🔵 -1.4% [-2.6%, -0.0%] 🔵 -1.6% [-5.6%, +2.4%]
WolfSheep small 🔵 +1.4% [+0.6%, +2.2%] 🔵 +2.5% [-0.7%, +5.8%]
WolfSheep large 🔵 -1.4% [-2.5%, -0.3%] 🟢 -5.2% [-6.8%, -3.7%]
SugarscapeG1mt small 🔵 -0.2% [-1.0%, +0.5%] 🔵 -1.6% [-3.2%, +0.0%]
SugarscapeG1mt large 🔵 +0.3% [-0.3%, +1.0%] 🔵 +0.2% [-1.8%, +2.0%]
BoidFlockers small 🔵 +1.1% [+0.6%, +1.6%] 🔵 +0.1% [-0.4%, +0.6%]
BoidFlockers large 🔵 +2.5% [+2.2%, +3.0%] 🔵 +1.2% [+0.9%, +1.5%]
MultiLevelAllianceModel small 🔵 +0.5% [-1.1%, +2.1%] 🔵 +0.4% [-0.0%, +0.8%]
MultiLevelAllianceModel large 🔵 +2.6% [+1.8%, +3.6%] 🔵 +3.0% [+1.9%, +4.1%]

@quaquel quaquel added the bug Release notes label label Oct 6, 2026
@quaquel
quaquel merged commit 0596e42 into mesa:main Oct 6, 2026
14 of 16 checks passed
@0xnithinmys
0xnithinmys deleted the fix/agentset-removed-agent-mid-iteration branch October 22, 2026 15:41
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
…3918)

Weak-ref AgentSet was only checking �gentref() is not None (i.e. not
garbage-collected), but never re-checked �gent in self._agents.
An agent removed from the set by an earlier agent's method call in the
same loop was still invoked — causing crashes, silent wrong results, or
non-reproducibility depending on GC timing.

Add the same �gent in self._agents membership guard that
_HardKeyAgentSet.do/shuffle_do/map already uses, keeping both
implementations consistent.

Fixes #3917

Co-authored-by: Nithin N <[email protected]>
(cherry picked from commit 0596e42)
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.

AgentSet.do/shuffle_do/map calls removed agents mid-iteration

2 participants