Make stats timers thread safe to use#223
Merged
BewareMyPower merged 2 commits intoMar 20, 2023
Merged
Conversation
### Motivation The timers' callbacks in `ProducerStatsImpl` and `ConsumerStatsImpl` are not thread safe because they both capture the `this` pointer, while when the callback is called in the event loop, `this` might point to an expired instance. ### Modifications - Capture the weak pointer instead of `this` in these callbacks. Since we cannot call `shared_from_this()` in the constructor, a `start()` method is added. - Remove the useless `executor_` field and unnecessary null check for `timer_`.
merlimat
approved these changes
Mar 20, 2023
Demogorgon314
approved these changes
Mar 20, 2023
Contributor
Author
|
It seems there are some tests failing. I'll fix them ASAP. |
d5b000a to
adfedf6
Compare
addisonj
reviewed
Mar 20, 2023
|
|
||
| Lock lock(mutex_); | ||
| ConsumerStatsImpl tmp = *this; | ||
| std::ostringstream oss; |
Contributor
Author
There was a problem hiding this comment.
Why? We need to log the current instance. Before this PR, it was logged by copying the current instance, which is heavy and might cause unexpected destruction of the shared pointer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
The timers' callbacks in
ProducerStatsImplandConsumerStatsImplare not thread safe because they both capture thethispointer, while when the callback is called in the event loop,thismight point to an expired instance.Modifications
thisin these callbacks. Since we cannot callshared_from_this()in the constructor, astart()method is added.executor_field and unnecessary null check fortimer_.