Repository navigation
fix: skip removed agents mid-iteration in AgentSet.do/shuffle_do/map - #3918
Merged
quaquel merged 1 commit intoOct 6, 2026
Merged
Conversation
0xnithinmys
force-pushed
the
fix/agentset-removed-agent-mid-iteration
branch
from
October 5, 2026 09:58
f84609b to
3d698e0
Compare
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
force-pushed
the
fix/agentset-removed-agent-mid-iteration
branch
from
October 5, 2026 10:08
3d698e0 to
92a6392
Compare
|
Performance benchmarks:
|
quaquel
approved these changes
Oct 6, 2026
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentSet.do,shuffle_do, andmaponly checkedagentref() 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(whatmodel.agentsreturns) already has this guard.This PR makes the weak-ref
AgentSetconsistent with it.Impact
Any model using a derived AgentSet — from
.select(),.copy(),non-inplace
.shuffle(),.groupby(...).do(...)— where one agent's stepremoves another (predator-prey, epidemic removal, combat) was exposed to:
self.cell/self.spacewhich isNoneFix
Added
and agent in self._agentsto the iteration guard in all three methods.Test
Added
test_agentset_do_skips_agent_removed_mid_iterationmirroring theexisting
test_hardkeyagentset_strtest for_HardKeyAgentSet.Fixes #3917