Skip to content

Commit 5bc40aa

Browse files
authored
Merge pull request #80 from molmod/cp2k_file_race_condition
enforce lock during file creation
2 parents f791b0d + 1846e1a commit 5bc40aa

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

psiflow/execution.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import shutil
77
import sys
88
from pathlib import Path
9+
from threading import Lock
910

1011
# see https://stackoverflow.com/questions/59904631/python-class-constants-in-dataclasses
1112
from typing import Any, Optional, Union
@@ -420,6 +421,7 @@ def __init__(
420421
self.definitions = {d.name: d for d in definitions}
421422
assert len(self.definitions) == len(definitions)
422423
self.file_index = {}
424+
self.lock = Lock()
423425
parsl.load(config)
424426

425427
def __enter__(self):
@@ -431,16 +433,17 @@ def __exit__(self, exc_type, exc_value, traceback):
431433
parsl.dfk().cleanup()
432434

433435
def new_file(self, prefix: str, suffix: str) -> File:
434-
assert prefix[-1] == "_"
435-
assert suffix[0] == "."
436-
key = (prefix, suffix)
437-
if key not in self.file_index.keys():
438-
self.file_index[key] = 0
439-
padding = 6
440-
assert self.file_index[key] < (16**padding)
441-
identifier = "{0:0{1}x}".format(self.file_index[key], padding)
442-
self.file_index[key] += 1
443-
return File(str(self.path / (prefix + identifier + suffix)))
436+
with self.lock:
437+
assert prefix[-1] == "_"
438+
assert suffix[0] == "."
439+
key = (prefix, suffix)
440+
if key not in self.file_index.keys():
441+
self.file_index[key] = 0
442+
padding = 6
443+
assert self.file_index[key] < (16**padding)
444+
identifier = "{0:0{1}x}".format(self.file_index[key], padding)
445+
self.file_index[key] += 1
446+
return File(str(self.path / (prefix + identifier + suffix)))
444447

445448
@classmethod
446449
def from_config(

0 commit comments

Comments
 (0)