@@ -164,22 +164,23 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
164
164
dialOpts = append (dialOpts , grpc .WithBlock ())
165
165
dialOpts = append (dialOpts , c .opts ... )
166
166
ctx := context .Background ()
167
- ctx , _ = context .WithTimeout (ctx , c .dialTimeout )
167
+ ctx , cancel := context .WithTimeout (ctx , c .dialTimeout )
168
+ defer cancel ()
168
169
cc , err = grpc .DialContext (ctx , endpoint , dialOpts ... )
169
170
if err != nil {
170
171
return nil , errors .WithStack (err )
171
172
}
172
173
173
174
cl := proto .NewGossipClient (cc )
174
175
175
- ctx , cancel : = context .WithTimeout (context .Background (), defConnTimeout )
176
+ ctx , cancel = context .WithTimeout (context .Background (), defConnTimeout )
176
177
defer cancel ()
177
178
if _ , err = cl .Ping (ctx , & proto.Empty {}); err != nil {
178
179
cc .Close ()
179
180
return nil , errors .WithStack (err )
180
181
}
181
182
182
- ctx , cf : = context .WithCancel (context .Background ())
183
+ ctx , cancel = context .WithCancel (context .Background ())
183
184
if stream , err = cl .GossipStream (ctx ); err == nil {
184
185
connInfo , err = c .authenticateRemotePeer (stream , true )
185
186
if err == nil {
@@ -194,14 +195,15 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
194
195
if ! bytes .Equal (actualOrg , oldOrg ) {
195
196
c .logger .Warning ("Remote endpoint claims to be a different peer, expected" , expectedPKIID , "but got" , pkiID )
196
197
cc .Close ()
198
+ cancel ()
197
199
return nil , errors .New ("authentication failure" )
198
200
}
199
201
}
200
202
conn := newConnection (cl , cc , stream , nil )
201
203
conn .pkiID = pkiID
202
204
conn .info = connInfo
203
205
conn .logger = c .logger
204
- conn .cancel = cf
206
+ conn .cancel = cancel
205
207
206
208
h := func (m * proto.SignedGossipMessage ) {
207
209
c .logger .Debug ("Got message:" , m )
@@ -218,6 +220,7 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
218
220
c .logger .Warningf ("Authentication failed: %+v" , err )
219
221
}
220
222
cc .Close ()
223
+ cancel ()
221
224
return nil , errors .WithStack (err )
222
225
}
223
226
@@ -271,15 +274,16 @@ func (c *commImpl) Probe(remotePeer *RemotePeer) error {
271
274
dialOpts = append (dialOpts , grpc .WithBlock ())
272
275
dialOpts = append (dialOpts , c .opts ... )
273
276
ctx := context .Background ()
274
- ctx , _ = context .WithTimeout (ctx , c .dialTimeout )
277
+ ctx , cancel := context .WithTimeout (ctx , c .dialTimeout )
278
+ defer cancel ()
275
279
cc , err := grpc .DialContext (ctx , remotePeer .Endpoint , dialOpts ... )
276
280
if err != nil {
277
281
c .logger .Debugf ("Returning %v" , err )
278
282
return err
279
283
}
280
284
defer cc .Close ()
281
285
cl := proto .NewGossipClient (cc )
282
- ctx , cancel : = context .WithTimeout (context .Background (), defConnTimeout )
286
+ ctx , cancel = context .WithTimeout (context .Background (), defConnTimeout )
283
287
defer cancel ()
284
288
_ , err = cl .Ping (ctx , & proto.Empty {})
285
289
c .logger .Debugf ("Returning %v" , err )
@@ -292,15 +296,16 @@ func (c *commImpl) Handshake(remotePeer *RemotePeer) (api.PeerIdentityType, erro
292
296
dialOpts = append (dialOpts , grpc .WithBlock ())
293
297
dialOpts = append (dialOpts , c .opts ... )
294
298
ctx := context .Background ()
295
- ctx , _ = context .WithTimeout (ctx , c .dialTimeout )
299
+ ctx , cancel := context .WithTimeout (ctx , c .dialTimeout )
300
+ defer cancel ()
296
301
cc , err := grpc .DialContext (ctx , remotePeer .Endpoint , dialOpts ... )
297
302
if err != nil {
298
303
return nil , err
299
304
}
300
305
defer cc .Close ()
301
306
302
307
cl := proto .NewGossipClient (cc )
303
- ctx , cancel : = context .WithTimeout (context .Background (), defConnTimeout )
308
+ ctx , cancel = context .WithTimeout (context .Background (), defConnTimeout )
304
309
defer cancel ()
305
310
if _ , err = cl .Ping (ctx , & proto.Empty {}); err != nil {
306
311
return nil , err
0 commit comments