Skip to content

Commit

Permalink
fix race condition on Honeycomb.Flush() (#140)
Browse files Browse the repository at this point in the history
Co-authored-by: Vera Reynolds <verareynolds@honeycomb.io>
  • Loading branch information
bfreis and vreynolds authored Sep 24, 2021
1 parent d6b54c4 commit ff38639
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion transmission/transmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ func (h *Honeycomb) Flush() (err error) {
// the old one (which has a side-effect of flushing the data) and make a new
// one. We start the new one and swap it with the old one so that we minimize
// the time we hold the musterLock for.
m := h.muster
newMuster := h.createMuster()
err = newMuster.Start()
if err != nil {
return err
}
h.musterLock.Lock()
m := h.muster
h.muster = newMuster
h.musterLock.Unlock()
return m.Stop()
Expand Down
31 changes: 31 additions & 0 deletions transmission/transmission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,37 @@ func TestHoneycombTransmissionFlush(t *testing.T) {
// Add concurrently with Flush. Before we added locks, the race detector
// would detect a race here.
})

t.Run("Flush should not race or panic if called from multiple goroutines", func(t *testing.T) {
w := new(Honeycomb)
w.MaxBatchSize = 1000
var wg sync.WaitGroup
wg.Add(2)

if err := w.Start(); err != nil {
t.Error("unable to start", err)
}
defer func() {
wg.Wait()
w.Stop()
}()
go func() {
defer wg.Done()
err := w.Flush()
if err != nil {
t.Error("unable to flush", err)
}
}()
go func() {
defer wg.Done()
err := w.Flush()
if err != nil {
t.Error("unable to flush", err)
}
}()
// This test makes sure that you can make concurrent calls to Flush.
// Race test used to panic here.
})
}

func TestHoneycombSenderAddingResponsesBlocking(t *testing.T) {
Expand Down

0 comments on commit ff38639

Please sign in to comment.