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

Data race is found around PikaServer::statistic_::server_stat::exec_count_db #2670

Closed
cheniujh opened this issue May 22, 2024 · 0 comments
Closed
Labels
☢️ Bug Something isn't working

Comments

@cheniujh
Copy link
Collaborator

cheniujh commented May 22, 2024

Is this a regression?

No

Description

1 从代码逻辑来看
g_pika_server->statistic_.server_stat.exec_count_db 的类型是:std::unordered_map<std::string, std::atomic<uint64_t>> exec_count_db, value是atomic,用于累加命令执行次数。
这个map主要被使用到的地方是:PikaServer::UpdateQueryNumAndExecCountDB,而这个函数高度并发,大致逻辑如下:
image
问题其实比较明显:exec_count_db这个map本身无锁,在pika启动以后,最开始的那批负载打进来(此时map为空),会触发对这个map的并发insert,这无疑是要data race的

2 打开thread sanitizer再跑,验证了1中的逻辑推断,这个map有datarace,而且用address sanitizer再跑的话,报的问题是heap use the after free, 典型的并发保护没做好:
image



  1. From the code logic perspective, the type of g_pika_server->statistic_.server_stat.exec_count_db is: std::unordered_map<std::string, std::atomic<uint64_t>> exec_count_db. The value is atomic, used to accumulate the number of command executions. This map is mainly used in the function PikaServer::UpdateQueryNumAndExecCountDB, which is highly concurrent. The general logic is as follows:
    image
    The issue is quite obvious: the exec_count_db map itself is lock-free. After Pika starts, the initial batch of loads will trigger concurrent inserts into this map, which undoubtedly causes a data race.

  2. Running with Thread Sanitizer confirms the logic inference in point 1, showing that this map has a data race. Additionally, running with Address Sanitizer reports an issue of heap use after free, which is typical of improper concurrent protection:
    image

Please provide a link to a minimal reproduction of the bug

No response

Screenshots or videos

No response

Please provide the version you discovered this bug in (check about page for version information)

No response

Anything else?

No response

@cheniujh cheniujh added the ☢️ Bug Something isn't working label May 22, 2024
@cheniujh cheniujh changed the title Data race is found around g_pika_server->statistic_.server_stat.exec_count_db Data race is found around PikaServer::statistic_::server_stat::exec_count_db May 22, 2024
@cheniujh cheniujh closed this as completed Jun 3, 2024
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

No branches or pull requests

1 participant