Skip to content

Bug: Events.GET_BATCH_STARTED(once=N) fires twice when engine.terminate_epoch(False) is called inside handler #3625

@Ayush-Aditya

Description

@Ayush-Aditya

When using Events.GET_BATCH_STARTED with the once=N filter, the handler ends up triggering twice if engine.terminate_epoch(False) is called inside that same handler.

I noticed there's already an inline TODO about this in tests/ignite/engine/test_engine.py (around line 342), plus some comments in ignite/engine/engine.py about trying to avoid double triggers when the dataloader restarts.

Here is a minimal script to reproduce it

from unittest.mock import MagicMock
from ignite.engine import Engine, Events

engine = Engine(MagicMock(return_value=1))
count = {"n": 0}

@engine.on(Events.GET_BATCH_STARTED(once=12))
def h():
    count["n"] += 1
    if count["n"] == 1:
        engine.terminate_epoch(False)

engine.run(range(10), max_epochs=3)
print("GET_BATCH_STARTED_once_12_calls", count["n"])

Actual behavior:
The script prints GET_BATCH_STARTED_once_12_calls 2.

Expected behavior:
Because of once=12, the handler should strictly fire exactly once.

If this is something you want fixed right now, let me know! I'd be happy to open a draft PR with a failing test and a fix for the engine event flow.

Environment
PyTorch Version: 2.10.0+cpu
Python version: 3.13.5
Ignite version: 0.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions