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: use the goprocess for closing #669

Merged
merged 7 commits into from Jul 4, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -21,11 +21,11 @@ require (
github.com/libp2p/go-libp2p-netutil v0.1.0
github.com/libp2p/go-libp2p-peerstore v0.1.1
github.com/libp2p/go-libp2p-secio v0.1.0
github.com/libp2p/go-libp2p-swarm v0.1.0
github.com/libp2p/go-libp2p-swarm v0.1.1
github.com/libp2p/go-libp2p-testing v0.0.4
github.com/libp2p/go-libp2p-transport-upgrader v0.1.1
github.com/libp2p/go-libp2p-yamux v0.2.1
github.com/libp2p/go-maddr-filter v0.0.4
github.com/libp2p/go-maddr-filter v0.0.5
github.com/libp2p/go-stream-muxer-multistream v0.2.0
github.com/libp2p/go-tcp-transport v0.1.0
github.com/libp2p/go-ws-transport v0.1.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Expand Up @@ -138,6 +138,8 @@ github.com/libp2p/go-libp2p-secio v0.1.0 h1:NNP5KLxuP97sE5Bu3iuwOWyT/dKEGMN5zSLM
github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8=
github.com/libp2p/go-libp2p-swarm v0.1.0 h1:HrFk2p0awrGEgch9JXK/qp/hfjqQfgNxpLWnCiWPg5s=
github.com/libp2p/go-libp2p-swarm v0.1.0/go.mod h1:wQVsCdjsuZoc730CgOvh5ox6K8evllckjebkdiY5ta4=
github.com/libp2p/go-libp2p-swarm v0.1.1 h1:QW7pjyTRIxt9yyBid52YmMRGtkFXUE/rbEVWsQ0ae+w=
github.com/libp2p/go-libp2p-swarm v0.1.1/go.mod h1:4NVJaLwq/dr5kEq79Jo6pMin7ZFwLx73ln1FTefR91Q=
github.com/libp2p/go-libp2p-testing v0.0.2/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E=
github.com/libp2p/go-libp2p-testing v0.0.3 h1:bdij4bKaaND7tCsaXVjRfYkMpvoOeKj9AVQGJllA6jM=
github.com/libp2p/go-libp2p-testing v0.0.3/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E=
Expand All @@ -151,6 +153,8 @@ github.com/libp2p/go-libp2p-yamux v0.2.1 h1:Q3XYNiKCC2vIxrvUJL+Jg1kiyeEaIDNKLjgE
github.com/libp2p/go-libp2p-yamux v0.2.1/go.mod h1:1FBXiHDk1VyRM1C0aez2bCfHQ4vMZKkAQzZbkSQt5fI=
github.com/libp2p/go-maddr-filter v0.0.4 h1:hx8HIuuwk34KePddrp2mM5ivgPkZ09JH4AvsALRbFUs=
github.com/libp2p/go-maddr-filter v0.0.4/go.mod h1:6eT12kSQMA9x2pvFQa+xesMKUBlj9VImZbj3B9FBH/Q=
github.com/libp2p/go-maddr-filter v0.0.5 h1:CW3AgbMO6vUvT4kf87y4N+0P8KUl2aqLYhrGyDUbLSg=
github.com/libp2p/go-maddr-filter v0.0.5/go.mod h1:Jk+36PMfIqCJhAnaASRH83bdAvfDRp/w6ENFaC9bG+M=
github.com/libp2p/go-mplex v0.0.3/go.mod h1:pK5yMLmOoBR1pNCqDlA2GQrdAVTMkqFalaTWe7l4Yd0=
github.com/libp2p/go-mplex v0.1.0 h1:/nBTy5+1yRyY82YaO6HXQRnO5IAGsXTjEJaR3LdTPc0=
github.com/libp2p/go-mplex v0.1.0/go.mod h1:SXgmdki2kwCUlCCbfGLEgHjC4pFqhTp0ZoV6aiKgxDU=
Expand Down
9 changes: 8 additions & 1 deletion p2p/host/basic/basic_host.go
Expand Up @@ -146,6 +146,7 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo
if h.cmgr != nil {
h.cmgr.Close()
}
_ = h.emitters.evtLocalProtocolsUpdated.Close()
return h.Network().Close()
})

Expand Down Expand Up @@ -746,7 +747,13 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {

// Close shuts down the Host's services (network, etc).
func (h *BasicHost) Close() error {
_ = h.emitters.evtLocalProtocolsUpdated.Close()
// You're thinking of adding some teardown logic here, right? Well
// don't! Add any process teardown logic to the teardown function in the
// constructor.
//
// This:
// 1. May be called multiple times.
// 2. May _never_ be called if the host is stopped by the context.
return h.proc.Close()
}

Expand Down
18 changes: 18 additions & 0 deletions p2p/host/basic/basic_host_test.go
Expand Up @@ -23,6 +23,13 @@ import (
madns "github.com/multiformats/go-multiaddr-dns"
)

func TestHostDoubleClose(t *testing.T) {
ctx := context.Background()
h1 := New(swarmt.GenSwarm(t, ctx))
h1.Close()
h1.Close()
}

func TestHostSimple(t *testing.T) {
ctx := context.Background()
h1 := New(swarmt.GenSwarm(t, ctx))
Expand Down Expand Up @@ -129,6 +136,8 @@ func TestHostAddrsFactory(t *testing.T) {
}

func getHostPair(ctx context.Context, t *testing.T) (host.Host, host.Host) {
t.Helper()

h1 := New(swarmt.GenSwarm(t, ctx))
h2 := New(swarmt.GenSwarm(t, ctx))

Expand All @@ -141,6 +150,7 @@ func getHostPair(ctx context.Context, t *testing.T) (host.Host, host.Host) {
}

func assertWait(t *testing.T, c chan protocol.ID, exp protocol.ID) {
t.Helper()
select {
case proto := <-c:
if proto != exp {
Expand Down Expand Up @@ -199,6 +209,8 @@ func TestHostProtoPreference(t *testing.T) {
t.Fatal(err)
}

// XXX: This is racy now that we push protocol updates. If this tests
// fails, try allowing both protoOld and protoMinor.
assertWait(t, connectedOn, protoOld)

s2.Close()
Expand All @@ -208,6 +220,12 @@ func TestHostProtoPreference(t *testing.T) {
t.Fatal(err)
}

// Force a lazy handshake as we may have received a protocol update by this point.
_, err = s3.Write([]byte("hello"))
if err != nil {
t.Fatal(err)
}

assertWait(t, connectedOn, protoMinor)
s3.Close()
}
Expand Down
5 changes: 3 additions & 2 deletions p2p/net/mock/mock_test.go
Expand Up @@ -11,6 +11,7 @@ import (
"time"

ci "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/helpers"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
Expand Down Expand Up @@ -321,7 +322,7 @@ func TestStreams(t *testing.T) {
func makePinger(st string, n int) func(network.Stream) {
return func(s network.Stream) {
go func() {
defer s.Close()
defer helpers.FullClose(s)

for i := 0; i < n; i++ {
b := make([]byte, 4+len(st))
Expand All @@ -342,7 +343,7 @@ func makePinger(st string, n int) func(network.Stream) {
func makePonger(st string) func(network.Stream) {
return func(s network.Stream) {
go func() {
defer s.Close()
defer helpers.FullClose(s)

for {
b := make([]byte, 4+len(st))
Expand Down