{"id":1729,"date":"2015-01-03T15:37:42","date_gmt":"2015-01-03T23:37:42","guid":{"rendered":"http:\/\/xiehang.com\/blog\/?p=1729"},"modified":"2015-01-03T15:38:49","modified_gmt":"2015-01-03T23:38:49","slug":"these-guys-dont-know-how-to-avoid-race-condition","status":"publish","type":"post","link":"https:\/\/xiehang.com\/blog\/2015\/01\/03\/these-guys-dont-know-how-to-avoid-race-condition\/","title":{"rendered":"These guys don’t know how to avoid race condition"},"content":{"rendered":"

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.<\/p>\n

I think the diff would be a better explanation.<\/p>\n

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

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, […]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[504,501],"_links":{"self":[{"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/posts\/1729"}],"collection":[{"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/comments?post=1729"}],"version-history":[{"count":3,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/posts\/1729\/revisions"}],"predecessor-version":[{"id":1732,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/posts\/1729\/revisions\/1732"}],"wp:attachment":[{"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/media?parent=1729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/categories?post=1729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/tags?post=1729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}