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: avoid panic when peer is blacklisted after connection #434

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion blacklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func TestMapBlacklist(t *testing.T) {
if !b.Contains(p) {
t.Fatal("peer not in the blacklist")
}

}

func TestTimeCachedBlacklist(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ func (p *PubSub) processLoop(ctx context.Context) {

case s := <-p.newPeerStream:
pid := s.Conn().RemotePeer()

ch, ok := p.peers[pid]

if !ok {
log.Warn("new stream for unknown peer: ", pid)
s.Reset()
Expand All @@ -504,6 +504,7 @@ func (p *PubSub) processLoop(ctx context.Context) {
if p.blacklist.Contains(pid) {
log.Warn("closing stream for blacklisted peer: ", pid)
close(ch)
delete(p.peers, pid)
s.Reset()
continue
}
Expand Down
49 changes: 49 additions & 0 deletions pubsub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pubsub

import (
"context"
"testing"
"time"
)

// See https://github.com/libp2p/go-libp2p-pubsub/issues/426
func TestPubSubRemovesBlacklistedPeer(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

hosts := getNetHosts(t, ctx, 2)

bl := NewMapBlacklist()

psubs0 := getPubsub(ctx, hosts[0])
psubs1 := getPubsub(ctx, hosts[1], WithBlacklist(bl))
connect(t, hosts[0], hosts[1])

// Bad peer is blacklisted after it has connected.
// Calling p.BlacklistPeer directly does the right thing but we should also clean
// up the peer if it has been added the the blacklist by another means.
bl.Add(hosts[0].ID())

_, err := psubs0.Subscribe("test")
if err != nil {
t.Fatal(err)
}

sub1, err := psubs1.Subscribe("test")
if err != nil {
t.Fatal(err)
}

time.Sleep(time.Millisecond * 100)

psubs0.Publish("test", []byte("message"))

wctx, cancel2 := context.WithTimeout(ctx, 1*time.Second)
defer cancel2()

_, _ = sub1.Next(wctx)

// Explicitly cancel context so PubSub cleans up peer channels.
// Issue 426 reports a panic due to a peer channel being closed twice.
cancel()
time.Sleep(time.Millisecond * 100)
}