Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Start child processes one at a time; run gcov without a directory

Reading the coverage profiles on several threads broke an assumption of
ChildProcess::Start on POSIX: it changes the working directory of the
whole process around posix_spawn. Two gcov runs started at once could
both run in one test's directory, so on the GCC job the map of
"Subtracts one" showed what "Adds one" reached.

- ChildProcess::Start holds a lock while a child starts, on POSIX and
  Windows, so the directory change and the handles a child inherits never
  overlap with another start.
- gcov gets the absolute path of the data file and no working directory,
  so no directory changes while profiles are read in parallel.
Commit
b26486d68ece48a1dfdb208a9f3f1bd8acab0382
Author
Leitwolf11 ·
Parents
454cca9b6399

Files · Download .tar.gz

Changes

 extras/catch_amalgamated.cpp             | 40 +++++++++++++++++++++++++-------
 src/Progmasoft/Runner/CoverageReport.cpp | 18 +++++++-------
 src/Progmasoft/Runner/Process.cpp        | 22 +++++++++++++++++-
 3 files changed, 62 insertions(+), 18 deletions(-)

diff --git a/extras/catch_amalgamated.cpp b/extras/catch_amalgamated.cpp
index cc4612f2..7a1c9541 100644
--- a/extras/catch_amalgamated.cpp
+++ b/extras/catch_amalgamated.cpp
@@ -11186,14 +11186,16 @@ namespace Progmasoft::Catch3::Runner
                     return Error{ "the notes file " + gcno.string()
                                   + " could not be read: " + error.message() };
                 }
-                auto json = RunTool(
-                    gcov,
-                    // Without --branch-probabilities the JSON has no branches.
-                    { "--json-format",
-                      "--branch-probabilities",
-                      "--stdout",
-                      gcda.filename().string() },
-                    gcda.parent_path());
+                // The data file by its absolute path, so that gcov needs no
+                // working directory: profiles are read on several threads,
+                // and a working directory is the whole process's.
+                // Without --branch-probabilities the JSON has no branches.
+                auto json
+                    = RunTool(gcov,
+                              { "--json-format",
+                                "--branch-probabilities",
+                                "--stdout",
+                                std::filesystem::absolute(gcda).string() });
                 if (!json)
                 {
                     return json.GetError();
@@ -15676,6 +15678,7 @@ namespace Progmasoft::Catch3::Runner
 #include <cstdlib>
 #include <cstring>
 #include <iterator>
+#include <mutex>
 #include <string_view>
 #include <thread>
 
@@ -16280,6 +16283,20 @@ namespace Progmasoft::Catch3::Runner
 #endif
     }
 
+    namespace
+    {
+        /// Held while a child starts. Starting changes what the whole process
+        /// shares: the working directory on POSIX, and the handles a child
+        /// inherits; two threads starting children at once would see each
+        /// other's directory or hand their pipes to each other's child.
+        std::mutex &
+        StartLock()
+        {
+            static std::mutex lock;
+            return lock;
+        }
+    } // namespace
+
 #if defined(_WIN32)
     Expected<std::unique_ptr<ChildProcess>>
     ChildProcess::Start(const ChildOptions &options)
@@ -16288,6 +16305,7 @@ namespace Progmasoft::Catch3::Runner
         {
             return Error{ "no program to start" };
         }
+        const std::lock_guard<std::mutex> starting{ StartLock() };
         SECURITY_ATTRIBUTES inheritable{};
         inheritable.nLength = sizeof(inheritable);
         inheritable.bInheritHandle = TRUE;
@@ -16650,6 +16668,7 @@ namespace Progmasoft::Catch3::Runner
         {
             return Error{ "no program to start" };
         }
+        const std::lock_guard<std::mutex> starting{ StartLock() };
         int input[2] = { -1, -1 };
         int output[2] = { -1, -1 };
         int errors[2] = { -1, -1 };
@@ -16741,7 +16760,10 @@ namespace Progmasoft::Catch3::Runner
         arguments.push_back(nullptr);
 
         // posix_spawn has no working-directory action everywhere, so the
-        // directory changes around the call; only this thread spawns.
+        // directory changes around the call, under StartLock. A thread that
+        // reads a relative path meanwhile would read it from there, so
+        // callers that start children from several threads use absolute
+        // paths or no working directory.
         std::filesystem::path previousDirectory;
         if (!options.workingDirectory.empty())
         {
diff --git a/src/Progmasoft/Runner/CoverageReport.cpp b/src/Progmasoft/Runner/CoverageReport.cpp
index fb2760c3..ccdd0538 100644
--- a/src/Progmasoft/Runner/CoverageReport.cpp
+++ b/src/Progmasoft/Runner/CoverageReport.cpp
@@ -239,14 +239,16 @@ namespace Progmasoft::Catch3::Runner
                     return Error{ "the notes file " + gcno.string()
                                   + " could not be read: " + error.message() };
                 }
-                auto json = RunTool(
-                    gcov,
-                    // Without --branch-probabilities the JSON has no branches.
-                    { "--json-format",
-                      "--branch-probabilities",
-                      "--stdout",
-                      gcda.filename().string() },
-                    gcda.parent_path());
+                // The data file by its absolute path, so that gcov needs no
+                // working directory: profiles are read on several threads,
+                // and a working directory is the whole process's.
+                // Without --branch-probabilities the JSON has no branches.
+                auto json
+                    = RunTool(gcov,
+                              { "--json-format",
+                                "--branch-probabilities",
+                                "--stdout",
+                                std::filesystem::absolute(gcda).string() });
                 if (!json)
                 {
                     return json.GetError();
diff --git a/src/Progmasoft/Runner/Process.cpp b/src/Progmasoft/Runner/Process.cpp
index 85987210..41fa87f1 100644
--- a/src/Progmasoft/Runner/Process.cpp
+++ b/src/Progmasoft/Runner/Process.cpp
@@ -6,6 +6,7 @@
 #include <cstdlib>
 #include <cstring>
 #include <iterator>
+#include <mutex>
 #include <string_view>
 #include <thread>
 
@@ -611,6 +612,20 @@ namespace Progmasoft::Catch3::Runner
 #endif
     }
 
+    namespace
+    {
+        /// Held while a child starts. Starting changes what the whole process
+        /// shares: the working directory on POSIX, and the handles a child
+        /// inherits; two threads starting children at once would see each
+        /// other's directory or hand their pipes to each other's child.
+        std::mutex &
+        StartLock()
+        {
+            static std::mutex lock;
+            return lock;
+        }
+    } // namespace
+
 #if defined(_WIN32)
     Expected<std::unique_ptr<ChildProcess>>
     ChildProcess::Start(const ChildOptions &options)
@@ -619,6 +634,7 @@ namespace Progmasoft::Catch3::Runner
         {
             return Error{ "no program to start" };
         }
+        const std::lock_guard<std::mutex> starting{ StartLock() };
         SECURITY_ATTRIBUTES inheritable{};
         inheritable.nLength = sizeof(inheritable);
         inheritable.bInheritHandle = TRUE;
@@ -981,6 +997,7 @@ namespace Progmasoft::Catch3::Runner
         {
             return Error{ "no program to start" };
         }
+        const std::lock_guard<std::mutex> starting{ StartLock() };
         int input[2] = { -1, -1 };
         int output[2] = { -1, -1 };
         int errors[2] = { -1, -1 };
@@ -1072,7 +1089,10 @@ namespace Progmasoft::Catch3::Runner
         arguments.push_back(nullptr);
 
         // posix_spawn has no working-directory action everywhere, so the
-        // directory changes around the call; only this thread spawns.
+        // directory changes around the call, under StartLock. A thread that
+        // reads a relative path meanwhile would read it from there, so
+        // callers that start children from several threads use absolute
+        // paths or no working directory.
         std::filesystem::path previousDirectory;
         if (!options.workingDirectory.empty())
         {