fix: clean up failed pprof startup files#3216
Open
buley wants to merge 1 commit intomicrosoft:mainfrom
Open
Conversation
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.
Contributor
There was a problem hiding this comment.
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 forceStartCPUProfilefailure deterministically. - Ensure
BeginProfilingcloses 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BeginProfilingcreates the CPU profile file before callingruntime/pprof.StartCPUProfile.If startup fails, it panics and leaves the partially created file behind even though
CPUProfiler.StartCPUProfilealready 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/pprofRelated