mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Callable Schedule interval can return 0 in EventGenerator, bypassing validation

Open Krishsharma179 opened this issue 7 months ago • 1 comments

Describe the bug Schedule validates fixed numeric intervals with interval <= 0 (must be strictly > 0), but EventGenerator._get_interval() only rejects callable interval results when < 0.
This means a callable interval returning 0 is accepted, which is inconsistent with fixed-interval validation and can schedule events at the same simulation time repeatedly.

Expected behavior Callable and non-callable intervals should follow the same rule: interval must be strictly greater than 0.
If a callable returns 0 (or negative), a ValueError should be raised.

To Reproduce

  1. Create a model.
  2. Create a Schedule with a callable interval that returns 0.
  3. Start an EventGenerator using that schedule.
  4. Observe that no ValueError is raised for interval 0.

Minimal repro:

from mesa.time.events import EventGenerator, EventList, Schedule


class DummyModel:
	def __init__(self):
		self.time = 0.0
		self._event_list = EventList()
		self._event_generators = set()


def noop():
	pass


# Case 1: fixed interval=0 is correctly rejected.
try:
	Schedule(interval=0)
	print("fixed interval=0 -> NOT rejected (unexpected)")
except ValueError as exc:
	print(f"fixed interval=0 -> rejected (expected): {exc}")


# Case 2: callable interval returning 0 currently passes validation.
schedule = Schedule(interval=lambda _m: 0)
generator = EventGenerator(DummyModel(), noop, schedule)
interval = generator._get_interval()
print(f"callable interval returns: {interval}")

if interval == 0:
	print("BUG VERIFIED: callable interval can return 0")
else:
	print("No bug reproduced")

Current code:

def _get_interval(self) -> float | int:
        """Get the next interval value."""
        if callable(self.schedule.interval):
            interval = self.schedule.interval(self.model)
            if interval < 0:
                raise ValueError(f"Interval must be > 0, got {interval}")
            return interval
        return self.schedule.interval

proposed solution :

def _get_interval(self) -> float | int:
        """Get the next interval value."""
        if callable(self.schedule.interval):
            interval = self.schedule.interval(self.model)
            if interval <= 0:  #changed here
                raise ValueError(f"Interval must be strictly > 0, got {interval}")
            return interval
        return self.schedule.interval

I’ll be opening a PR shortly; the implementation is ready, and any feedback or suggestions are welcome

Krishsharma179 avatar Apr 12 '26 07:04 Krishsharma179

I disagree. In event scheduling, when drawing time between arrivals, having a 0 can be perfectly acceptable. An example is a negative binomial arrival process.

quaquel avatar Apr 12 '26 08:04 quaquel