Skip to content
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

Merged

Conversation

BewareMyPower
Copy link
Contributor

@BewareMyPower BewareMyPower commented Mar 20, 2023

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_.

### 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_`.
@BewareMyPower BewareMyPower added the bug Something isn't working label Mar 20, 2023
@BewareMyPower BewareMyPower added this to the 3.2.0 milestone Mar 20, 2023
@BewareMyPower BewareMyPower self-assigned this Mar 20, 2023
@BewareMyPower
Copy link
Contributor Author

It seems there are some tests failing. I'll fix them ASAP.

@BewareMyPower BewareMyPower marked this pull request as draft March 20, 2023 06:50
@BewareMyPower BewareMyPower marked this pull request as ready for review March 20, 2023 07:38
@BewareMyPower BewareMyPower force-pushed the bewaremypower/producer-stats-crash branch from d5b000a to adfedf6 Compare March 20, 2023 07:59
@BewareMyPower BewareMyPower merged commit e79357e into apache:main Mar 20, 2023
@BewareMyPower BewareMyPower deleted the bewaremypower/producer-stats-crash branch March 20, 2023 13:28
@@ -57,23 +53,20 @@ void ConsumerStatsImpl::flushAndReset(const boost::system::error_code& ec) {
}

Lock lock(mutex_);
ConsumerStatsImpl tmp = *this;
std::ostringstream oss;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants