From 361a0f945899e599815c8254e48a85005f53d05b Mon Sep 17 00:00:00 2001 From: Stas Glubokiy Date: Tue, 31 Jul 2018 00:37:14 +0300 Subject: [PATCH] Fix race condition in luigi.lock.acquire_for (#2357) (#2477) --- luigi/lock.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/luigi/lock.py b/luigi/lock.py index 1b31ed0c90..e1a604f540 100644 --- a/luigi/lock.py +++ b/luigi/lock.py @@ -21,6 +21,7 @@ """ from __future__ import print_function +import errno import hashlib import os import sys @@ -102,10 +103,14 @@ def acquire_for(pid_dir, num_available=1, kill_signal=None): my_pid, my_cmd, pid_file = get_info(pid_dir) - # Check if there is a pid file corresponding to this name - if not os.path.exists(pid_dir): + # Create a pid file if it does not exist + try: os.mkdir(pid_dir) os.chmod(pid_dir, 0o777) + except OSError as exc: + if exc.errno != errno.EEXIST: + raise + pass # Let variable "pids" be all pids who exist in the .pid-file who are still # about running the same command.