Jan 032015
 

Actually it is not that hard to avoid race condition, as long as you have that concept in mind. I have to say, some people don’t know how to avoid race condition.

I think the diff would be a better explanation.

+import tempfile
 
 # Import salt libs
 import salt.crypt
 @@ -668,12 +669,15 @@ def _pillar(self, load):
             if not os.path.isdir(cdir):
                 os.makedirs(cdir)
             datap = os.path.join(cdir, 'data.p')
-            with salt.utils.fopen(datap, 'w+b') as fp_:
+            tmpfh, tmpfname = tempfile.mkstemp(dir=cdir)
+            os.close(tmpfh)
+            with salt.utils.fopen(tmpfname, 'w+b') as fp_:
                 fp_.write(
                         self.serial.dumps(
                             {'grains': load['grains'],
                              'pillar': data})
                             )
+            os.rename(tmpfname, datap)
         return data