multi: don't access loop variables in goroutines

This commit makes sure that no loop variables or other temporary
variables are accessed directly in a goroutine but are instead passed
into the goroutine through a parameter. This makes sure a copy of the
value is put on the stack and is not changed while the outside loop
continues.
This commit is contained in:
Oliver Gugger committed 2022-11-21 13:54:23 +01:00
1 parent e7170ef819
commit ad8e25cbc9
7 files changed
+33 -21

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")
+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
+3 -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.
+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 -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)
} }
} }