Skip to content

Commit 3b21e66

Browse files
authored
Merge pull request #2919 from oesteban/enh/robuster-node-tear-up
ENH: Do not override caught exceptions with FileNotFoundError from unfinished hashfile
2 parents 62bf7f0 + 6a0763c commit 3b21e66

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

nipype/pipeline/engine/nodes.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from ...utils.filemanip import (md5, FileNotFoundError, ensure_list,
2828
simplify_list, copyfiles, fnames_presuffix,
2929
loadpkl, split_filename, load_json, makedirs,
30-
emptydirs, savepkl, to_str, indirectory)
30+
emptydirs, savepkl, to_str, indirectory, silentrm)
3131

3232
from ...interfaces.base import (traits, InputMultiPath, CommandLine, Undefined,
3333
DynamicTraitedSpec, Bunch, InterfaceResult,
@@ -440,7 +440,6 @@ def run(self, updatehash=False):
440440
for outdatedhash in glob(op.join(self.output_dir(), '_0x*.json')):
441441
os.remove(outdatedhash)
442442

443-
444443
# Hashfile while running
445444
hashfile_unfinished = op.join(
446445
outdir, '_0x%s_unfinished.json' % self._hashvalue)
@@ -474,7 +473,11 @@ def run(self, updatehash=False):
474473
except Exception:
475474
logger.warning('[Node] Error on "%s" (%s)', self.fullname, outdir)
476475
# Tear-up after error
477-
os.remove(hashfile_unfinished)
476+
if not silentrm(hashfile_unfinished):
477+
logger.warning("""\
478+
Interface finished unexpectedly and the corresponding unfinished hashfile %s \
479+
does not exist. Another nipype instance may be running against the same work \
480+
directory. Please ensure no other concurrent workflows are racing""", hashfile_unfinished)
478481
raise
479482

480483
# Tear-up after success

nipype/utils/filemanip.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,27 @@ def emptydirs(path, noexist_ok=False):
832832
makedirs(path)
833833

834834

835+
def silentrm(filename):
836+
"""
837+
Equivalent to ``rm -f``, returns ``False`` if the file did not
838+
exist.
839+
840+
Parameters
841+
----------
842+
843+
filename : str
844+
file to be deleted
845+
846+
"""
847+
try:
848+
os.remove(filename)
849+
except OSError as e:
850+
if e.errno != errno.ENOENT:
851+
raise
852+
return False
853+
return True
854+
855+
835856
def which(cmd, env=None, pathext=None):
836857
"""
837858
Return the path to an executable which would be run if the given

0 commit comments

Comments
 (0)