Merge pull request #7188 from guggero/go-func-vars

Don't access loop variables in goroutines directly
This commit is contained in:
Oliver Gugger authored and GitHub committed 2022-11-22 11:06:42 +01:00
commit ca501d920e
15 files changed
+46 -22

No files matched your search

+8 -4
View File
@@ -235,7 +235,9 @@ out:
// //
// TODO(wilmer): add retry logic if rescan fails? // TODO(wilmer): add retry logic if rescan fails?
b.wg.Add(1) b.wg.Add(1)
go func() {
//nolint:lll
go func(msg *chainntnfs.HistoricalConfDispatch) {
defer b.wg.Done() defer b.wg.Done()
confDetails, _, err := b.historicalConfDetails( confDetails, _, err := b.historicalConfDetails(
@@ -269,7 +271,7 @@ out:
"details of %v: %v", "details of %v: %v",
msg.ConfRequest, err) msg.ConfRequest, err)
} }
}() }(msg)
case *chainntnfs.HistoricalSpendDispatch: case *chainntnfs.HistoricalSpendDispatch:
// In order to ensure we don't block the caller // In order to ensure we don't block the caller
@@ -278,7 +280,9 @@ out:
// //
// TODO(wilmer): add retry logic if rescan fails? // TODO(wilmer): add retry logic if rescan fails?
b.wg.Add(1) b.wg.Add(1)
go func() {
//nolint:lll
go func(msg *chainntnfs.HistoricalSpendDispatch) {
defer b.wg.Done() defer b.wg.Done()
spendDetails, err := b.historicalSpendDetails( spendDetails, err := b.historicalSpendDetails(
@@ -320,7 +324,7 @@ out:
"details of %v: %v", "details of %v: %v",
msg.SpendRequest, err) msg.SpendRequest, err)
} }
}() }(msg)
case *blockEpochRegistration: case *blockEpochRegistration:
chainntnfs.Log.Infof("New block epoch subscription") chainntnfs.Log.Infof("New block epoch subscription")
+4 -2
View File
@@ -347,7 +347,9 @@ out:
// //
// TODO(wilmer): add retry logic if rescan fails? // TODO(wilmer): add retry logic if rescan fails?
b.wg.Add(1) b.wg.Add(1)
go func() {
//nolint:lll
go func(msg *chainntnfs.HistoricalConfDispatch) {
defer b.wg.Done() defer b.wg.Done()
confDetails, _, err := b.historicalConfDetails( confDetails, _, err := b.historicalConfDetails(
@@ -372,7 +374,7 @@ out:
if err != nil { if err != nil {
chainntnfs.Log.Error(err) chainntnfs.Log.Error(err)
} }
}() }(msg)
case *blockEpochRegistration: case *blockEpochRegistration:
chainntnfs.Log.Infof("New block epoch subscription") chainntnfs.Log.Infof("New block epoch subscription")
+4 -2
View File
@@ -432,7 +432,9 @@ func (n *NeutrinoNotifier) notificationDispatcher() {
// chain asynchronously to prevent blocking // chain asynchronously to prevent blocking
// potentially long rescans. // potentially long rescans.
n.wg.Add(1) n.wg.Add(1)
go func() {
//nolint:lll
go func(msg *chainntnfs.HistoricalConfDispatch) {
defer n.wg.Done() defer n.wg.Done()
confDetails, err := n.historicalConfDetails( confDetails, err := n.historicalConfDetails(
@@ -457,7 +459,7 @@ func (n *NeutrinoNotifier) notificationDispatcher() {
if err != nil { if err != nil {
chainntnfs.Log.Error(err) chainntnfs.Log.Error(err)
} }
}() }(msg)
case *blockEpochRegistration: case *blockEpochRegistration:
chainntnfs.Log.Infof("New block epoch subscription") chainntnfs.Log.Infof("New block epoch subscription")
+2
View File
@@ -1325,6 +1325,7 @@ func (d *AuthenticatedGossiper) networkHandler() {
d.wg.Add(1) d.wg.Add(1)
go func() { go func() {
defer d.wg.Done() defer d.wg.Done()
log.Infof("Broadcasting %v new announcements in %d sub batches", log.Infof("Broadcasting %v new announcements in %d sub batches",
len(announcementBatch), len(splitAnnouncementBatch)) len(announcementBatch), len(splitAnnouncementBatch))
@@ -2947,6 +2948,7 @@ func (d *AuthenticatedGossiper) handleAnnSig(nMsg *networkMsg,
d.wg.Add(1) d.wg.Add(1)
go func() { go func() {
defer d.wg.Done() defer d.wg.Done()
log.Debugf("Received half proof for channel "+ log.Debugf("Received half proof for channel "+
"%v with existing full proof. Sending"+ "%v with existing full proof. Sending"+
" full proof to peer=%x", " full proof to peer=%x",
@@ -198,6 +198,9 @@ certain large transactions](https://github.com/lightningnetwork/lnd/pull/7100).
* [test: fix loop variables being accessed in * [test: fix loop variables being accessed in
closures](https://github.com/lightningnetwork/lnd/pull/7032). closures](https://github.com/lightningnetwork/lnd/pull/7032).
* [Fix loop and other temporary variables being accessed in
goroutines](https://github.com/lightningnetwork/lnd/pull/7188).
## Watchtowers ## Watchtowers
+1
View File
@@ -1969,6 +1969,7 @@ func (f *Manager) handleFundingAccept(peer lnpeer.Peer,
f.wg.Add(1) f.wg.Add(1)
go func() { go func() {
defer f.wg.Done() defer f.wg.Done()
f.waitForPsbt(psbtIntent, resCtx, pendingChanID) f.waitForPsbt(psbtIntent, resCtx, pendingChanID)
}() }()
+3 -2
View File
@@ -71,10 +71,11 @@ func (m *Monitor) Start() error {
} }
m.wg.Add(1) m.wg.Add(1)
go func() { go func(check *Observation) {
defer m.wg.Done() defer m.wg.Done()
check.monitor(m.cfg.Shutdown, m.quit) check.monitor(m.cfg.Shutdown, m.quit)
}() }(check)
} }
return nil return nil
+1
View File
@@ -1742,6 +1742,7 @@ func (s *Switch) htlcForwarder() {
wg.Add(1) wg.Add(1)
go func(l ChannelLink) { go func(l ChannelLink) {
defer wg.Done() defer wg.Done()
l.Stop() l.Stop()
}(link) }(link)
} }
+4 -4
View File
@@ -78,13 +78,12 @@ func completePaymentRequests(client lnrpc.LightningClient,
// Launch all payments simultaneously. // Launch all payments simultaneously.
results := make(chan error) results := make(chan error)
for _, payReq := range paymentRequests { for _, payReq := range paymentRequests {
payReqCopy := payReq go func(payReq string) {
go func() { err := send(payReq)
err := send(payReqCopy)
if awaitResponse { if awaitResponse {
results <- err results <- err
} }
}() }(payReq)
} }
// If awaiting a response, verify that all payments succeeded. // If awaiting a response, verify that all payments succeeded.
@@ -406,6 +405,7 @@ func subscribeChannelNotifications(ctxb context.Context, t *harnessTest,
chanUpdates := make(chan *lnrpc.ChannelEventUpdate, 20) chanUpdates := make(chan *lnrpc.ChannelEventUpdate, 20)
go func() { go func() {
defer cancelFunc() defer cancelFunc()
for { for {
select { select {
case <-quit: case <-quit:
+9 -5
View File
@@ -1483,9 +1483,13 @@ out:
// Launch a goroutine to re-package and send // Launch a goroutine to re-package and send
// notifications for any newly confirmed transactions. // notifications for any newly confirmed transactions.
go func() { //nolint:lll
go func(txNtfn *wallet.TransactionNotifications) {
for _, block := range txNtfn.AttachedBlocks { for _, block := range txNtfn.AttachedBlocks {
details, err := minedTransactionsToDetails(currentHeight, block, t.w.ChainParams()) details, err := minedTransactionsToDetails(
currentHeight, block,
t.w.ChainParams(),
)
if err != nil { if err != nil {
continue continue
} }
@@ -1499,11 +1503,11 @@ out:
} }
} }
}() }(txNtfn)
// Launch a goroutine to re-package and send // Launch a goroutine to re-package and send
// notifications for any newly unconfirmed transactions. // notifications for any newly unconfirmed transactions.
go func() { go func(txNtfn *wallet.TransactionNotifications) {
for _, tx := range txNtfn.UnminedTransactions { for _, tx := range txNtfn.UnminedTransactions {
detail, err := unminedTransactionsToDetail( detail, err := unminedTransactionsToDetail(
tx, t.w.ChainParams(), tx, t.w.ChainParams(),
@@ -1518,7 +1522,7 @@ out:
return return
} }
} }
}() }(txNtfn)
case <-t.quit: case <-t.quit:
break out break out
} }
+2 -1
View File
@@ -180,8 +180,9 @@ func (h *MiddlewareHandler) Run() error {
// request to the client). // request to the client).
h.wg.Add(1) h.wg.Add(1)
go func() { go func() {
defer h.wg.Done()
h.receiveResponses(errChan, responses) h.receiveResponses(errChan, responses)
h.wg.Done()
}() }()
return h.sendInterceptRequests(errChan, responses) return h.sendInterceptRequests(errChan, responses)
+2 -2
View File
@@ -5214,7 +5214,7 @@ sendLoop:
// payment so we can continue to serve requests while // payment so we can continue to serve requests while
// this payment is being dispatched. // this payment is being dispatched.
wg.Add(1) wg.Add(1)
go func() { go func(payIntent *rpcPaymentIntent) {
defer wg.Done() defer wg.Done()
// Attempt to grab a free semaphore slot, using // Attempt to grab a free semaphore slot, using
@@ -5292,7 +5292,7 @@ sendLoop:
} }
return return
} }
}() }(payIntent)
} }
} }
+1
View File
@@ -1415,6 +1415,7 @@ func (s *UtxoSweeper) waitForSpend(outpoint wire.OutPoint,
s.wg.Add(1) s.wg.Add(1)
go func() { go func() {
defer s.wg.Done() defer s.wg.Done()
select { select {
case spend, ok := <-spendEvent.Spend: case spend, ok := <-spendEvent.Spend:
if !ok { if !ok {
+1
View File
@@ -39,6 +39,7 @@ func NewForce(interval time.Duration) *Force {
m.wg.Add(1) m.wg.Add(1)
go func() { go func() {
defer m.wg.Done() defer m.wg.Done()
for { for {
select { select {
case t := <-m.ticker: case t := <-m.ticker:
+1
View File
@@ -689,6 +689,7 @@ func (s *sessionQueueSet) ApplyAndWait(getApply func(*sessionQueue) func()) {
wg.Add(1) wg.Add(1)
go func(sq *sessionQueue) { go func(sq *sessionQueue) {
defer wg.Done() defer wg.Done()
getApply(sq)() getApply(sq)()
}(sessionq) }(sessionq)
} }