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

mark the peer as dead if the inbound stream closes #39

Merged
merged 3 commits into from
Oct 14, 2017
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
18 changes: 8 additions & 10 deletions comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (p *PubSub) handleNewStream(s inet.Stream) {
// but it doesn't hurt to send it.
s.Close()
}
select {
case p.peerDead <- s.Conn().RemotePeer():
case <-p.ctx.Done():
}
return
}

Expand All @@ -54,7 +58,6 @@ func (p *PubSub) handleNewStream(s inet.Stream) {
}

func (p *PubSub) handleSendingMessages(ctx context.Context, s inet.Stream, outgoing <-chan *RPC) {
var dead bool
bufw := bufio.NewWriter(s)
wc := ggio.NewDelimitedWriter(bufw)

Expand All @@ -74,21 +77,16 @@ func (p *PubSub) handleSendingMessages(ctx context.Context, s inet.Stream, outgo
if !ok {
return
}
if dead {
Copy link
Member Author

Choose a reason for hiding this comment

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

Shouldn't need this anymore (I believe this existed as a hack to prevent some deadlocks but those are no longer possible).

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you are correct. This was because sends on the other end of this outgoing channel could get filled up and block the loops. All those sends are now coupled with default selects (though please double check this for me).

// continue in order to drain messages
continue
}

err := writeMsg(&rpc.RPC)
if err != nil {
s.Reset()
log.Warningf("writing message to %s: %s", s.Conn().RemotePeer(), err)
dead = true
go func() {
p.peerDead <- s.Conn().RemotePeer()
}()
select {
case p.peerDead <- s.Conn().RemotePeer():
case <-ctx.Done():
}
}

case <-ctx.Done():
return
}
Expand Down
8 changes: 8 additions & 0 deletions floodsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func NewFloodSub(ctx context.Context, h host.Host) *PubSub {

// processLoop handles all inputs arriving on the channels
func (p *PubSub) processLoop(ctx context.Context) {
defer func() {
// Clean up go routines.
for _, ch := range p.peers {
close(ch)
}
p.peers = nil
p.topics = nil
}()
for {
select {
case s := <-p.newPeers:
Expand Down
39 changes: 39 additions & 0 deletions floodsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,45 @@ func TestSubscribeMultipleTimes(t *testing.T) {
}
}

func TestPeerDisconnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hosts := getNetHosts(t, ctx, 2)
psubs := getPubsubs(ctx, hosts)

connect(t, hosts[0], hosts[1])

_, err := psubs[0].Subscribe("foo")
if err != nil {
t.Fatal(err)
}

_, err = psubs[1].Subscribe("foo")
if err != nil {
t.Fatal(err)
}

time.Sleep(time.Millisecond * 10)

peers := psubs[0].ListPeers("foo")
assertPeerList(t, peers, hosts[1].ID())
for _, c := range hosts[1].Network().ConnsToPeer(hosts[0].ID()) {
streams, err := c.GetStreams()
if err != nil {
t.Fatal(err)
}
for _, s := range streams {
s.Close()
}
}

time.Sleep(time.Millisecond * 10)

peers = psubs[0].ListPeers("foo")
assertPeerList(t, peers)
}

func assertPeerList(t *testing.T, peers []peer.ID, expected ...peer.ID) {
sort.Sort(peer.IDSlice(peers))
sort.Sort(peer.IDSlice(expected))
Expand Down