# Buggy code
def save_targets_ensemble(self, targets):
    self._make_internals_directory()
    if not isinstance(targets, np.ndarray):
        raise ValueError(
            "Targets must be of type np.ndarray, but is %s" % type(targets)
        )

    filepath = self._get_targets_ensemble_filename()

    lock_path = filepath + ".lock"
    with lockfile.LockFile(lock_path):
        if os.path.exists(filepath):
            existing_targets = np.load(filepath)
            if existing_targets.shape[0] > targets.shape[0] or (
                existing_targets.shape == targets.shape
                and np.allclose(existing_targets, targets)
            ):
                return filepath

        with tempfile.NamedTemporaryFile(
            "wb", dir=os.path.dirname(filepath), delete=False
        ) as fh:
            np.save(fh, targets.astype(np.float32))
            tempname = fh.name

        os.rename(tempname, filepath)

    return filepath


# Traceback (FileExistsError)
File "/x/Redly/anaconda3/lib/python3.5/site-packages/lockfile/linklockfile.py", line 31, in acquire
os.link(self.unique_name, self.lock_file)
FileExistsError: [Errno 17] File exists: '/x/Redly/truffles/nfs_share/atsklrn_tmp/.auto-sklearn/mgm-19-6811-41172700.20609-2442176699744317723' -> '/x/Redly/truffles/nfs_share/atsklrn_tmp/.auto-sklearn/true_targets_ensemble.npy.lock.lock'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "nfs_share/Redly-SQL-Example2.py", line 449, in <module>
fit_classifiers(dataframe, pool_size, atsklrn_tempdir, slave_node, predict_only)
File "nfs_share/Redly-SQL-Example2.py", line 271, in fit_classifiers
,ensemble_nbest=15)
File "/x/Redly/anaconda3/lib/python3.5/site-packages/autosklearn/automl.py", line 480, in fit_ensemble
self._proc_ensemble.main()
File "/x/Redly/anaconda3/lib/python3.5/site-packages/autosklearn/ensemble_builder.py", line 94, in main
targets_ensemble = backend.load_targets_ensemble()
File "/x/Redly/anaconda3/lib/python3.5/site-packages/autosklearn/util/backend.py", line 117, in load_targets_ensemble
with lockfile.LockFile(lock_path):
File "/x/Redly/anaconda3/lib/python3.5/site-packages/lockfile/__init__.py", line 197, in __enter__
self.acquire()
File "/x/Redly/anaconda3/lib/python3.5/site-packages/lockfile/linklockfile.py", line 50, in acquire
time.sleep(timeout is not None and timeout / 10 or 0.1)
KeyboardInterrupt
Traceback (most recent call last):
File "/x/Redly/anaconda3/lib/python3.5/site-packages/lockfile/linklockfile.py", line 31, in acquire
os.link(self.unique_name, self.lock_file)
FileExistsError: [Errno 17] File exists: '/x/Redly/truffles/nfs_share/atsklrn_tmp/.auto-sklearn/mgm-19-6811-41172700.20609-2442176699744317723' -> '/x/Redly/truffles/nfs_share/atsklrn_tmp/.auto-sklearn/true_targets_ensemble.npy.lock.lock'

# Fixed code
def save_targets_ensemble(self, targets):
    self._make_internals_directory()
    if not isinstance(targets, np.ndarray):
        raise ValueError(
            "Targets must be of type np.ndarray, but is %s" % type(targets)
        )

    filepath = self._get_targets_ensemble_filename()

    # Try to open the file without locking it, this will reduce the
    # number of times where we erronously keep a lock on the ensemble
    # targets file although the process already was killed
    try:
        existing_targets = np.load(filepath)
        if existing_targets.shape[0] > targets.shape[0] or (
            existing_targets.shape == targets.shape
            and np.allclose(existing_targets, targets)
        ):
            return filepath
    except Exception:
        pass

    lock_path = filepath + ".lock"
    with lockfile.LockFile(lock_path):
        if os.path.exists(filepath):
            existing_targets = np.load(filepath)
            if existing_targets.shape[0] > targets.shape[0] or (
                existing_targets.shape == targets.shape
                and np.allclose(existing_targets, targets)
            ):
                return filepath

        with tempfile.NamedTemporaryFile(
            "wb", dir=os.path.dirname(filepath), delete=False
        ) as fh:
            np.save(fh, targets.astype(np.float32))
            tempname = fh.name

        os.rename(tempname, filepath)

    return filepath