{"record":{"id":"a6dc002f0843fb5f","repo":"Lightning-AI/pytorch-lightning","slug":"attempting-to-stop-recording-an-action-action-na","errorCode":null,"errorMessage":"Attempting to stop recording an action ({action_name}) which was never started.","messagePattern":"Attempting to stop recording an action \\((.+?)\\) which was never started\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/profilers/advanced.py","lineNumber":85,"sourceCode":"                If you attempt to stop recording an action which was never started.\n        \"\"\"\n        super().__init__(dirpath=dirpath, filename=filename)\n        self.profiled_actions: dict[str, cProfile.Profile] = defaultdict(cProfile.Profile)\n        self.line_count_restriction = line_count_restriction\n        self.dump_stats = dump_stats\n\n    @override\n    def start(self, action_name: str) -> None:\n        # Disable all profilers before starting a new one\n        for pr in self.profiled_actions.values():\n            pr.disable()\n        self.profiled_actions[action_name].enable()\n\n    @override\n    def stop(self, action_name: str) -> None:\n        pr = self.profiled_actions.get(action_name)\n        if pr is None:\n            raise ValueError(f\"Attempting to stop recording an action ({action_name}) which was never started.\")\n        pr.disable()\n\n    def _dump_stats(self, action_name: str, profile: cProfile.Profile) -> None:\n        assert self.dirpath\n        dst_filepath = os.path.join(self.dirpath, self._prepare_filename(action_name=action_name, extension=\".prof\"))\n        dst_fs = get_filesystem(dst_filepath)\n        dst_fs.mkdirs(self.dirpath, exist_ok=True)\n        # temporarily save to local since pstats can only dump into a local file\n        with (\n            tempfile.TemporaryDirectory(prefix=\"test\", suffix=str(rank_zero_only.rank), dir=os.getcwd()) as tmp_dir,\n            dst_fs.open(dst_filepath, \"wb\") as dst_file,\n        ):\n            src_filepath = os.path.join(tmp_dir, \"tmp.prof\")\n            profile.dump_stats(src_filepath)\n            src_fs = get_filesystem(src_filepath)\n            with src_fs.open(src_filepath, \"rb\") as src_file:\n                dst_file.write(src_file.read())\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/profilers/advanced.py#L67-L103","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure every stop(action) is paired with an identical start(action)","Use a context manager or try/finally around start/stop so stop always runs after a successful start","Check profiler.profiled_actions keys before calling stop to confirm the action is active"],"exampleFix":"# before\nprofiler.start(\"my_action\")\ndo_work()\nprofiler.stop(\"my_acton\")  # typo -> ValueError\n\n# after\nprofiler.start(\"my_action\")\ntry:\n    do_work()\nfinally:\n    profiler.stop(\"my_action\")","handlingStrategy":"validation","validationCode":"if action_name not in profiler.profiled_actions:\n    raise RuntimeError(f\"action {action_name!r} not started; known: {list(profiler.profiled_actions)}\")","typeGuard":null,"tryCatchPattern":"profiler.start(action)\ntry:\n    work()\nfinally:\n    if action in profiler.profiled_actions:\n        profiler.stop(action)","preventionTips":["Always pair start/stop with try/finally","Define action-name constants to avoid typos","Prefer Lightning's profiler(...)/context APIs over manual start/stop"],"tags":["profiler","advanced-profiler","api-misuse","pytorch-lightning"],"backgroundTag":"unbalanced-start-stop-call","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}