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

StatsManager is not thread safe #3869

Closed
cangfengzhs opened this issue Feb 9, 2022 · 7 comments · Fixed by #3894
Closed

StatsManager is not thread safe #3869

cangfengzhs opened this issue Feb 9, 2022 · 7 comments · Fixed by #3894
Assignees
Labels
type/bug Type: something is unexpected
Milestone

Comments

@cangfengzhs
Copy link
Contributor

Describe the bug (required)

sm.stats_.emplace(
      counterName,
      std::make_pair(std::make_unique<std::mutex>(),
                     std::make_unique<StatsType>(
                         60,
                         std::initializer_list<StatsType::Duration>(
                             {seconds(5), seconds(60), seconds(600), seconds(3600)}))));

stats_ is a std::unordered_map. And some time will read/write without lock

@cangfengzhs cangfengzhs added the type/bug Type: something is unexpected label Feb 9, 2022
@Shylock-Hg
Copy link
Contributor

It's protected by nameMapLock_.

@cangfengzhs
Copy link
Contributor Author

cangfengzhs commented Feb 9, 2022

No, nameMapLock_ protect another map. You can find some other places where stat_ is used without lock.

@Shylock-Hg
Copy link
Contributor

Shylock-Hg commented Feb 9, 2022

No, nameMapLock_ protect another map. You can find some other places where stat_ is used without lock.

It protects stats_ too, add/delete counter will lock nameMapLock_ firstly.

@cangfengzhs
Copy link
Contributor Author

But reading stat_ doesn't with Lock. If one thread read stat_'s element while another thread insert/erase some element, this may be lead to structure modify of unordered_map. Reading thread will access some released memory.

@Shylock-Hg
Copy link
Contributor

But reading stat_ doesn't with Lock. If one thread read stat_'s element while another thread insert/erase some element, this may be lead to structure modify of unordered_map. Reading thread will access some released memory.

Yes, read need protection too.

@cangfengzhs
Copy link
Contributor Author

cangfengzhs commented Feb 9, 2022

Yes, read need protection too.

And I think this part requires some complex design rather than simply using read-write locks. The performance of the lock is very poor, and the metric cannot affect the performance of the system.

@Shylock-Hg
Copy link
Contributor

Shylock-Hg commented Feb 9, 2022

Yes, read need protection too.

And I think this part requires some complex design rather than simply using read-write locks. The performance of the lock is very poor, and the metric cannot affect the performance of the system.

Agree, in my mind, static lifetime metric is enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Type: something is unexpected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants