From 66ea4d199e3d9266b1b5fdb8752772e8137ffdea Mon Sep 17 00:00:00 2001 From: Nicolas Ochsner Date: Fri, 28 Apr 2023 16:09:20 +0200 Subject: [PATCH] fix: Fix race condition when creating lock directory (#2225) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Description This PR makes two changes: 1. when `--nolock` is set, it will avoid creating a lock directory 2. when multiple instances of snakemake are running there is a race condition on the `os.mkdir` call. Switching to `os.makedirs` and setting `exist_ok=True` will simply avoid raising an exception in this case. ### QC * [x] The PR contains a test case for the changes or the changes are already covered by an existing test case. * [x] The documentation (`docs/`) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake). --------- Co-authored-by: Johannes Köster --- snakemake/persistence.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snakemake/persistence.py b/snakemake/persistence.py index f3fd69da7..3b23c3389 100755 --- a/snakemake/persistence.py +++ b/snakemake/persistence.py @@ -45,12 +45,12 @@ def __init__( self._serialize_param = self._serialize_param_builtin self._max_len = None + self.path = os.path.abspath(".snakemake") - if not os.path.exists(self.path): - os.mkdir(self.path) + os.makedirs(self.path, exist_ok=True) + self._lockdir = os.path.join(self.path, "locks") - if not os.path.exists(self._lockdir): - os.mkdir(self._lockdir) + os.makedirs(self._lockdir, exist_ok=True) self.dag = dag self._lockfile = dict()