Skip to content

Commit

Permalink
fix: fix race, do not try to upload empty cpu profile
Browse files Browse the repository at this point in the history
  • Loading branch information
korniltsev committed Oct 25, 2023
1 parent d2d4bcb commit b8da8c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
github.com/pyroscope-io/godeltaprof v0.1.2/go.mod h1:psMITXp90+8pFenXkKIpNhrfmI9saQnPbba27VIaiQE=
14 changes: 12 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Session struct {
logger Logger
stopOnce sync.Once
stopCh chan struct{}
wg sync.WaitGroup
flushCh chan *flush
trieMutex sync.Mutex

Expand Down Expand Up @@ -203,7 +204,11 @@ func (ps *Session) Start() error {
t := ps.truncatedTime()
ps.reset(t, t)

go ps.takeSnapshots()
ps.wg.Add(1)
go func() {
defer ps.wg.Done()
ps.takeSnapshots()
}()
return nil
}

Expand Down Expand Up @@ -409,6 +414,7 @@ func (ps *Session) Stop() {
ps.stopOnce.Do(func() {
// TODO: wait for stopCh consumer to finish!
close(ps.stopCh)
ps.wg.Wait()
// before stopping, upload the tries
ps.uploadLastBitOfData(time.Now())
})
Expand All @@ -417,6 +423,10 @@ func (ps *Session) Stop() {
func (ps *Session) uploadLastBitOfData(now time.Time) {
if ps.isCPUEnabled() {
pprof.StopCPUProfile()
prof := ps.cpuBuf.Bytes()
if len(prof) == 0 {
return
}
ps.upstream.Upload(&upstream.UploadJob{
Name: ps.appName,
StartTime: ps.startTime,
Expand All @@ -426,7 +436,7 @@ func (ps *Session) uploadLastBitOfData(now time.Time) {
Units: "samples",
AggregationType: "sum",
Format: upstream.FormatPprof,
Profile: copyBuf(ps.cpuBuf.Bytes()),
Profile: copyBuf(prof),
})
}
}
Expand Down

0 comments on commit b8da8c5

Please sign in to comment.