Lightning-AI/pytorch-lightning · error · ValueError

Attempting to stop recording an action ({action_name}) which

Error message

Attempting to stop recording an action ({action_name}) which was never started.

What it means

AdvancedProfiler.stop(action_name) looks up the named cProfile profiler in self.profiled_actions. If stop() is called for an action that has no corresponding start() call (or a differently named one), there is nothing to disable and it raises ValueError.

Source

Thrown at src/lightning/pytorch/profilers/advanced.py:85

                If you attempt to stop recording an action which was never started.
        """
        super().__init__(dirpath=dirpath, filename=filename)
        self.profiled_actions: dict[str, cProfile.Profile] = defaultdict(cProfile.Profile)
        self.line_count_restriction = line_count_restriction
        self.dump_stats = dump_stats

    @override
    def start(self, action_name: str) -> None:
        # Disable all profilers before starting a new one
        for pr in self.profiled_actions.values():
            pr.disable()
        self.profiled_actions[action_name].enable()

    @override
    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.")
        pr.disable()

    def _dump_stats(self, action_name: str, profile: cProfile.Profile) -> None:
        assert self.dirpath
        dst_filepath = os.path.join(self.dirpath, self._prepare_filename(action_name=action_name, extension=".prof"))
        dst_fs = get_filesystem(dst_filepath)
        dst_fs.mkdirs(self.dirpath, exist_ok=True)
        # temporarily save to local since pstats can only dump into a local file
        with (
            tempfile.TemporaryDirectory(prefix="test", suffix=str(rank_zero_only.rank), dir=os.getcwd()) as tmp_dir,
            dst_fs.open(dst_filepath, "wb") as dst_file,
        ):
            src_filepath = os.path.join(tmp_dir, "tmp.prof")
            profile.dump_stats(src_filepath)
            src_fs = get_filesystem(src_filepath)
            with src_fs.open(src_filepath, "rb") as src_file:
                dst_file.write(src_file.read())

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Ensure every stop(action) is paired with an identical start(action)
  2. Use a context manager or try/finally around start/stop so stop always runs after a successful start
  3. Check profiler.profiled_actions keys before calling stop to confirm the action is active

Example fix

# before
profiler.start("my_action")
do_work()
profiler.stop("my_acton")  # typo -> ValueError

# after
profiler.start("my_action")
try:
    do_work()
finally:
    profiler.stop("my_action")
Defensive patterns

Strategy: validation

Validate before calling

if action_name not in profiler.profiled_actions:
    raise RuntimeError(f"action {action_name!r} not started; known: {list(profiler.profiled_actions)}")

Try / catch

profiler.start(action)
try:
    work()
finally:
    if action in profiler.profiled_actions:
        profiler.stop(action)

Prevention

When it happens

Trigger: Calling profiler.stop("foo") without a prior profiler.start("foo"); using mismatched names between start and stop; nested libraries that stop profiling actions they never started.

Common situations: Manually instrumenting a code block and typo'ing the action name in stop(); calling Lightning's profiler hooks out of order (stop before start) in custom loops.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/a6dc002f0843fb5f. Report an issue: GitHub.