Skip to content

Conversation

@ckerr
Copy link
Member

@ckerr ckerr commented Dec 19, 2025

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.

@ckerr ckerr added scope:core type:fix A bug fix type:perf A code change that improves performance labels Dec 19, 2025
@ckerr ckerr requested review from Coeur and tearfur December 19, 2025 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope:core type:fix A bug fix type:perf A code change that improves performance

Development

Successfully merging this pull request may close these issues.

1 participant