Skip to content

Commit 471b72f

Browse files
authored
Merge pull request #624 from libp2p/fix/identify-context
Use cancelable background context in identify
2 parents a1bae0a + b8616c6 commit 471b72f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

p2p/host/basic/basic_host.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,16 @@ type HostOpts struct {
115115

116116
// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
117117
func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, error) {
118+
bgctx, cancel := context.WithCancel(ctx)
119+
118120
h := &BasicHost{
119121
network: net,
120122
mux: msmux.NewMultistreamMuxer(),
121123
negtimeout: DefaultNegotiationTimeout,
122124
AddrsFactory: DefaultAddrsFactory,
123125
maResolver: madns.DefaultResolver,
126+
ctx: bgctx,
127+
cancel: cancel,
124128
}
125129

126130
h.proc = goprocessctx.WithContextAndTeardown(ctx, func() error {
@@ -138,7 +142,7 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost,
138142
h.ids = opts.IdentifyService
139143
} else {
140144
// we can't set this as a default above because it depends on the *BasicHost.
141-
h.ids = identify.NewIDService(ctx, h)
145+
h.ids = identify.NewIDService(bgctx, h)
142146
}
143147

144148
if uint64(opts.NegotiationTimeout) != 0 {
@@ -171,10 +175,6 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost,
171175
net.SetConnHandler(h.newConnHandler)
172176
net.SetStreamHandler(h.newStreamHandler)
173177

174-
bgctx, cancel := context.WithCancel(ctx)
175-
h.ctx = bgctx
176-
h.cancel = cancel
177-
178178
return h, nil
179179
}
180180

0 commit comments

Comments
 (0)