Ensure reference fileCPU is not overwritten

Would previously happen if memory profiling was already started.
Now using os.File.Stat beforehand to check if a handler of the file
is already active and confirm the status of profiling. This required
changing the type of the reference fileCPU from io.Closer to *os.File.
Asserting the type would have worked as well.
This commit is contained in:
ionutboangiu
2024-07-24 18:54:35 +03:00
committed by Dan Christian Bogos
parent 1c490a9020
commit 218ad92635
3 changed files with 27 additions and 18 deletions

View File

@@ -21,7 +21,6 @@ package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/signal"
@@ -354,7 +353,7 @@ func main() {
go singnalHandler(shdWg, shdChan)
var cS *cores.CoreService
var cpuProf io.Closer
var cpuProf *os.File
if *cpuProfDir != utils.EmptyString {
cpuPath := filepath.Join(*cpuProfDir, utils.CpuPathCgr)
cpuProf, err = cores.StartCPUProfiling(cpuPath)
@@ -371,7 +370,7 @@ func main() {
}
pprof.StopCPUProfile()
if err := cpuProf.Close(); err != nil {
log.Printf("could not close file %q: %v", cpuProf.(*os.File).Name(), err)
log.Print(err)
}
}()
}