Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race detector detected race in heartbeat #9539

Merged
merged 1 commit into from
Dec 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions heartbeat/monitors/active/http/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net/http"
"strconv"
"strings"
"sync"
"time"

"github.com/elastic/beats/libbeat/common"
Expand Down Expand Up @@ -157,20 +158,37 @@ func createPingFactory(
var (
writeStart, readStart, writeEnd time.Time
)
// Ensure memory consistency for these callbacks.
// It seems they can be invoked still sometime after the request is done
cbMutex := sync.Mutex{}

client := &http.Client{
CheckRedirect: checkRedirect,
Timeout: timeout,
Transport: &SimpleTransport{
Dialer: dialer,
OnStartWrite: func() { writeStart = time.Now() },
OnEndWrite: func() { writeEnd = time.Now() },
OnStartRead: func() { readStart = time.Now() },
Dialer: dialer,
OnStartWrite: func() {
cbMutex.Lock()
writeStart = time.Now()
cbMutex.Unlock()
},
OnEndWrite: func() {
cbMutex.Lock()
writeEnd = time.Now()
cbMutex.Unlock()
},
OnStartRead: func() {
cbMutex.Lock()
readStart = time.Now()
cbMutex.Unlock()
},
},
}

_, end, result, err := execPing(client, request, body, timeout, validator)
event.DeepUpdate(result)
cbMutex.Lock()
defer cbMutex.Unlock()

if !readStart.IsZero() {
event.DeepUpdate(common.MapStr{
Expand Down