Skip to content

Commit a333031

Browse files
committed
sweepbatcher: fix race conditions in UseLogger
1 parent a2cee86 commit a333031

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

sweepbatcher/greedy_batch_selection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ func (b *Batcher) greedyAddSweep(ctx context.Context, sweep *sweep) error {
9292
return nil
9393
}
9494

95-
log.Debugf("Batch selection algorithm returned batch id %d for"+
96-
" sweep %x, but acceptance failed.", batchId,
95+
debugf("Batch selection algorithm returned batch id %d "+
96+
"for sweep %x, but acceptance failed.", batchId,
9797
sweep.swapHash[:6])
9898
}
9999

sweepbatcher/log.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ package sweepbatcher
22

33
import (
44
"fmt"
5+
"sync/atomic"
56

67
"github.com/btcsuite/btclog"
78
"github.com/lightningnetwork/lnd/build"
89
)
910

10-
// log is a logger that is initialized with no output filters. This
11+
// log_ is a logger that is initialized with no output filters. This
1112
// means the package will not perform any logging by default until the
1213
// caller requests it.
13-
var log btclog.Logger
14+
var log_ atomic.Pointer[btclog.Logger]
15+
16+
// log returns active logger.
17+
func log() btclog.Logger {
18+
return *log_.Load()
19+
}
1420

1521
// The default amount of logging is none.
1622
func init() {
@@ -20,12 +26,32 @@ func init() {
2026
// batchPrefixLogger returns a logger that prefixes all log messages with
2127
// the ID.
2228
func batchPrefixLogger(batchID string) btclog.Logger {
23-
return build.NewPrefixLog(fmt.Sprintf("[Batch %s]", batchID), log)
29+
return build.NewPrefixLog(fmt.Sprintf("[Batch %s]", batchID), log())
2430
}
2531

2632
// UseLogger uses a specified Logger to output package logging info.
2733
// This should be used in preference to SetLogWriter if the caller is also
2834
// using btclog.
2935
func UseLogger(logger btclog.Logger) {
30-
log = logger
36+
log_.Store(&logger)
37+
}
38+
39+
// debugf logs a message with level DEBUG.
40+
func debugf(format string, params ...interface{}) {
41+
log().Debugf(format, params...)
42+
}
43+
44+
// infof logs a message with level INFO.
45+
func infof(format string, params ...interface{}) {
46+
log().Infof(format, params...)
47+
}
48+
49+
// warnf logs a message with level WARN.
50+
func warnf(format string, params ...interface{}) {
51+
log().Warnf(format, params...)
52+
}
53+
54+
// errorf logs a message with level ERROR.
55+
func errorf(format string, params ...interface{}) {
56+
log().Errorf(format, params...)
3157
}

sweepbatcher/sweep_batcher.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,15 @@ func (b *Batcher) Run(ctx context.Context) error {
535535
case sweepReq := <-b.sweepReqs:
536536
sweep, err := b.fetchSweep(runCtx, sweepReq)
537537
if err != nil {
538-
log.Warnf("fetchSweep failed: %v.", err)
538+
warnf("fetchSweep failed: %v.", err)
539+
539540
return err
540541
}
541542

542543
err = b.handleSweep(runCtx, sweep, sweepReq.Notifier)
543544
if err != nil {
544-
log.Warnf("handleSweep failed: %v.", err)
545+
warnf("handleSweep failed: %v.", err)
546+
545547
return err
546548
}
547549

@@ -550,11 +552,13 @@ func (b *Batcher) Run(ctx context.Context) error {
550552
close(testReq.quit)
551553

552554
case err := <-b.errChan:
553-
log.Warnf("Batcher received an error: %v.", err)
555+
warnf("Batcher received an error: %v.", err)
556+
554557
return err
555558

556559
case <-runCtx.Done():
557-
log.Infof("Stopping Batcher: run context cancelled.")
560+
infof("Stopping Batcher: run context cancelled.")
561+
558562
return runCtx.Err()
559563
}
560564
}
@@ -612,8 +616,8 @@ func (b *Batcher) handleSweep(ctx context.Context, sweep *sweep,
612616
return err
613617
}
614618

615-
log.Infof("Batcher handling sweep %x, completed=%v", sweep.swapHash[:6],
616-
completed)
619+
infof("Batcher handling sweep %x, completed=%v",
620+
sweep.swapHash[:6], completed)
617621

618622
// If the sweep has already been completed in a confirmed batch then we
619623
// can't attach its notifier to the batch as that is no longer running.
@@ -624,8 +628,8 @@ func (b *Batcher) handleSweep(ctx context.Context, sweep *sweep,
624628
// on-chain confirmations to prevent issues caused by reorgs.
625629
parentBatch, err := b.store.GetParentBatch(ctx, sweep.swapHash)
626630
if err != nil {
627-
log.Errorf("unable to get parent batch for sweep %x: "+
628-
"%v", sweep.swapHash[:6], err)
631+
errorf("unable to get parent batch for sweep %x:"+
632+
" %v", sweep.swapHash[:6], err)
629633

630634
return err
631635
}
@@ -676,8 +680,8 @@ func (b *Batcher) handleSweep(ctx context.Context, sweep *sweep,
676680
return nil
677681
}
678682

679-
log.Warnf("Greedy batch selection algorithm failed for sweep %x: %v. "+
680-
"Falling back to old approach.", sweep.swapHash[:6], err)
683+
warnf("Greedy batch selection algorithm failed for sweep %x: %v."+
684+
" Falling back to old approach.", sweep.swapHash[:6], err)
681685

682686
// If one of the batches accepts the sweep, we provide it to that batch.
683687
for _, batch := range b.batches {
@@ -782,13 +786,13 @@ func (b *Batcher) spinUpBatchFromDB(ctx context.Context, batch *batch) error {
782786
}
783787

784788
if len(dbSweeps) == 0 {
785-
log.Infof("skipping restored batch %d as it has no sweeps",
789+
infof("skipping restored batch %d as it has no sweeps",
786790
batch.id)
787791

788792
// It is safe to drop this empty batch as it has no sweeps.
789793
err := b.store.DropBatch(ctx, batch.id)
790794
if err != nil {
791-
log.Warnf("unable to drop empty batch %d: %v",
795+
warnf("unable to drop empty batch %d: %v",
792796
batch.id, err)
793797
}
794798

@@ -930,7 +934,7 @@ func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
930934
b.wg.Add(1)
931935
go func() {
932936
defer b.wg.Done()
933-
log.Infof("Batcher monitoring spend for swap %x",
937+
infof("Batcher monitoring spend for swap %x",
934938
sweep.swapHash[:6])
935939

936940
for {
@@ -1109,7 +1113,7 @@ func (b *Batcher) loadSweep(ctx context.Context, swapHash lntypes.Hash,
11091113
}
11101114
} else {
11111115
if s.ConfTarget == 0 {
1112-
log.Warnf("Fee estimation was requested for zero "+
1116+
warnf("Fee estimation was requested for zero "+
11131117
"confTarget for sweep %x.", swapHash[:6])
11141118
}
11151119
minFeeRate, err = b.wallet.EstimateFeeRate(ctx, s.ConfTarget)

sweepbatcher/sweep_batcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ func testMaxSweepsPerBatch(t *testing.T, store testStore,
13821382
batcherStore testBatcherStore) {
13831383

13841384
// Disable logging, because this test is very noisy.
1385-
oldLogger := log
1385+
oldLogger := log()
13861386
UseLogger(build.NewSubLogger("SWEEP", nil))
13871387
defer UseLogger(oldLogger)
13881388

0 commit comments

Comments
 (0)