Skip to content

Make stats timers thread safe to use#223

Merged
BewareMyPower merged 2 commits into
apache:mainfrom
BewareMyPower:bewaremypower/producer-stats-crash
Mar 20, 2023
Merged

Make stats timers thread safe to use#223
BewareMyPower merged 2 commits into
apache:mainfrom
BewareMyPower:bewaremypower/producer-stats-crash

Conversation

@BewareMyPower
Copy link
Copy Markdown
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
Copy Markdown
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

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

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
Copy Markdown
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