From 36df0c7c327cb56ab79ea5a9dd91df7fb8a6e7a6 Mon Sep 17 00:00:00 2001 From: oesteban Date: Thu, 18 Apr 2019 09:15:25 -0700 Subject: [PATCH 1/2] fix(node): do not override exceptions when the unfinished hashfile does not exist --- nipype/pipeline/engine/nodes.py | 6 ++++-- nipype/utils/filemanip.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/nipype/pipeline/engine/nodes.py b/nipype/pipeline/engine/nodes.py index b338fd862d..9a617dc8cb 100644 --- a/nipype/pipeline/engine/nodes.py +++ b/nipype/pipeline/engine/nodes.py @@ -27,7 +27,7 @@ from ...utils.filemanip import (md5, FileNotFoundError, ensure_list, simplify_list, copyfiles, fnames_presuffix, loadpkl, split_filename, load_json, makedirs, - emptydirs, savepkl, to_str, indirectory) + emptydirs, savepkl, to_str, indirectory, silentrm) from ...interfaces.base import (traits, InputMultiPath, CommandLine, Undefined, DynamicTraitedSpec, Bunch, InterfaceResult, @@ -474,7 +474,9 @@ def run(self, updatehash=False): except Exception: logger.warning('[Node] Error on "%s" (%s)', self.fullname, outdir) # Tear-up after error - os.remove(hashfile_unfinished) + if not silentrm(hashfile_unfinished): + logger.debug('Unfinished hashfile %s does not exist', + hashfile_unfinished) raise # Tear-up after success diff --git a/nipype/utils/filemanip.py b/nipype/utils/filemanip.py index f229061396..d8a65a6712 100644 --- a/nipype/utils/filemanip.py +++ b/nipype/utils/filemanip.py @@ -832,6 +832,27 @@ def emptydirs(path, noexist_ok=False): makedirs(path) +def silentrm(filename): + """ + Equivalent to ``rm -f``, returns ``False`` if the file did not + exist. + + Parameters + ---------- + + filename : str + file to be deleted + + """ + try: + os.remove(filename) + except OSError as e: + if e.errno != errno.ENOENT: + raise + return False + return True + + def which(cmd, env=None, pathext=None): """ Return the path to an executable which would be run if the given From 6a0763c0419081634521d4ca43107c0b8660d40c Mon Sep 17 00:00:00 2001 From: oesteban Date: Thu, 18 Apr 2019 15:29:01 -0700 Subject: [PATCH 2/2] fix(nodes): flag that this error may point to race conditions --- nipype/pipeline/engine/nodes.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nipype/pipeline/engine/nodes.py b/nipype/pipeline/engine/nodes.py index 9a617dc8cb..fc9e6e4b4e 100644 --- a/nipype/pipeline/engine/nodes.py +++ b/nipype/pipeline/engine/nodes.py @@ -440,7 +440,6 @@ def run(self, updatehash=False): for outdatedhash in glob(op.join(self.output_dir(), '_0x*.json')): os.remove(outdatedhash) - # Hashfile while running hashfile_unfinished = op.join( outdir, '_0x%s_unfinished.json' % self._hashvalue) @@ -475,8 +474,10 @@ def run(self, updatehash=False): logger.warning('[Node] Error on "%s" (%s)', self.fullname, outdir) # Tear-up after error if not silentrm(hashfile_unfinished): - logger.debug('Unfinished hashfile %s does not exist', - hashfile_unfinished) + logger.warning("""\ +Interface finished unexpectedly and the corresponding unfinished hashfile %s \ +does not exist. Another nipype instance may be running against the same work \ +directory. Please ensure no other concurrent workflows are racing""", hashfile_unfinished) raise # Tear-up after success