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

Handle Multiple Graft Topics Correctly #386

Merged
merged 3 commits into from
Sep 10, 2020
Merged

Conversation

nisdas
Copy link
Contributor

@nisdas nisdas commented Sep 10, 2020

Background

With every iteration of the heartbeat routine for a peer running gossipsub, it will ascertain whether it has enough peers in its mesh for a topic. If there are not enough peers it will retrieve the relevant peers and add them to its mesh. Doing this for the same peer for multiple topics, we can batch all the GRAFT messages for the different topics into a single RPC message.

Problem

In sendGraftPrune while looping through all the topics to send a message out for, we take the pointer to string value of the topic and utilize that in our control message. Unfortunately in a loop that reference doesn't hold for each topic, instead changing for each iteration in a loop. So we instead end up referring to the last element in the original topic slice for all the GRAFT topic ids in our new ControlGraft message. To illustrate this:

https://play.golang.org/p/1cPEh5fYIvx

Solution

The solution is simple where we copy each topic before referencing them, this mitigates the problem with the other topics being overwritten in the message. Also a regression test has been added to test this exact case. There maybe more cases like this in the repo but I couldn't find anything similar so far when I scanned though the code.

Copy link
Collaborator

@vyzo vyzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch! Thanks for catching this! nasty critter.

@vyzo
Copy link
Collaborator

vyzo commented Sep 10, 2020

test fails with data race; can you run locally with -race and fix it?

@vyzo vyzo merged commit f7f33e1 into libp2p:master Sep 10, 2020
@nisdas nisdas deleted the copyGraftTopic branch September 10, 2020 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants