Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

fix(network): add mutex to avoid data race #127

Merged
merged 1 commit into from
May 22, 2019
Merged

Conversation

hannahhoward
Copy link
Contributor

@hannahhoward hannahhoward commented May 20, 2019

Goals

Fix #125

Implementation

Adds a mutex to the network interface default implementation's receiver member. This appears to be the source of the data races in #125. While it's not totally clear if this covers all of the race conditions, it clearly something that should happen, since receiver is referenced repeatedly across multiple goroutines. Also adds a nil check to the notification methods.

Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

IMO, we need to rethink this a bit. The BitswapNetwork should have a Start method to register with the Host. We'd call that from Bitswap after we finish setting everything up. That would make this lock unnecessary.

However, this works for now.

@@ -172,18 +176,26 @@ func (bsnet *impl) Provide(ctx context.Context, k cid.Cid) error {
func (bsnet *impl) handleNewStream(s inet.Stream) {
defer s.Close()

bsnet.receiverLk.RLock()
Copy link
Member

Choose a reason for hiding this comment

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

Let's just stash a copy of the receiver here. That way we don't have to keep re-locking (it shouldn't change, right?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that makes sense

@hannahhoward
Copy link
Contributor Author

Maybe calling SetDelegate actually it was binds the host and starts receiving messages? It is called at the very end of the BS setup already

@Stebalien
Copy link
Member

SGTM.

delay binding of network until a receiver is present. also add test of ipfs host network
@hannahhoward
Copy link
Contributor Author

@Stebalien re-implemented as suggested. I don't exactly know how to verify this is the right solution thought I did write a test and a cursory search shows most bitswap clients (cluster, ipfs, filecoin) initialize Bitswap shortly after initializing the network.

@Stebalien
Copy link
Member

This looks correct.

@Stebalien Stebalien merged commit e546588 into master May 22, 2019
@Stebalien
Copy link
Member

And thanks for writing a test! You're setting a good example for the rest of us.

@Stebalien Stebalien deleted the fix/race-125 branch May 22, 2019 17:56
Jorropo pushed a commit to Jorropo/go-libipfs that referenced this pull request Jan 26, 2023
fix(network): add mutex to avoid data race

This commit was moved from ipfs/go-bitswap@e546588
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data races in go-bitswap
2 participants