Reduce critical section in rate limiter code #365
Merged
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.
While investigating a deadlock which started happening recently [0] I noticed that
match
ing on a temporary value extends that temporaries lifetime to the duration of thematch
statement. It can be extended even further when you return thematch
expression as a temporary value itself.This is usually not a problem but can lead to surprising deadlocks when the temporary value contains a
MutexGuard
or something similar [1] [2]. Then the resource gets locked longer than expected.I originally wrote the rate limiter code with the intention of NOT locking
self.strategy
for the entire duration of the log statement so it makes sense to fix that now.However, I'm not convinced that this bug caused the investigated deadlock. The code always acquired the locks in following order 1.
self.strategy
2.stdout
and should have released it in the reverse order. For this lifetime extension to cause the deadlock there would also have to be a critical section which first locksstdout
thenself.strategy
which I don't think it does anywhere.Test Plan
existing tests