From 5084f75724819363dbc7ef879ad34fa9bf9f8754 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 23 May 2025 15:43:46 +0000 Subject: [PATCH] util.make_dir: handle multi-process race When launching an electrum daemon and an electrum cli command quickly after each other, in a test using a random fresh datadir, they both try to create the same folder hierarchy and race on the "if not exist: mkdir" commands. --- electrum/util.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/electrum/util.py b/electrum/util.py index d00246f00..26aec0e1b 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -1159,13 +1159,21 @@ def os_chmod(path, mode): raise -def make_dir(path, allow_symlink=True): - """Make directory if it does not yet exist.""" +def make_dir(path, *, allow_symlink=True): + """Makes directory if it does not yet exist. + Also sets sane 0700 permissions on the dir. + """ if not os.path.exists(path): if not allow_symlink and os.path.islink(path): raise Exception('Dangling link: ' + path) - os.mkdir(path) + try: + os.mkdir(path) + except FileExistsError: + # this can happen in a multiprocess race, e.g. when an electrum daemon + # and an electrum cli command are launched in rapid fire + pass os_chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) + assert os.path.exists(path) def is_subpath(long_path: str, short_path: str) -> bool: