Skip to content

Model._event_generators grows unbounded #3313

@quaquel

Description

@quaquel

Describe the bug
Model._event_generators continues to grow while running a model, and also will contain generators that are exhausted. It is a minor bug, but worth addressing at some point. But since a generator also holds a hard ref to the callable, it might, for example, prevent agents from being garbage collected.

Expected behavior
Whenever a generator is exhausted (or stopped?), it should remove itself from model._event_generators.

proposed direction for fix
There are a couple of directions:

  1. Turn ._event_generators into a set, and whenever a generator is exhausted, it removes itself from this set. The only function of _event_generators is to serve as a hard ref to generators, so ordering does not matter. This fix would also imply that the responsibility of adding a generator to schedule_recurring should move from schedule_recurring into the EventGenerator class, so all instances are in the set.
  2. Make it the responsibility of the caller of schedule_recurring to maintain a hard ref to the generator. This is tricky for users, so less ideal.
  3. To reduce the severity of the leak, we should consider turning self.function into a weakref similar to how it is done for Event, and if the ref becomes None, this should automagically stop the generator.

Metadata

Metadata

Assignees

Labels

bugRelease notes label

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions