From 806e1ff9036254bc0ad91856fcff7a9807db7d48 Mon Sep 17 00:00:00 2001 From: pooyanazad Date: Sun, 28 Jun 2026 20:22:54 +0200 Subject: [PATCH 1/2] fix: AdvancedProfiler crash when stop() is called after teardown --- src/lightning/pytorch/profilers/advanced.py | 4 +++- .../tests_pytorch/profilers/test_profiler.py | 24 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/lightning/pytorch/profilers/advanced.py b/src/lightning/pytorch/profilers/advanced.py index c0b4b9953cc33..565385a2a7b9c 100644 --- a/src/lightning/pytorch/profilers/advanced.py +++ b/src/lightning/pytorch/profilers/advanced.py @@ -82,7 +82,7 @@ def start(self, action_name: str) -> None: def stop(self, action_name: str) -> None: pr = self.profiled_actions.get(action_name) if pr is None: - raise ValueError(f"Attempting to stop recording an action ({action_name}) which was never started.") + return pr.disable() def _dump_stats(self, action_name: str, profile: cProfile.Profile) -> None: @@ -116,6 +116,8 @@ def summary(self) -> str: @override def teardown(self, stage: Optional[str]) -> None: super().teardown(stage=stage) + for pr in self.profiled_actions.values(): + pr.disable() self.profiled_actions.clear() def __reduce__(self) -> tuple: diff --git a/tests/tests_pytorch/profilers/test_profiler.py b/tests/tests_pytorch/profilers/test_profiler.py index f6cf279e0c52f..de365c15d4d1f 100644 --- a/tests/tests_pytorch/profilers/test_profiler.py +++ b/tests/tests_pytorch/profilers/test_profiler.py @@ -375,13 +375,31 @@ def test_advanced_profiler_dump_states_sanitizes_filename(tmp_path, char): def test_advanced_profiler_value_errors(advanced_profiler): """Ensure errors are raised where expected.""" action = "test" - with pytest.raises(ValueError, match="Attempting to stop recording*"): - advanced_profiler.stop(action) - + # Starting an action already started does not raise an error since defaultdict handles it silently advanced_profiler.start(action) advanced_profiler.stop(action) +def test_advanced_profiler_stop_on_never_started_action(advanced_profiler): + """Ensure AdvancedProfiler.stop() does not crash when stopping an action that was never started. + + Regression test for https://github.com/Lightning-AI/pytorch-lightning/issues/9136 + """ + # Should not raise ValueError + advanced_profiler.stop("never_started_action") + + +def test_advanced_profiler_teardown_with_active_action(advanced_profiler): + """Ensure AdvancedProfiler.stop() does not crash after teardown clears profiled_actions. + + Regression test for https://github.com/Lightning-AI/pytorch-lightning/issues/9136 + """ + with advanced_profiler.profile("action"): + # teardown clears the recorded actions, mimicking describe() + advanced_profiler.teardown(stage=None) + # Exiting the 'with' block will call stop("action") which should not raise an error + + def test_advanced_profiler_deepcopy(advanced_profiler): advanced_profiler.describe() assert deepcopy(advanced_profiler) From 6b9b8358511ba93591268fdc86c6279d9a576579 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 28 Jun 2026 18:28:07 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/tests_pytorch/profilers/test_profiler.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/tests_pytorch/profilers/test_profiler.py b/tests/tests_pytorch/profilers/test_profiler.py index de365c15d4d1f..c3ccf7a57d4f6 100644 --- a/tests/tests_pytorch/profilers/test_profiler.py +++ b/tests/tests_pytorch/profilers/test_profiler.py @@ -384,6 +384,7 @@ def test_advanced_profiler_stop_on_never_started_action(advanced_profiler): """Ensure AdvancedProfiler.stop() does not crash when stopping an action that was never started. Regression test for https://github.com/Lightning-AI/pytorch-lightning/issues/9136 + """ # Should not raise ValueError advanced_profiler.stop("never_started_action") @@ -393,6 +394,7 @@ def test_advanced_profiler_teardown_with_active_action(advanced_profiler): """Ensure AdvancedProfiler.stop() does not crash after teardown clears profiled_actions. Regression test for https://github.com/Lightning-AI/pytorch-lightning/issues/9136 + """ with advanced_profiler.profile("action"): # teardown clears the recorded actions, mimicking describe()