Fix: skip extra output/trace dispatch after scenario end

When the scenario engine finishes during SimulatorCoreImpl::Step(), the simulation continues to write trace output and dispatch to the output generator for that final step. This produces one extra cyclic entry beyond the intended simulation end time, visible as an off-by-one in output row counts and trace sizes.

Root cause

In SimulatorCoreImpl::Step(), the scenario engine's IsFinished() state was only checked after the full step pipeline (environment step → trace write → output generation) had already completed. There was no mechanism to detect the transition from running to finished within a single step, so the output side-effects of the finishing step were always emitted.

Solution

Detect the scenario finish transition within Step() by comparing IsFinished() before and after scenario_engine_->Step().

const bool was_finished = scenario_engine_->IsFinished();
scenario_engine_->Step(current_timestamp, delta_time);
const bool scenario_just_finished = !was_finished && scenario_engine_->IsFinished();

When scenario_just_finished is true:

  • env_->Step() is still called with the normal delta_time --> simulation dynamics are fully preserved, ground truth timestamps update correctly
  • optionally_write_to_trace_() is skipped --> no extra trace entry
  • output_generator_->Step() is skipped --> no extra output row

This approach was chosen over a number of alternatives (early return, zero delta_time) because it preserves identical simulation behavior for all traffic participant models and downstream evaluators, while only suppressing the output artifacts of the redundant final step.

Test changes

  • New testGivenScenarioFinishesDuringStep_WhenStep_ThenOutputSkipped verifies that the environment is stepped normally but output generation is skipped when the scenario finishes during a step.
  • Existing test fix: Changed EXPECT_CALL(..., IsFinished()).Times(1) to ON_CALL(...).WillByDefault(...) since IsFinished() is now called twice per step (before and after scenario_engine_->Step()).
Edited by Manuel Mühlberger

Merge request reports

Loading