gohugoio/hugo · error

failed to start CPU profile: %w

Error message

failed to start CPU profile: %w

What it means

Returned by initCPUProfile when pprof.StartCPUProfile(f) fails after the profile file was created. The runtime returns an error here chiefly when CPU profiling is already active in the process; Hugo closes the file and wraps the error.

Source

Thrown at commands/hugobuilder.go:183

	if err != nil {
		return nil, err
	}

	return hstrings.UniqueStringsSorted(h.PathSpec.BaseFs.WatchFilenames()), nil
}

func (c *hugoBuilder) initCPUProfile() (func(), error) {
	if c.r.cpuprofile == "" {
		return nil, nil
	}

	f, err := os.Create(c.r.cpuprofile)
	if err != nil {
		return nil, fmt.Errorf("failed to create CPU profile: %w", err)
	}
	if err := pprof.StartCPUProfile(f); err != nil {
		f.Close()
		return nil, fmt.Errorf("failed to start CPU profile: %w", err)
	}
	return func() {
		pprof.StopCPUProfile()
		f.Close()
	}, nil
}

func (c *hugoBuilder) initMemProfile() {
	if c.r.memprofile == "" {
		return
	}

	f, err := os.Create(c.r.memprofile)
	if err != nil {
		c.r.logger.Errorf("could not create memory profile: ", err)
	}
	defer f.Close()
	runtime.GC() // get up-to-date statistics

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Ensure only one CPU profile is active per process — remove any other StartCPUProfile call in an embedding program.
  2. Rerun the command; if it persists, run without --profile-cpu and report the underlying error.
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure no other CPU profile is already running in-process
// (pprof.StartCPUProfile fails if profiling is already active)

Try / catch

if err := b.build(); err != nil {
    if strings.Contains(err.Error(), "start CPU profile") {
        // another profiler is active; stop it or drop the flag
        pprof.StopCPUProfile()
    }
    return err
}

Prevention

When it happens

Trigger: pprof.StartCPUProfile returning non-nil — in practice only when a CPU profile is already running in the same process (double start), since the file itself was created successfully.

Common situations: Rare in normal CLI use; can appear if profiling is initialized twice (e.g. embedding Hugo's commands in another program that already started a CPU profile).

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/f0f3e56881b0cd6c.json. Report an issue: GitHub ↗.