Repository navigation
fix(agentset): downgrade _HardKeyAgentSet.select_random() to AgentSet (#3992) - #3994
Merged
Merged
Conversation
1 task done
quaquel
approved these changes
Oct 26, 2026
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.
Description
Background & Problem
In
_HardKeyAgentSet(which backsmodel.agentsandmodel.agents_by_type[...]), the internal dictionary stores strong references to keep active agents alive. However, to prevent memory leaks when agents are removed from the model, all user-facing views returned by_HardKeyAgentSetmust be downgraded to standardAgentSet(weak references viaWeakKeyDictionary) wheninplace=False:https://github.com/mesa/mesa/blob/b1db02ad/mesa/agentset.py#L923-L942
While methods like
select(),copy(),shuffle(),sort(), andgroupby()all correctly implement this downgrade,select_random()was missing an override in_HardKeyAgentSet.Consequently, calling
model.agents.select_random(...)inheritedAbstractAgentSet.select_random(), which defaults to returningtype(self)(...)(a_HardKeyAgentSet). This retained strong references to the sampled agents. When those agents were removed viaagent.remove(), Python's garbage collector was prevented from freeing them, causing an unbounded memory leak in simulations that sample agents across steps.Solution
This PR adds the
select_random()override to_HardKeyAgentSet, bringing it into full parity withselect(),shuffle(), andsort():inplace=False(default): Downgrades the returned sample to standardAgentSet(weak references), allowing removed agents to be garbage collected properly.inplace=True: Updates and returnsself(_HardKeyAgentSet), preserving expected in-place mutation behavior.Testing
test_hardkeyagentset_select_random_garbage_collection()intests/test_agentset.pyverifying withweakref.refandgc.collect()that dead agents are successfully freed after removal.test_hardkeyagentset_downgrade()to verify thathard_set.select_random()returnsAgentSet(not_HardKeyAgentSet) wheninplace=False, and returns_HardKeyAgentSetwheninplace=True.AgentSettests pass.Fixes #3992
Type of change
Checklist
ruff checkandruff format).pytest tests/test_agentset.py).