{"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/8a468df065a75c1c7cf9f6850f32148746590ea5/commands/hugobuilder.go#L165-L201","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Ensure only one CPU profile is active per process — remove any other StartCPUProfile call in an embedding program.","Rerun the command; if it persists, run without --profile-cpu and report the underlying error."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// ensure no other CPU profile is already running in-process\n// (pprof.StartCPUProfile fails if profiling is already active)","typeGuard":null,"tryCatchPattern":"if err := b.build(); err != nil {\n    if strings.Contains(err.Error(), \"start CPU profile\") {\n        // another profiler is active; stop it or drop the flag\n        pprof.StopCPUProfile()\n    }\n    return err\n}","preventionTips":["Never enable Hugo's CPU profiling while your embedding program already calls pprof.StartCPUProfile","Only one CPU profile can run per process — coordinate profiling ownership","Gate profiling flags behind a dedicated debug mode, not defaults"],"tags":["hugo","profiling","pprof","runtime"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}