Repository navigation
fix(agentset): implement _from_iterable to preserve random and avoid strong ref leaks in set operations - #4105
Conversation
…strong ref leaks in set operations
|
Performance benchmarks:
|
AshNicolus
left a comment
There was a problem hiding this comment.
Reproduced on main: set operators on model.agents return a _HardKeyAgentSet that keeps removed agents alive, and empty results warn and get an unseeded Random. With this PR all four operators, the reflected forms like {a} - model.agents, and in-place ops come back as weak AgentSets on model.random. Full suite passes.
One issue, see the inline comment: hard-coding AgentSet breaks subclasses.
Small thing: test_agentset_set_operations_with_python_set passes on main too. A remove + gc.collect() check on model.agents - subset would test the leak directly.
| def _from_iterable(self, it: Iterable[A]) -> AgentSet[A]: | ||
| """Construct an AgentSet from an iterable, preserving the random generator. | ||
|
|
||
| This hook is called by collections.abc.Set binary operators (&, |, -, ^). | ||
| It ensures returned sets hold weak references and preserve self.random. | ||
| """ | ||
| return AgentSet(it, random=self.random) |
There was a problem hiding this comment.
With class MySet(AgentSet), s - {a} returned MySet on main and a plain AgentSet here, while select() still returns MySet. The rest of the file uses type(self)(...) and lets _HardKeyAgentSet downgrade, so:
| def _from_iterable(self, it: Iterable[A]) -> AgentSet[A]: | |
| """Construct an AgentSet from an iterable, preserving the random generator. | |
| This hook is called by collections.abc.Set binary operators (&, |, -, ^). | |
| It ensures returned sets hold weak references and preserve self.random. | |
| """ | |
| return AgentSet(it, random=self.random) | |
| def _from_iterable(self, it: Iterable[A]) -> AbstractAgentSet[A]: | |
| """Build the result of a set operator (&, |, -, ^), keeping self.random.""" | |
| return type(self)(it, self.random) |
plus in _HardKeyAgentSet, next to copy():
def _from_iterable(self, it: Iterable[A]) -> AgentSet[A]:
return AgentSet(it, self.random)…strong ref leaks in set operations (mesa#4105) * fix(agentset): implement _from_iterable to preserve random and avoid strong ref leaks in set operations * fix(agentset): preserve subclass type in _from_iterable and test leak prevention Adapted for 3.5.x: the conflict is only in tests/test_agentset.py, where main's tests follow 4.0-only weighted select_random tests. Only the tests added by mesa#4105 are taken, with their gc, warnings and weakref imports. (cherry picked from commit 71f239a) Assisted-by: Claude Opus 5.5
Pre-PR Checklist
Summary
Implements
_from_iterableonAbstractAgentSetso binary set operations (&,|,-,^):self.randomand suppress spurious non-reproducibility warnings when operations produce an empty set.AgentSetinstances instead of leaking strong-reference_HardKeyAgentSetinstances frommodel.agents.Bug / Issue
Fixes #4104
In Python's
collections.abc.Set, binary set operators (&,|,-,^) construct results viaself._from_iterable(...). Without_from_iterable:cls(it)withrandom=None, triggering a spuriousUserWarningand assigning an unseeded stdlibRandom()that broke simulation reproducibility.model.agentsreturned a_HardKeyAgentSetholding strong references to agents, violating Mesa's architecture where operations onmodel.agentsmust return weakAgentSetinstances to avoid memory leaks.Implementation
Implemented
_from_iterable(self, it)onAbstractAgentSetinmesa/agentset.pyto constructAgentSet(it, random=self.random).Testing
tests/test_agentset.py:test_agentset_set_operations_preserve_random_and_suppress_empty_warningtest_model_agents_set_operations_return_weak_agentsettest_agentset_set_operations_with_python_settest_agentset.pypass cleanly.ruff checkandruff format --check.Additional Notes
No breaking changes. This ensures consistent memory management and random generator propagation across all set operations.