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 data race in params.Entries chan #5

Merged
merged 2 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
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
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")
}
}