diff --git a/src/Common/Scheduler/Nodes/IOResourceManager.cpp b/src/Common/Scheduler/Nodes/IOResourceManager.cpp index 645ce8fb3f0..e2042a29a80 100644 --- a/src/Common/Scheduler/Nodes/IOResourceManager.cpp +++ b/src/Common/Scheduler/Nodes/IOResourceManager.cpp @@ -490,16 +490,15 @@ void IOResourceManager::Resource::forEachResourceNode(IResourceManager::VisitorF void IOResourceManager::forEachNode(IResourceManager::VisitorFunc visitor) { - // Gather resource upfront to avoid holding mutex for a long time - std::map sorted_resources; + // Copy resource to avoid holding mutex for a long time + std::unordered_map resources_copy; { std::unique_lock lock{mutex}; - for (auto & [resource_name, resource] : resources) - sorted_resources[resource_name] = resource; + resources_copy = resources; } /// Run tasks one by one to avoid concurrent calls to visitor - for (auto & [resource_name, resource] : sorted_resources) + for (auto & [resource_name, resource] : resources_copy) resource->forEachResourceNode(visitor); }