Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 565cecd

Browse files
committedFeb 26, 2025··
sweepbatcher: fix race conditions in UseLogger
1 parent 95a534d commit 565cecd

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-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+
log().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: 10 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,12 @@ 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)
3137
}

‎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+
log().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+
log().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+
log().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+
log().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+
log().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+
log().Errorf("unable to get parent batch for sweep %x:"+
632+
" %v", sweep.swapHash[:6], err)
629633

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

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

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

785789
if len(dbSweeps) == 0 {
786-
log.Infof("skipping restored batch %d as it has no sweeps",
790+
log().Infof("skipping restored batch %d as it has no sweeps",
787791
batch.id)
788792

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

@@ -931,7 +935,7 @@ func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
931935
b.wg.Add(1)
932936
go func() {
933937
defer b.wg.Done()
934-
log.Infof("Batcher monitoring spend for swap %x",
938+
log().Infof("Batcher monitoring spend for swap %x",
935939
sweep.swapHash[:6])
936940

937941
for {
@@ -1110,7 +1114,7 @@ func (b *Batcher) loadSweep(ctx context.Context, swapHash lntypes.Hash,
11101114
}
11111115
} else {
11121116
if s.ConfTarget == 0 {
1113-
log.Warnf("Fee estimation was requested for zero "+
1117+
log().Warnf("Fee estimation was requested for zero "+
11141118
"confTarget for sweep %x.", swapHash[:6])
11151119
}
11161120
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
@@ -1373,7 +1373,7 @@ func testMaxSweepsPerBatch(t *testing.T, store testStore,
13731373
batcherStore testBatcherStore) {
13741374

13751375
// Disable logging, because this test is very noisy.
1376-
oldLogger := log
1376+
oldLogger := log()
13771377
UseLogger(build.NewSubLogger("SWEEP", nil))
13781378
defer UseLogger(oldLogger)
13791379

0 commit comments

Comments
 (0)
Please sign in to comment.