Skip to content

fix(agentset): downgrade _HardKeyAgentSet.select_random() to AgentSet (#3992) - #3994

Merged
quaquel merged 1 commit into
mesa:mainfrom
Dilraj07:fix/hardkey-select-random-leak
Oct 26, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
Dilraj07:fix/hardkey-select-random-leak

Conversation

@Dilraj07

Copy link
Copy Markdown
Contributor

Description

Background & Problem

In _HardKeyAgentSet (which backs model.agents and model.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 _HardKeyAgentSet must be downgraded to standard AgentSet (weak references via WeakKeyDictionary) when inplace=False:

https://github.com/mesa/mesa/blob/b1db02ad/mesa/agentset.py#L923-L942

While methods like select(), copy(), shuffle(), sort(), and groupby() all correctly implement this downgrade, select_random() was missing an override in _HardKeyAgentSet.

Consequently, calling model.agents.select_random(...) inherited AbstractAgentSet.select_random(), which defaults to returning type(self)(...) (a _HardKeyAgentSet). This retained strong references to the sampled agents. When those agents were removed via agent.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 with select(), shuffle(), and sort():

  • inplace=False (default): Downgrades the returned sample to standard AgentSet (weak references), allowing removed agents to be garbage collected properly.
  • inplace=True: Updates and returns self (_HardKeyAgentSet), preserving expected in-place mutation behavior.

Testing

  • Added test_hardkeyagentset_select_random_garbage_collection() in tests/test_agentset.py verifying with weakref.ref and gc.collect() that dead agents are successfully freed after removal.
  • Extended test_hardkeyagentset_downgrade() to verify that hard_set.select_random() returns AgentSet (not _HardKeyAgentSet) when inplace=False, and returns _HardKeyAgentSet when inplace=True.
  • 100% diff coverage verified; all 52 AgentSet tests pass.

Fixes #3992

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project (ruff check and ruff format).
  • I have added tests that prove my fix is effective or that my feature works.
  • All new and existing tests passed (pytest tests/test_agentset.py).

@github-actions github-actions Bot added the testing Release notes label label Oct 26, 2026
@quaquel quaquel added the bug Release notes label label Oct 26, 2026
@quaquel
quaquel merged commit c75d8f8 into mesa:main Oct 26, 2026
16 of 17 checks passed
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.

_HardKeyAgentSet.select_random() returns strong references, causing memory leak on removed agents

2 participants