fix: cache the value of tr_peerMgrGetDesiredAvailable()
#7961
+72
−48
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Mitigates the risks of the long-standing
tr_torrentStat()threading issue (#3811, #4571, #4936, #5853, #7948, to name a few).This PR doesn't compete with #7948. Both can be evaluated independently.
From the crash logs, it looks like
tr_peerMgrGetDesiredAvailable()is the most common culprit. This makes sense: as currently implemented, that function iterates through all the peer connections. That iteration is inherently risky without a lock, particularly because the calculation is slow.This PR has
tr_peerMgrGetDesiredAvailable()cache its value instead of recalculating it every time. The cache is invalidated when either (a) the user changes which files they want or (b) the swarm changes, e.g. by a peer closing connection or a peer sending us have, have all, have none, or bitfield messages.By caching this, we reduce both the cost and risk of calculating it.