Skip to content

Commit

Permalink
Merge pull request #2128 from kcalvinalvin/2024-02-27-no-panic-for-su…
Browse files Browse the repository at this point in the history
…bscription-callback-errors

blockchain: always relock chainLock for subscription callbacks
  • Loading branch information
Roasbeef committed Mar 9, 2024
2 parents 8ed234b + 3d1150a commit f0ec9fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
8 changes: 5 additions & 3 deletions blockchain/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block, flags BehaviorFlags)
// Notify the caller that the new block was accepted into the block
// chain. The caller would typically want to react by relaying the
// inventory to other peers.
b.chainLock.Unlock()
b.sendNotification(NTBlockAccepted, block)
b.chainLock.Lock()
func() {
b.chainLock.Unlock()
defer b.chainLock.Lock()
b.sendNotification(NTBlockAccepted, block)
}()

return isMainChain, nil
}
16 changes: 10 additions & 6 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,11 @@ func (b *BlockChain) connectBlock(node *blockNode, block *btcutil.Block,
// Notify the caller that the block was connected to the main chain.
// The caller would typically want to react with actions such as
// updating wallets.
b.chainLock.Unlock()
b.sendNotification(NTBlockConnected, block)
b.chainLock.Lock()
func() {
b.chainLock.Unlock()
defer b.chainLock.Lock()
b.sendNotification(NTBlockConnected, block)
}()

// Since we may have changed the UTXO cache, we make sure it didn't exceed its
// maximum size. If we're pruned and have flushed already, this will be a no-op.
Expand Down Expand Up @@ -834,9 +836,11 @@ func (b *BlockChain) disconnectBlock(node *blockNode, block *btcutil.Block, view
// Notify the caller that the block was disconnected from the main
// chain. The caller would typically want to react with actions such as
// updating wallets.
b.chainLock.Unlock()
b.sendNotification(NTBlockDisconnected, block)
b.chainLock.Lock()
func() {
b.chainLock.Unlock()
defer b.chainLock.Lock()
b.sendNotification(NTBlockDisconnected, block)
}()

return nil
}
Expand Down

0 comments on commit f0ec9fb

Please sign in to comment.