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

p2p/dnsdisc: fix crash when iterator closed before first call to Next #22906

Merged
merged 1 commit into from
May 20, 2021
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
6 changes: 6 additions & 0 deletions p2p/dnsdisc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ func (it *randomIterator) pickTree() *clientTree {
it.mu.Lock()
defer it.mu.Unlock()

// First check if iterator was closed.
// Need to do this here to avoid nil map access in rebuildTrees.
if it.trees == nil {
return nil
}

// Rebuild the trees map if any links have changed.
if it.lc.changed {
it.rebuildTrees()
Expand Down
15 changes: 15 additions & 0 deletions p2p/dnsdisc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ func TestIterator(t *testing.T) {
checkIterator(t, it, nodes)
}

func TestIteratorCloseWithoutNext(t *testing.T) {
tree1, url1 := makeTestTree("t1", nil, nil)
c := NewClient(Config{Resolver: newMapResolver(tree1.ToTXT("t1"))})
it, err := c.NewIterator(url1)
if err != nil {
t.Fatal(err)
}

it.Close()
ok := it.Next()
if ok {
t.Fatal("Next returned true after Close")
}
}

// This test checks if closing randomIterator races.
func TestIteratorClose(t *testing.T) {
nodes := testNodes(nodesSeed1, 500)
Expand Down