From 935fba6a43c7d921d1486d94f67bd48a3248f074 Mon Sep 17 00:00:00 2001 From: Christophe Vandeplas Date: Tue, 14 Jan 2020 21:09:31 +0100 Subject: [PATCH] fix: [galaxies] lock when generating galaxies cache, fixes #23 --- src/MISP_maltego/transforms/common/util.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/MISP_maltego/transforms/common/util.py b/src/MISP_maltego/transforms/common/util.py index aac9144..9653d54 100644 --- a/src/MISP_maltego/transforms/common/util.py +++ b/src/MISP_maltego/transforms/common/util.py @@ -501,6 +501,12 @@ def galaxy_update_local_copy(force=False): force = True if force: + # FIXME create a lock to prevent two processes doing the same, and writing to the file at the same time + lockfile = local_path_uuid_mapping + '.lock' + from pathlib import Path + while os.path.exists(lockfile): + time.sleep(0.3) + Path(local_path_uuid_mapping + '.lock').touch() # download the latest zip of the public galaxy try: resp = requests.get(galaxy_archive_url) @@ -542,7 +548,9 @@ def galaxy_update_local_copy(force=False): pass with open(local_path_uuid_mapping, 'w') as f: - json.dump(cluster_uuids, f, sort_keys=True, indent=4) + json.dump(cluster_uuids, f) + # remove the lock + os.remove(lockfile) def galaxy_load_cluster_mapping():