Skip to content

Commit

Permalink
Add error handling in debug files
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Dec 17, 2024
1 parent 50a1915 commit 7873bf5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion debug/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func (h *HandlerT) StopCPUProfile() error {
defer h.mu.Unlock()
pprof.StopCPUProfile()
if h.cpuW == nil {
return errors.New("CPU profiling not in progress")
// Profiling was not in progress, so nothing to stop.
return nil
}
log.Info("Done writing CPU profile", "dump", h.cpuFile)
if err := h.cpuW.Close(); err != nil {
Expand Down
10 changes: 8 additions & 2 deletions debug/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package debug

import (
"errors"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -218,6 +219,11 @@ func StartPProf(address string, withMetrics bool) {
// Exit stops all running profiles, flushing their output to the
// respective file.
func Exit() {
Handler.StopCPUProfile()
Handler.StopGoTrace()
err := errors.Join(
Handler.StopCPUProfile(),
Handler.StopGoTrace(),
)
if err != nil {
log.Error("Failed to stop profiles", "err", err)
}
}
6 changes: 4 additions & 2 deletions debug/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package debug

import (
"errors"
"fmt"
"os"
"runtime/trace"

Expand Down Expand Up @@ -55,11 +56,12 @@ func (h *HandlerT) StopGoTrace() error {
defer h.mu.Unlock()
trace.Stop()
if h.traceW == nil {
return errors.New("trace not in progress")
// trace was not running, nothing to stop
return nil
}
log.Info("Done writing Go trace", "dump", h.traceFile)
if err := h.traceW.Close(); err != nil {
return err
return fmt.Errorf("failed to close trace file: %w", err)
}
h.traceW = nil
h.traceFile = ""
Expand Down

0 comments on commit 7873bf5

Please sign in to comment.