Skip to content

Commit 6106c1a

Browse files
testwilldevelopStorm
authored andcommitted
fix: close CPU profile
Signed-off-by: guoguangwu <[email protected]>
1 parent 990c9c3 commit 6106c1a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

bin/bin.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func dumpHeapProfile() {
6161
// If CPU profiling is enabled (ZGRAB2_CPUPROFILE is not empty), start tracking
6262
// CPU profiling in the configured file. Caller is responsible for invoking
6363
// stopCPUProfile() when finished.
64-
func startCPUProfile() {
64+
func startCPUProfile() *os.File {
6565
if file := getCPUProfileFile(); file != "" {
6666
now := time.Now()
6767
fullFile := getFormattedFile(file, now)
@@ -72,24 +72,30 @@ func startCPUProfile() {
7272
if err := pprof.StartCPUProfile(f); err != nil {
7373
log.Fatal("could not start CPU profile: ", err)
7474
}
75+
return f
7576
}
77+
78+
return nil
7679
}
7780

7881
// If CPU profiling is enabled (ZGRAB2_CPUPROFILE is not empty), stop profiling
7982
// CPU usage.
80-
func stopCPUProfile() {
83+
func stopCPUProfile(f *os.File) {
8184
if getCPUProfileFile() != "" {
8285
pprof.StopCPUProfile()
8386
}
87+
if f != nil {
88+
f.Close()
89+
}
8490
}
8591

8692
// ZGrab2Main should be called by func main() in a binary. The caller is
8793
// responsible for importing any modules in use. This allows clients to easily
8894
// include custom sets of scan modules by creating new main packages with custom
8995
// sets of ZGrab modules imported with side-effects.
9096
func ZGrab2Main() {
91-
startCPUProfile()
92-
defer stopCPUProfile()
97+
f := startCPUProfile()
98+
defer stopCPUProfile(f)
9399
defer dumpHeapProfile()
94100
_, moduleType, flag, err := zgrab2.ParseCommandLine(os.Args[1:])
95101

0 commit comments

Comments
 (0)