Skip to content

Preserve scenario initialization errors during cleanup - #249

Open
tandede wants to merge 1 commit into
ucla-mobility:mainfrom
tandede:fix/scenario-cleanup-before-init
Open

Preserve scenario initialization errors during cleanup#249
tandede wants to merge 1 commit into
ucla-mobility:mainfrom
tandede:fix/scenario-cleanup-before-init

Conversation

@tandede

@tandede tandede commented Aug 25, 2026

Copy link
Copy Markdown

Pull request information

Description

Problem analysis

single_town06_carla and single_2lanefree_carla always enter their finally blocks, including when CAV-world, model, or simulator initialization fails. Several cleanup variables were only assigned after those initialization steps succeeded.

In the reported --apply_ml path, the original model-loading or network exception was therefore replaced by an UnboundLocalError when cleanup first referenced eval_manager. The same control flow could also reference an unavailable scenario manager or vehicle list. This hid the actionable failure and made the scenario appear to fail in cleanup instead of at its real initialization boundary.

Implementation

Both scenario entry points now establish their cleanup state before entering the scenario:

  • manager references start as None;
  • foreground and background vehicle collections start as empty lists;
  • recorder state becomes active only after start_recorder() succeeds;
  • evaluation, recorder shutdown, and scenario-manager shutdown run only for resources that were successfully created;
  • vehicle destruction remains safe when initialization stopped before either collection was populated.

The normal successful path is unchanged. The guards only affect partial initialization, and any original exception continues propagating after the resources created before that point are released.

Regression coverage

The new simulator-independent regression stubs the CARLA boundary and exercises the real run_scenario() functions:

  1. For both scenarios named in UnboundLocalError: local variable 'eval_manager' referenced before assignment #242, a CAV-world initialization failure must remain the visible exception instead of being replaced during cleanup.
  2. When initialization progresses far enough to create the scenario manager and foreground/background vehicles but evaluation-manager construction then fails, the original error is preserved and every created resource is released exactly once.

On current main, the first regression reproduces UnboundLocalError in both scenario modules. With this change, both original initialization errors are preserved and the partial-resource cleanup assertions pass.

Validation

  • python -m pytest -q test/test_scenario_cleanup.py2 passed, 2 subtests passed
  • pycodestyle on all three changed files
  • python -m compileall -q on all three changed files
  • git diff --check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnboundLocalError: local variable 'eval_manager' referenced before assignment

1 participant