From 4d545e001187eead6447ece5e7f4b33016414f07 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Mon, 17 May 2010 16:24:32 +0200
Subject: [PATCH] BUG: race condition when removing ABORT file

---
 .../abortCalculation/abortCalculation.C       | 53 ++++++++++++-------
 1 file changed, 34 insertions(+), 19 deletions(-)

diff --git a/src/postProcessing/functionObjects/jobControl/abortCalculation/abortCalculation.C b/src/postProcessing/functionObjects/jobControl/abortCalculation/abortCalculation.C
index 9cd6a2af07a..08bf5ce7733 100644
--- a/src/postProcessing/functionObjects/jobControl/abortCalculation/abortCalculation.C
+++ b/src/postProcessing/functionObjects/jobControl/abortCalculation/abortCalculation.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2009-2009 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2009-2010 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -28,6 +28,7 @@ License
 #include "error.H"
 #include "Time.H"
 #include "OSspecific.H"
+#include "PstreamReduceOps.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -52,8 +53,12 @@ const Foam::NamedEnum<Foam::abortCalculation::actionType, 3>
 
 void Foam::abortCalculation::removeFile() const
 {
-    if (isFile(abortFile_))
+    bool hasAbort = isFile(abortFile_);
+    reduce(hasAbort, orOp<bool>());
+
+    if (hasAbort && Pstream::master())
     {
+        // cleanup ABORT file (on master only)
         rm(abortFile_);
     }
 }
@@ -92,14 +97,9 @@ Foam::abortCalculation::~abortCalculation()
 
 void Foam::abortCalculation::read(const dictionary& dict)
 {
-    word actionName;
-
     if (dict.found("action"))
     {
-        action_ = actionTypeNames_.read
-        (
-            dict.lookup("action")
-        );
+        action_ = actionTypeNames_.read(dict.lookup("action"));
     }
     else
     {
@@ -115,26 +115,41 @@ void Foam::abortCalculation::read(const dictionary& dict)
 
 void Foam::abortCalculation::execute()
 {
-    if (isFile(abortFile_))
+    bool hasAbort = isFile(abortFile_);
+    reduce(hasAbort, orOp<bool>());
+
+    if (hasAbort)
     {
         switch (action_)
         {
             case noWriteNow :
-                obr_.time().stopAt(Time::saNoWriteNow);
-                Info<< "user requested abort - "
-                       "stop immediately without writing data" << endl;
+                if (obr_.time().stopAt(Time::saNoWriteNow))
+                {
+                    Info<< "USER REQUESTED ABORT (timeIndex="
+                        << obr_.time().timeIndex()
+                        << "): stop without writing data"
+                        << endl;
+                }
                 break;
 
             case writeNow :
-                obr_.time().stopAt(Time::saWriteNow);
-                Info<< "user requested abort - "
-                       "stop immediately with writing data" << endl;
+                if (obr_.time().stopAt(Time::saWriteNow))
+                {
+                    Info<< "USER REQUESTED ABORT (timeIndex="
+                        << obr_.time().timeIndex()
+                        << "): stop+write data"
+                        << endl;
+                }
                 break;
 
             case nextWrite :
-                obr_.time().stopAt(Time::saNextWrite);
-                Info<< "user requested abort - "
-                       "stop after next data write" << endl;
+                if (obr_.time().stopAt(Time::saNextWrite))
+                {
+                    Info<< "USER REQUESTED ABORT (timeIndex="
+                        << obr_.time().timeIndex()
+                        << "): stop after next data write"
+                        << endl;
+                }
                 break;
         }
     }
@@ -149,7 +164,7 @@ void Foam::abortCalculation::end()
 
 void Foam::abortCalculation::write()
 {
-    execute();
+    // Do nothing - only valid on execute
 }
 
 
-- 
GitLab