Fix unbounded growth of Model._event_generators#3317
Merged
Conversation
Change Model._event_generators from a list to a set and move registration responsibility into EventGenerator. EventGenerator now adds itself to model._event_generators when scheduled and discards itself on stop/cancel; the model no longer appends generators. This avoids duplicate entries, enables O(1) add/remove, and keeps strong references to active generators to prevent GC.
Member
Author
|
When designing this part I thought this would be a problem, because now an EventGenerator can't be restarted anymore. But that's not true, because:
So this nicely solves itself. |
Member
What do you mean by restarting? |
quaquel
approved these changes
Feb 15, 2026
Member
Author
|
Calling .start() after you have called .stop() |
Member
|
Fair enough. |
Krishsharma179
pushed a commit
to Krishsharma179/mesa
that referenced
this pull request
Feb 21, 2026
Change Model._event_generators from a list to a set and move registration responsibility into EventGenerator. EventGenerator now adds itself to model._event_generators when scheduled and discards itself on stop/cancel; the model no longer appends generators. This avoids duplicate entries, enables O(1) add/remove, and keeps strong references to active generators to prevent GC.
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.
Summary
Model._event_generatorsgrows unbounded because exhausted and stoppedEventGenerators are never removed.Bug / Issue
_event_generatorsexists solely to hold strong references to active generators, preventing garbage collection. However, generators were never removed when they finished or were stopped, causing the list to grow indefinitely. Since generators hold hard references to their callables (which may be bound agent methods), this could also prevent agents from being garbage collected.Closes #3313.
Implementation
_event_generatorsfromlisttoset(ordering is irrelevant for a GC-prevention container).EventGenerator:start()adds itself to the set,stop()and exhaustion remove it viadiscard()._event_generators.append()call fromModel.schedule_recurring, sincestart()now handles it.Testing
Existing tests pass. Could add a test verifying that a generator with
count=nis removed from_event_generatorsafter completing its executions.Additional Notes
No public API changes,
_event_generatorsis a private attribute.