{"record":{"id":"f0f3e56881b0cd6c","repo":"gohugoio/hugo","slug":"failed-to-start-cpu-profile-w","errorCode":null,"errorMessage":"failed to start CPU profile: %w","messagePattern":"failed to start CPU profile: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"commands/hugobuilder.go","lineNumber":183,"sourceCode":"\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn hstrings.UniqueStringsSorted(h.PathSpec.BaseFs.WatchFilenames()), nil\n}\n\nfunc (c *hugoBuilder) initCPUProfile() (func(), error) {\n\tif c.r.cpuprofile == \"\" {\n\t\treturn nil, nil\n\t}\n\n\tf, err := os.Create(c.r.cpuprofile)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create CPU profile: %w\", err)\n\t}\n\tif err := pprof.StartCPUProfile(f); err != nil {\n\t\tf.Close()\n\t\treturn nil, fmt.Errorf(\"failed to start CPU profile: %w\", err)\n\t}\n\treturn func() {\n\t\tpprof.StopCPUProfile()\n\t\tf.Close()\n\t}, nil\n}\n\nfunc (c *hugoBuilder) initMemProfile() {\n\tif c.r.memprofile == \"\" {\n\t\treturn\n\t}\n\n\tf, err := os.Create(c.r.memprofile)\n\tif err != nil {\n\t\tc.r.logger.Errorf(\"could not create memory profile: \", err)\n\t}\n\tdefer f.Close()\n\truntime.GC() // get up-to-date statistics","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/commands/hugobuilder.go#L165-L201","documentation":"After the CPU profile file is created, Hugo calls `pprof.StartCPUProfile(f)`. This error wraps a failure from the Go runtime profiler, which most commonly occurs when profiling is already active in the current process. The created file is closed before returning the error.","triggerScenarios":"Triggered when `pprof.StartCPUProfile` returns non-nil — classically because another goroutine/component already started CPU profiling, or the runtime reports it cannot configure the profiler on this platform.","commonSituations":"Running Hugo embedded inside a larger Go binary that already invoked `pprof.StartCPUProfile`; a test harness or shared library that starts profiling before Hugo's `initProfiling` runs; re-entrant invocation.","solutions":["Ensure only one component in the process calls CPU profiling; remove or guard the other `pprof.StartCPUProfile` call.","Run `hugo` as a standalone process instead of embedding it where profiling is already active.","Omit `--cpuprofile` if profiling is being handled elsewhere."],"exampleFix":"// before\npprof.StartCPUProfile(f) // elsewhere in host app, still active\n// ...\nhugo.Build(...) // also sets --cpuprofile\n// after\npprof.StopCPUProfile() // stop the first profiler before building","handlingStrategy":"try-catch","validationCode":"// Ensure no other CPU profiler is active before invoking Hugo.\n// (pprof exposes no public IsRunning; track it in your host process.)\nvar cpuProfilingActive bool\nfunc safeStartCPUProfile(f *os.File) error {\n  if cpuProfilingActive { return fmt.Errorf(\"cpu profile already active\") }\n  if err := pprof.StartCPUProfile(f); err != nil { return err }\n  cpuProfilingActive = true\n  return nil\n}","typeGuard":"null","tryCatchPattern":"// Wrap the Hugo build call and surface the profiling conflict cleanly.\nerr := hugo.Build(cfg)\nif err != nil && strings.Contains(err.Error(), \"failed to start CPU profile\") {\n  // resolve the host-process profiler conflict, then retry without --cpuprofile\n}","preventionTips":["Run Hugo as a dedicated process when using `--cpuprofile`.","In embedded usage, centralize CPU profiling so only one caller ever starts it.","Call `pprof.StopCPUProfile` before re-entering the build pipeline."],"tags":["hugo","profiling","pprof","go"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}