Skip to content

Commit a246b3e

Browse files
committed
lnd+triggerforceclose: close DB correctly
1 parent 7bdc85b commit a246b3e

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

cmd/chantools/triggerforceclose.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ func noiseDial(idKey keychain.SingleKeyECDH, lnAddr *lnwire.NetAddress,
311311
}
312312

313313
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+
}
315319

316320
var dialNet tor.Net = &tor.ClearNet{}
317321
if torProxy != "" {
@@ -328,7 +332,8 @@ func connectPeer(peerHost, torProxy string, identity keychain.SingleKeyECDH,
328332
peerHost, "9735", dialNet.ResolveTCPAddr,
329333
)
330334
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)
332337
}
333338

334339
peerPubKey := peerAddr.IdentityKey
@@ -337,7 +342,11 @@ func connectPeer(peerHost, torProxy string, identity keychain.SingleKeyECDH,
337342
peerAddr.String())
338343
conn, err := noiseDial(identity, peerAddr, dialNet, dialTimeout)
339344
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()
341350
}
342351

343352
log.Infof("Attempting to establish p2p connection to peer %x, dial"+
@@ -346,9 +355,20 @@ func connectPeer(peerHost, torProxy string, identity keychain.SingleKeyECDH,
346355
Addr: peerAddr,
347356
Permanent: false,
348357
}
349-
p, err := lnd.ConnectPeer(conn, req, chainParams, identity)
358+
p, channelDB, err := lnd.ConnectPeer(conn, req, chainParams, identity)
350359
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()
352372
}
353373

354374
log.Infof("Connection established to peer %x",
@@ -358,17 +378,23 @@ func connectPeer(peerHost, torProxy string, identity keychain.SingleKeyECDH,
358378
select {
359379
case <-p.ActiveSignal():
360380
case <-p.QuitSignal():
361-
return nil, fmt.Errorf("peer %x disconnected",
381+
return nil, cleanup, fmt.Errorf("peer %x disconnected",
362382
peerPubKey.SerializeCompressed())
363383
}
364384

365-
return p, nil
385+
return p, cleanup, nil
366386
}
367387

368388
func requestForceClose(peerHost, torProxy string, channelPoint wire.OutPoint,
369389
identity keychain.SingleKeyECDH) error {
370390

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+
372398
if err != nil {
373399
return fmt.Errorf("error connecting to peer: %w", err)
374400
}
@@ -405,6 +431,9 @@ func requestForceClose(peerHost, torProxy string, channelPoint wire.OutPoint,
405431
return fmt.Errorf("error sending message: %w", err)
406432
}
407433

434+
// Wait a few seconds to give the peer time to process the message.
435+
time.Sleep(5 * time.Second)
436+
408437
return nil
409438
}
410439

lnd/brontide.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lnd
33
import (
44
"errors"
55
"fmt"
6+
"math/rand"
67
"os"
78
"time"
89

@@ -53,11 +54,12 @@ var (
5354

5455
func ConnectPeer(conn *brontide.Conn, connReq *connmgr.ConnReq,
5556
netParams *chaincfg.Params,
56-
identityECDH keychain.SingleKeyECDH) (*peer.Brontide, error) {
57+
identityECDH keychain.SingleKeyECDH) (*peer.Brontide, *channeldb.DB,
58+
error) {
5759

5860
featureMgr, err := feature.NewManager(feature.Config{})
5961
if err != nil {
60-
return nil, err
62+
return nil, nil, err
6163
}
6264

6365
initFeatures := featureMgr.Get(feature.SetInit)
@@ -72,7 +74,7 @@ func ConnectPeer(conn *brontide.Conn, connReq *connmgr.ConnReq,
7274
}
7375
errBuffer, err := queue.NewCircularBuffer(500)
7476
if err != nil {
75-
return nil, err
77+
return nil, nil, err
7678
}
7779

7880
pongBuf := make([]byte, lnwire.MaxPongBytes)
@@ -99,27 +101,31 @@ func ConnectPeer(conn *brontide.Conn, connReq *connmgr.ConnReq,
99101
)
100102

101103
if err := writePool.Start(); err != nil {
102-
return nil, fmt.Errorf("unable to start write pool: %w", err)
104+
return nil, nil, fmt.Errorf("unable to start write pool: %w",
105+
err)
103106
}
104107
if err := readPool.Start(); err != nil {
105-
return nil, fmt.Errorf("unable to start read pool: %w", err)
108+
return nil, nil, fmt.Errorf("unable to start read pool: %w",
109+
err)
106110
}
107111

112+
randNum := rand.Int31()
108113
backend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{
109114
DBPath: os.TempDir(),
110-
DBFileName: "channel.db",
115+
DBFileName: fmt.Sprintf("channel-%d.db", randNum),
111116
NoFreelistSync: true,
112117
AutoCompact: false,
113118
AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge,
114119
DBTimeout: kvdb.DefaultDBTimeout,
115120
})
116121
if err != nil {
117-
return nil, err
122+
return nil, nil, err
118123
}
119124

120125
channelDB, err := channeldb.CreateWithBackend(backend)
121126
if err != nil {
122-
return nil, err
127+
_ = backend.Close()
128+
return nil, nil, err
123129
}
124130

125131
gossiper := discovery.New(discovery.Config{
@@ -212,7 +218,8 @@ func ConnectPeer(conn *brontide.Conn, connReq *connmgr.ConnReq,
212218
},
213219
)
214220
if err != nil {
215-
return nil, fmt.Errorf("unable to create interceptable "+
221+
_ = channelDB.Close()
222+
return nil, nil, fmt.Errorf("unable to create interceptable "+
216223
"switch: %w", err)
217224
}
218225

@@ -322,8 +329,9 @@ func ConnectPeer(conn *brontide.Conn, connReq *connmgr.ConnReq,
322329

323330
p := peer.NewBrontide(pCfg)
324331
if err := p.Start(); err != nil {
325-
return nil, err
332+
_ = channelDB.Close()
333+
return nil, nil, err
326334
}
327335

328-
return p, nil
336+
return p, channelDB, nil
329337
}

0 commit comments

Comments
 (0)