Skip to content

Fix tracing-state leak after transform exceptions#9019

Open
yinkev wants to merge 1 commit into
Project-MONAI:devfrom
yinkev:7701-trace-transform-exception-state
Open

Fix tracing-state leak after transform exceptions#9019
yinkev wants to merge 1 commit into
Project-MONAI:devfrom
yinkev:7701-trace-transform-exception-state

Conversation

@yinkev

@yinkev yinkev commented Jul 25, 2026

Copy link
Copy Markdown

Fixes #7701.

Description

TraceableTransform.trace_transform() now restores the previous tracing state even when code inside the context manager raises. The original exception still propagates unchanged.

The previous implementation restored self.tracing only after a normal yield, so an exception permanently leaked the temporary tracing value into the transform. This caused later inverse transforms to fail with Transform Tracing must be enabled after a caught CUDA OOM or another runtime exception.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

Validation

  • The regression fails on upstream/dev because the tracing state remains changed after an exception.
  • python -m pytest tests/transforms/inverse/test_traceable_transform.py -q: 2 passed.
  • Black, isort, Ruff, mypy, documentation build, packaging, DCO, and pre-commit checks pass in upstream CI.
  • git diff --check passes.

Signed-off-by: kyinhub <kevinpyin@gmail.com>
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

TraceableTransform.trace_transform now restores the previous tracing value on both normal and exceptional context-manager exits. Its documentation describes this behavior and the parameter. A unit test verifies that a runtime error does not prevent restoration of the original tracing state.

Estimated code review effort: 1 (Trivial) | ~5 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes restore tracing state on exceptions and add a regression test, matching the issue's core requirement.
Out of Scope Changes check ✅ Passed The diff stays within scope: one fix, a docstring update, and a targeted regression test.
Title check ✅ Passed The title accurately summarizes the main fix: restoring tracing state after transform exceptions.
Description check ✅ Passed The description matches the template and includes the issue link, summary, change type checkboxes, and validation notes.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

Metadata/Tracing tracking fails after catching an exception

1 participant