Skip to content

Commit

Permalink
fix: fix race, do not try to upload empty cpu profile (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
korniltsev authored Oct 26, 2023
1 parent aefcd7f commit d4c3674
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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 @@ -210,7 +211,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 @@ -416,6 +421,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 @@ -424,6 +430,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 @@ -433,7 +443,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 d4c3674

Please sign in to comment.