kopia/kopia · error

could not create CPU profile output file

Error message

could not create CPU profile output file

What it means

During kopia's profiling start (cli/profile.go), the CPU profiler output file <profDir>/cpu.pprof is created with os.Create; when that fails the error is wrapped as "could not create CPU profile output file". This means the process could not open the file for writing in the configured profile directory.

Solutions

  1. Create the profile directory first (mkdir -p <profDir>) and verify it exists.
  2. Ensure the user running kopia has write permission on the profile directory.
  3. Free disk space or fix an invalid path passed via the profiling flag.
  4. Disable CPU profiling if you don't need it.

Example fix

// before
kopia --profile-cpu --profile-directory /nonexistent-dir server start

// after
mkdir -p /var/tmp/kopia-profiles
kopia --profile-cpu --profile-directory /var/tmp/kopia-profiles server start
Defensive patterns

Strategy: validation

Validate before calling

profDir := flagProfileDirectory
if info, err := os.Stat(profDir); err != nil || !info.IsDir() {
	os.MkdirAll(profDir, 0o755)
}
if f, err := os.CreateTemp(profDir, ".write-test"); err != nil {
	// directory not writable: fix permissions/path before enabling profiling
} else {
	f.Close(); os.Remove(f.Name())
}

Try / catch

if err := runKopia(args); err != nil {
	if strings.Contains(err.Error(), "could not create CPU profile output file") {
		// retry without profiling flags or with a writable profDir
	}
}

Prevention

When it happens

Trigger: Running kopia with CPU profiling enabled (--profile-cpu or equivalent) where os.Create(profDir/cpu.pprof) fails because the directory does not exist, is not writable, disk is full, or the path is invalid.

Common situations: Pointing the profile directory at a read-only filesystem or a path owned by another user; running in a container with a read-only root filesystem; disk-full conditions; typos in the --profile-directory flag or a stale path after a volume remount.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of kopia/kopia@82495e54b5 (2026-09-07). Data as JSON: /api/errors/73ee911f47ae05a8. Report an issue: GitHub.

Appendix: source

Thrown at cli/profile.go:91

		return nil
	}

	c.outputDirectory = outputDirectory

	// ensure upfront that the pprof output dir can be created.
	profDir, err := mkSubdirectories(c.outputDirectory, profDirName)
	if err != nil {
		return err
	}

	if !c.profileCPU {
		return nil
	}

	// start CPU profile dumper
	f, err := os.Create(filepath.Join(profDir, "cpu.pprof")) //nolint:gosec
	if err != nil {
		return errors.Wrap(err, "could not create CPU profile output file")
	}

	// CPU profile closer
	closer := func() {
		pprof.StopCPUProfile()

		if err := f.Close(); err != nil {
			log(ctx).Warn("error closing CPU profile output file:", err)
		}
	}

	if err := pprof.StartCPUProfile(f); err != nil {
		closer()

		return errors.Wrap(err, "could not start CPU profile")
	}

	c.cpuProfileCloser = closer

View on GitHub (pinned to 82495e54b5)