@@ -311,7 +311,11 @@ func noiseDial(idKey keychain.SingleKeyECDH, lnAddr *lnwire.NetAddress,
311
311
}
312
312
313
313
func connectPeer (peerHost , torProxy string , identity keychain.SingleKeyECDH ,
314
- dialTimeout time.Duration ) (* peer.Brontide , error ) {
314
+ dialTimeout time.Duration ) (* peer.Brontide , func () error , error ) {
315
+
316
+ cleanup := func () error {
317
+ return nil
318
+ }
315
319
316
320
var dialNet tor.Net = & tor.ClearNet {}
317
321
if torProxy != "" {
@@ -328,7 +332,8 @@ func connectPeer(peerHost, torProxy string, identity keychain.SingleKeyECDH,
328
332
peerHost , "9735" , dialNet .ResolveTCPAddr ,
329
333
)
330
334
if err != nil {
331
- return nil , fmt .Errorf ("error parsing peer address: %w" , err )
335
+ return nil , cleanup , fmt .Errorf ("error parsing peer address: " +
336
+ "%w" , err )
332
337
}
333
338
334
339
peerPubKey := peerAddr .IdentityKey
@@ -337,7 +342,11 @@ func connectPeer(peerHost, torProxy string, identity keychain.SingleKeyECDH,
337
342
peerAddr .String ())
338
343
conn , err := noiseDial (identity , peerAddr , dialNet , dialTimeout )
339
344
if err != nil {
340
- return nil , fmt .Errorf ("error dialing peer: %w" , err )
345
+ return nil , cleanup , fmt .Errorf ("error dialing peer: %w" , err )
346
+ }
347
+
348
+ cleanup = func () error {
349
+ return conn .Close ()
341
350
}
342
351
343
352
log .Infof ("Attempting to establish p2p connection to peer %x, dial" +
@@ -346,9 +355,20 @@ func connectPeer(peerHost, torProxy string, identity keychain.SingleKeyECDH,
346
355
Addr : peerAddr ,
347
356
Permanent : false ,
348
357
}
349
- p , err := lnd .ConnectPeer (conn , req , chainParams , identity )
358
+ p , channelDB , err := lnd .ConnectPeer (conn , req , chainParams , identity )
350
359
if err != nil {
351
- return nil , fmt .Errorf ("error connecting to peer: %w" , err )
360
+ return nil , cleanup , fmt .Errorf ("error connecting to peer: %w" ,
361
+ err )
362
+ }
363
+
364
+ cleanup = func () error {
365
+ p .Disconnect (errors .New ("done with peer" ))
366
+ if channelDB != nil {
367
+ if err := channelDB .Close (); err != nil {
368
+ log .Errorf ("Error closing channel DB: %v" , err )
369
+ }
370
+ }
371
+ return conn .Close ()
352
372
}
353
373
354
374
log .Infof ("Connection established to peer %x" ,
@@ -358,17 +378,23 @@ func connectPeer(peerHost, torProxy string, identity keychain.SingleKeyECDH,
358
378
select {
359
379
case <- p .ActiveSignal ():
360
380
case <- p .QuitSignal ():
361
- return nil , fmt .Errorf ("peer %x disconnected" ,
381
+ return nil , cleanup , fmt .Errorf ("peer %x disconnected" ,
362
382
peerPubKey .SerializeCompressed ())
363
383
}
364
384
365
- return p , nil
385
+ return p , cleanup , nil
366
386
}
367
387
368
388
func requestForceClose (peerHost , torProxy string , channelPoint wire.OutPoint ,
369
389
identity keychain.SingleKeyECDH ) error {
370
390
371
- p , err := connectPeer (peerHost , torProxy , identity , dialTimeout )
391
+ p , cleanup , err := connectPeer (
392
+ peerHost , torProxy , identity , dialTimeout ,
393
+ )
394
+ defer func () {
395
+ _ = cleanup ()
396
+ }()
397
+
372
398
if err != nil {
373
399
return fmt .Errorf ("error connecting to peer: %w" , err )
374
400
}
@@ -405,6 +431,9 @@ func requestForceClose(peerHost, torProxy string, channelPoint wire.OutPoint,
405
431
return fmt .Errorf ("error sending message: %w" , err )
406
432
}
407
433
434
+ // Wait a few seconds to give the peer time to process the message.
435
+ time .Sleep (5 * time .Second )
436
+
408
437
return nil
409
438
}
410
439
0 commit comments