From 1ab1ba28bf9c9590f12ee1fc11a25d6a1c562fc4 Mon Sep 17 00:00:00 2001 From: Mikael Johansson Date: Mon, 7 Dec 2015 13:47:55 +0100 Subject: [PATCH] Fixed directory creation race condition --- src/lighter/main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lighter/main.py b/src/lighter/main.py index c6b3106..d57c3fd 100755 --- a/src/lighter/main.py +++ b/src/lighter/main.py @@ -120,8 +120,13 @@ def parse_service(filename, targetdir=None): # Write json file to disk for logging purposes if targetdir: outputfile = os.path.join(targetdir, filename + '.json') - if not os.path.exists(os.path.dirname(outputfile)): + + # Exception if directory exists, e.g. because another thread created it concurrently + try: os.makedirs(os.path.dirname(outputfile)) + except OSError, e: + pass + with open(outputfile, 'w') as fd: fd.write(json.dumps(config, indent=4))