diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index a2bd21ed903e74a58eaa4738db8f08710e610419..d57cd13b43db75d6d55b27c7f1ce86f66a1f5447 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -115,6 +115,7 @@ type HostOpts struct { // NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network. func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, error) { + ctx, cancel := context.WithCancel(ctx) h := &BasicHost{ network: net, mux: msmux.NewMultistreamMuxer(), @@ -123,6 +124,14 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, maResolver: madns.DefaultResolver, } + h.proc = goprocess.WithTeardown(func() error { + if h.natmgr != nil { + h.natmgr.Close() + } + cancel() + return h.Network().Close() + }) + if opts.MultistreamMuxer != nil { h.mux = opts.MultistreamMuxer } @@ -162,25 +171,11 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, net.Notify(h.cmgr.Notifee()) } - var relayCtx context.Context - var relayCancel func() - - h.proc = goprocess.WithTeardown(func() error { - if h.natmgr != nil { - h.natmgr.Close() - } - if relayCancel != nil { - relayCancel() - } - return h.Network().Close() - }) - net.SetConnHandler(h.newConnHandler) net.SetStreamHandler(h.newStreamHandler) if opts.EnableRelay { - relayCtx, relayCancel = context.WithCancel(ctx) - err := circuit.AddRelayTransport(relayCtx, h, opts.RelayOpts...) + err := circuit.AddRelayTransport(ctx, h, opts.RelayOpts...) if err != nil { h.Close() return nil, err