-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make stats timers thread safe to use #223
Make stats timers thread safe to use #223
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_`.
It seems there are some tests failing. I'll fix them ASAP. |
d5b000a
to
adfedf6
Compare
@@ -57,23 +53,20 @@ void ConsumerStatsImpl::flushAndReset(const boost::system::error_code& ec) { | |||
} | |||
|
|||
Lock lock(mutex_); | |||
ConsumerStatsImpl tmp = *this; | |||
std::ostringstream oss; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Motivation
The timers' callbacks in
ProducerStatsImpl
andConsumerStatsImpl
are not thread safe because they both capture thethis
pointer, while when the callback is called in the event loop,this
might point to an expired instance.Modifications
this
in these callbacks. Since we cannot callshared_from_this()
in the constructor, astart()
method is added.executor_
field and unnecessary null check fortimer_
.