Skip to content

Fix tracing-state leak after transform exceptions#9013

Closed
yinkev wants to merge 1 commit into
Project-MONAI:devfrom
yinkev:fix/trace-transform-exception-state
Closed

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

Conversation

@yinkev

@yinkev yinkev commented Jul 25, 2026

Copy link
Copy Markdown

Closes #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 other 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

  • regression fails on upstream/dev with leaked tracing state;
  • python -m pytest tests/transforms/inverse/test_traceable_transform.py -q: 2 passed;
  • ruff check on both touched files;
  • git diff --check.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

trace_transform now restores the prior thread-local tracing state in a finally block, including when the context exits with an exception. A unit test verifies that tracing is temporarily disabled inside the context and restored afterward.

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

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title accurately summarizes the main change: restoring tracing state after exceptions.
Description check ✅ Passed The description matches the template and includes the change summary, change types, and validation.
✨ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
tests/transforms/inverse/test_traceable_transform.py (1)

32-42: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a docstring to the new test.

Document that the test verifies tracing state restoration after an exception.

As per path instructions, every Python definition should have an appropriate Google-style docstring.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/transforms/inverse/test_traceable_transform.py` around lines 32 - 42,
Add an appropriate Google-style docstring to the
test_trace_transform_restores_state_after_exception method documenting that it
verifies the tracing state is restored after an exception. Keep the test
behavior and assertions unchanged.

Source: Path instructions

monai/transforms/inverse.py (1)

404-411: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the context-manager contract.

Add a Google-style Args section for to_trace and document that the previous tracing state is restored on both normal and exceptional exits.

As per path instructions, Python definitions should use Google-style docstrings documenting their parameters, returns, and raised exceptions where applicable.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@monai/transforms/inverse.py` around lines 404 - 411, Update the
trace_transform method docstring to use Google style, adding an Args section
describing to_trace and documenting that the prior tracing state is restored
when the context manager exits normally or with an exception. Preserve the
existing tracing behavior.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@monai/transforms/inverse.py`:
- Around line 404-411: Update the trace_transform method docstring to use Google
style, adding an Args section describing to_trace and documenting that the prior
tracing state is restored when the context manager exits normally or with an
exception. Preserve the existing tracing behavior.

In `@tests/transforms/inverse/test_traceable_transform.py`:
- Around line 32-42: Add an appropriate Google-style docstring to the
test_trace_transform_restores_state_after_exception method documenting that it
verifies the tracing state is restored after an exception. Keep the test
behavior and assertions unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 7e06f2a2-40f2-4c16-a380-473f6e7e7b29

📥 Commits

Reviewing files that changed from the base of the PR and between 3ee058b and b5b4230.

📒 Files selected for processing (2)
  • monai/transforms/inverse.py
  • tests/transforms/inverse/test_traceable_transform.py

Signed-off-by: kyinhub <kevinpyin@gmail.com>
@yinkev
yinkev force-pushed the fix/trace-transform-exception-state branch from b5b4230 to 55a0e95 Compare July 25, 2026 18:42
@yinkev

yinkev commented Jul 25, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@yinkev

yinkev commented Jul 25, 2026

Copy link
Copy Markdown
Author

Replaced by #9019 to conform to this repository's documented contributor workflow and branch-naming convention. The replacement uses the same signed commit and identical code diff: #9019

@yinkev yinkev closed this Jul 25, 2026
@yinkev
yinkev deleted the fix/trace-transform-exception-state branch July 25, 2026 19:47
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