From 309753ebb5f4d1cae64554fac8d4e49c2c28884c Mon Sep 17 00:00:00 2001 From: Patrick Jackson Date: Thu, 30 Mar 2023 16:58:07 -0700 Subject: [PATCH 1/2] Fix data race in copyPaths --- src/libstore/store-api.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index b0ca1321c..6233768df 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -10,6 +10,7 @@ #include "archive.hh" #include "callback.hh" #include "remote-store.hh" +#include "sync.hh" #include #include @@ -1101,7 +1102,8 @@ std::map copyPaths( return storePathForDst; }; - uint64_t total = 0; + // total is accessed by each copy, which are each handled in separate threads + Sync total = 0; for (auto & missingPath : sortedMissing) { auto info = srcStore.queryPathInfo(missingPath); @@ -1124,8 +1126,8 @@ std::map copyPaths( PushActivity pact(act.id); LambdaSink progressSink([&](std::string_view data) { - total += data.size(); - act.progress(total, info->narSize); + *total.lock() += data.size(); + act.progress(*total.lock(), info->narSize); }); TeeSink tee { sink, progressSink }; From 804180ad52cc855a77bc7901c033e1f441b65cdc Mon Sep 17 00:00:00 2001 From: Patrick Jackson Date: Thu, 30 Mar 2023 18:05:53 -0700 Subject: [PATCH 2/2] Only lock once --- src/libstore/store-api.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 6233768df..3cf4c801b 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -1103,7 +1103,7 @@ std::map copyPaths( }; // total is accessed by each copy, which are each handled in separate threads - Sync total = 0; + Sync _total = 0; for (auto & missingPath : sortedMissing) { auto info = srcStore.queryPathInfo(missingPath); @@ -1126,8 +1126,9 @@ std::map copyPaths( PushActivity pact(act.id); LambdaSink progressSink([&](std::string_view data) { - *total.lock() += data.size(); - act.progress(*total.lock(), info->narSize); + auto total(_total.lock()); + *total += data.size(); + act.progress(*total, info->narSize); }); TeeSink tee { sink, progressSink };