Acquire `per_peer_state` `write` lock earlier in `ChannelManager::write`
authorElias Rohrer <dev@tnull.de>
Wed, 17 Apr 2024 09:54:25 +0000 (11:54 +0200)
committerElias Rohrer <dev@tnull.de>
Wed, 17 Apr 2024 10:02:00 +0000 (12:02 +0200)
commitce36f2c20be59452aa3414692cb2b4a175047b10
tree4a33f651b8c32637d03673341cd72844c04eac02
parentac9a2c8a20dadf5e68cbc3141072e73e20ca5004
Acquire `per_peer_state` `write` lock earlier in `ChannelManager::write`

Previously, we would hold the `per_peer_state` `read` lock during the
serialization of channel objects. While this might have reduced lock
congestion minimally, we also calculated the number of serializable
peers in this phase, only to use it later after dropping and reacquiring
the `per_peer_state` `write` lock.

This could potentially result in inconsistiencies if the
`serializable_peer_count` would change after we dropped the `read` lock
but before we acquired the `write` lock.

To mitigate the issue we just acquire the `write` lock ealier and hold
it for the remainder of `ChannelManager::write`.
lightning/src/ln/channelmanager.rs