Skip to content

fix: clean up failed pprof startup files#3216

Closed
buley wants to merge 3 commits intomicrosoft:mainfrom
buley:fix/pprof-startup-cleanup
Closed

fix: clean up failed pprof startup files#3216
buley wants to merge 3 commits intomicrosoft:mainfrom
buley:fix/pprof-startup-cleanup

Conversation

@buley
Copy link
Copy Markdown

@buley buley commented Mar 24, 2026

Summary

BeginProfiling creates the CPU profile file before calling runtime/pprof.StartCPUProfile.
If startup fails, it panics and leaves the partially created file behind even though CPUProfiler.StartCPUProfile already cleans up that path.

Fix

Use a shared start hook for both profiling entry points, close and remove the startup file before panicking from BeginProfiling, and add a unit test that forces the failure path and verifies cleanup.

Testing

  • go test ./internal/pprof

Related

Observe that BeginProfiling creates the CPU profile file before calling runtime/pprof.StartCPUProfile. If startup fails, it panics and leaves the file behind even though CPUProfiler.StartCPUProfile already cleans up that path.

Use a shared start hook, close and remove the file on startup failure, and add a unit test that forces the failure path and verifies cleanup.
Copilot AI review requested due to automatic review settings March 24, 2026 21:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a cleanup bug in the internal profiling helpers where BeginProfiling could leave behind a partially-created CPU profile file if runtime/pprof startup fails, and adds a regression test to lock the behavior in.

Changes:

  • Add a package-level test seam (startCPUProfile) so tests can force StartCPUProfile failure deterministically.
  • Ensure BeginProfiling closes and removes the CPU profile file before panicking when startup fails.
  • Add a unit test verifying the startup-failure cleanup path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/pprof/pprof.go Introduces a StartCPUProfile seam and uses it in both entry points; ensures failed startup removes the created CPU profile file.
internal/pprof/pprof_test.go Adds a regression test that forces startup failure and asserts the CPU profile file is removed.

@RyanCavanaugh RyanCavanaugh added this to the Possible Improvement milestone Apr 17, 2026
@RyanCavanaugh
Copy link
Copy Markdown
Member

Hardening internal debug/profiling codepaths against benign failures isn't a great use of churn/risk. It's fine if a local dev has to delete this file manually after killing the process or whatever.

@buley
Copy link
Copy Markdown
Author

buley commented Apr 21, 2026

Copy that - thanks for the clear answer @RyanCavanaugh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants