lnwallet: fix race condition in channel state machine, use single mutex

This commit fixes a race condition that was discovered as a result of
the new htlcswitch package. The StateSnapshot method and all of the
other methods which mutate the state of the channel state machine were
using distinct mutexes. The fix is trivial: all methods accessing the
internal channel state variable now use the same mutex.
This commit is contained in:
Olaoluwa Osuntokun 2017-06-08 22:24:10 -07:00
parent 38beeebe3d
commit 9676d476c9
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 5 additions and 6 deletions

@ -622,8 +622,6 @@ type LightningChannel struct {
// able to broadcast safely.
localCommitChain *commitmentChain
// stateMtx protects concurrent access to the state struct.
stateMtx sync.RWMutex
channelState *channeldb.OpenChannel
// [local|remote]Log is a (mostly) append-only log storing all the HTLC
@ -2738,8 +2736,8 @@ func (lc *LightningChannel) DeleteState(c *channeldb.ChannelCloseSummary) error
// StateSnapshot returns a snapshot of the current fully committed state within
// the channel.
func (lc *LightningChannel) StateSnapshot() *channeldb.ChannelSnapshot {
lc.stateMtx.RLock()
defer lc.stateMtx.RUnlock()
lc.RLock()
defer lc.RUnlock()
return lc.channelState.Snapshot()
}

@ -1453,7 +1453,7 @@ func coinSelect(feeRate uint64, amt btcutil.Amount,
estimatedSize = ((len(selectedUtxos) * p2wkhSpendSize) +
p2wshOutputSize + txOverhead)
// The difference bteween the selected amount and the amount
// The difference between the selected amount and the amount
// requested will be used to pay fees, and generate a change
// output with the remaining.
overShootAmt := totalSat - amtNeeded
@ -1468,7 +1468,8 @@ func coinSelect(feeRate uint64, amt btcutil.Amount,
continue
}
// If the fee is sufficient, then calculate the size of the change output.
// If the fee is sufficient, then calculate the size of the
// change output.
changeAmt := overShootAmt - requiredFee
return selectedUtxos, changeAmt, nil