Skip to content

Commit 33d3832

Browse files
committed
swap: Don't defer mutex unlock in a loop.
Using defer to unlock the mutex means that the mutex can be locked repeatedly during the loop and only unlocked when the function returns, rather than being unlocked at the end of the current loop iteration. This can lead to the mutex being held for longer than required.
1 parent 94ca39a commit 33d3832

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

server/swap/swap.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,11 +1103,11 @@ func (s *Swapper) processBlock(ctx context.Context, block *blockNotification) {
11031103
// Lock the matchTracker so the following checks and updates are atomic
11041104
// with respect to Status.
11051105
match.mtx.RLock()
1106-
defer match.mtx.RUnlock()
11071106

11081107
switch match.Status {
11091108
case order.MakerSwapCast:
11101109
if match.makerStatus.swapAsset != block.assetID {
1110+
match.mtx.RUnlock()
11111111
break
11121112
}
11131113
// If the maker has broadcast their transaction, the taker's
@@ -1118,6 +1118,7 @@ func (s *Swapper) processBlock(ctx context.Context, block *blockNotification) {
11181118
}
11191119
case order.TakerSwapCast:
11201120
if match.takerStatus.swapAsset != block.assetID {
1121+
match.mtx.RUnlock()
11211122
break
11221123
}
11231124
// If the taker has broadcast their transaction, the maker's
@@ -1126,6 +1127,8 @@ func (s *Swapper) processBlock(ctx context.Context, block *blockNotification) {
11261127
if s.tryConfirmSwap(ctx, match.takerStatus, block.time) {
11271128
s.unlockOrderCoins(match.Taker)
11281129
}
1130+
1131+
match.mtx.RUnlock()
11291132
}
11301133
}
11311134
}

0 commit comments

Comments
 (0)