Skip to content

Commit

Permalink
Fix data race in mockbeat (#5037)
Browse files Browse the repository at this point in the history
Mockbeat expects Beater.Stop to be called after Beater.Run has
initialized some shared state. This is a race as Stop can be called at
any moment, even before Run is ever called.

This caused race warnings and segfaults when running unit tests.

Closes #5037
  • Loading branch information
adriansr committed Oct 24, 2017
1 parent 0b5e2d9 commit 97a4d76
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions libbeat/mock/mockbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ var Version = "9.9.9"
var Name = "mockbeat"

type Mockbeat struct {
done chan struct{}
client beat.Client
done chan struct{}
}

// Creates beater
Expand All @@ -28,15 +27,13 @@ func New(b *beat.Beat, _ *common.Config) (beat.Beater, error) {
/// *** Beater interface methods ***///

func (mb *Mockbeat) Run(b *beat.Beat) error {
var err error

mb.client, err = b.Publisher.Connect()
client, err := b.Publisher.Connect()
if err != nil {
return err
}

// Wait until mockbeat is done
mb.client.Publish(beat.Event{
go client.Publish(beat.Event{
Timestamp: time.Now(),
Fields: common.MapStr{
"type": "mock",
Expand All @@ -50,6 +47,5 @@ func (mb *Mockbeat) Run(b *beat.Beat) error {
func (mb *Mockbeat) Stop() {
logp.Info("Mockbeat Stop")

mb.client.Close()
close(mb.done)
}

0 comments on commit 97a4d76

Please sign in to comment.