Skip to content

Commit

Permalink
Merge pull request #5 from kpp/fix_data_race
Browse files Browse the repository at this point in the history
Fix data race in params.Entries chan
  • Loading branch information
Stebalien authored Aug 23, 2019
2 parents ef14215 + 0e33736 commit 23958d6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
9 changes: 3 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type ServiceEntry struct {
Addr net.IP // @Deprecated

hasTXT bool
sent bool
}

// complete is used to check if we have all the info we need
Expand Down Expand Up @@ -287,12 +286,10 @@ func (c *client) query(params *QueryParam) error {

// Check if this entry is complete
if inp.complete() {
if inp.sent {
continue
}
inp.sent = true
copyInp := *inp // copy inp because we send it into another thread
// which can cause data race
select {
case params.Entries <- inp:
case params.Entries <- &copyInp:
default:
}
} else {
Expand Down
7 changes: 4 additions & 3 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mdns
import (
"testing"
"time"
"sync/atomic"
)

func TestServer_StartStop(t *testing.T) {
Expand All @@ -22,7 +23,7 @@ func TestServer_Lookup(t *testing.T) {
defer serv.Shutdown()

entries := make(chan *ServiceEntry, 1)
found := false
var found uint32
go func() {
select {
case e := <-entries:
Expand All @@ -35,7 +36,7 @@ func TestServer_Lookup(t *testing.T) {
if e.Info != "Local web server" {
t.Fatalf("bad: %v", e)
}
found = true
atomic.AddUint32(&found, 1)

case <-time.After(80 * time.Millisecond):
t.Fatalf("timeout")
Expand All @@ -52,7 +53,7 @@ func TestServer_Lookup(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
if !found {
if atomic.LoadUint32(&found) != 1 {
t.Fatalf("record not found")
}
}

0 comments on commit 23958d6

Please sign in to comment.