branch-2.1: [fix](runtime_profile) fix race condition in to_thrift #45047 (#45099)

Cherry-picked from #45047

Co-authored-by: Kaijie Chen <chenkaijie@selectdb.com>
This commit is contained in:
github-actions[bot]
2024-12-06 16:25:33 +08:00
committed by GitHub
parent 53291bd040
commit 08c8a416ea

View File

@ -576,8 +576,6 @@ void RuntimeProfile::to_thrift(TRuntimeProfileTree* tree) {
}
void RuntimeProfile::to_thrift(std::vector<TRuntimeProfileNode>* nodes) {
nodes->reserve(nodes->size() + _children.size());
int index = nodes->size();
nodes->push_back(TRuntimeProfileNode());
TRuntimeProfileNode& node = (*nodes)[index];
@ -607,10 +605,13 @@ void RuntimeProfile::to_thrift(std::vector<TRuntimeProfileNode>* nodes) {
ChildVector children;
{
// _children may be modified during to_thrift(),
// so we have to lock and copy _children to avoid race condition
std::lock_guard<std::mutex> l(_children_lock);
children = _children;
}
node.num_children = children.size();
nodes->reserve(nodes->size() + children.size());
for (int i = 0; i < children.size(); ++i) {
int child_idx = nodes->size();