invoke-ai/InvokeAI · error · RuntimeError

Profiler not initialized. Call start() first.

Error message

Profiler not initialized. Call start() first.

What it means

Profiler.stop() dumps and saves cProfile stats, but only after start() created the internal cProfile.Profile. If stop() is called before start() (or stop() is called twice, since the profiler handle is consumed/cleared), there is nothing to stop and a RuntimeError is raised.

Source

Thrown at invokeai/app/util/profiler.py:56

        self._output_dir.mkdir(parents=True, exist_ok=True)
        self._profiler: Optional[cProfile.Profile] = None
        self._prefix = prefix

        self.profile_id: Optional[str] = None

    def start(self, profile_id: str) -> None:
        if self._profiler:
            self.stop()

        self.profile_id = profile_id

        self._profiler = cProfile.Profile()
        self._profiler.enable()
        self._logger.info(f"Started profiling {self.profile_id}.")

    def stop(self) -> Path:
        if not self._profiler:
            raise RuntimeError("Profiler not initialized. Call start() first.")
        self._profiler.disable()

        filename = f"{self._prefix}_{self.profile_id}.prof" if self._prefix else f"{self.profile_id}.prof"
        path = Path(self._output_dir, filename)

        self._profiler.dump_stats(path)
        self._logger.info(f"Stopped profiling, profile dumped to {path}.")
        self._profiler = None
        self.profile_id = None

        return path

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Call start() before stop() and ensure it succeeded
  2. Guard stop() with a check of the profiler state or a flag set by start()
  3. Ensure stop() is only invoked once per profiling session (e.g. idempotent wrapper or flag)
  4. Wrap the profiling block in try/finally so an exception in the measured code still runs start() first / stop() exactly once

Example fix

// before
profiler.stop()  # RuntimeError if never started
// after
if profiler._profiler:
    profiler.stop()
# or
profiler.start()
try:
    run_workload()
finally:
    profiler.stop()
Defensive patterns

Strategy: try-catch

Validate before calling

if not getattr(profiler, '_profiler', None):
    profiler.start()

Try / catch

try:
    profiler.stop()
except RuntimeError as e:
    if 'Profiler not initialized' in str(e):
        pass  # already stopped or never started
    else:
        raise

Prevention

When it happens

Trigger: Calling profiler.stop() without a prior start(); calling stop() a second time after the first stop already tore down the profiler handle.

Common situations: Exception during profiled work skips start() but a finally/cleanup block still calls stop(); double invocation of stop in shutdown code; constructing Profiler and immediately stopping it in tests.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/776ad83f0bd754c4. Report an issue: GitHub.