Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix race conditions when creating media store and config directories #10913

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10913.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix race conditions when creating media store and config directories.
9 changes: 2 additions & 7 deletions synapse/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ def check_file(cls, file_path, config_name):
@classmethod
def ensure_directory(cls, dir_path):
dir_path = cls.abspath(dir_path)
try:
os.makedirs(dir_path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
os.makedirs(dir_path, exist_ok=True)
if not os.path.isdir(dir_path):
raise ConfigError("%s is not a directory" % (dir_path,))
return dir_path
Expand Down Expand Up @@ -693,8 +689,7 @@ def load_or_generate_config(cls, description, argv):
open_private_ports=config_args.open_private_ports,
)

if not path_exists(config_dir_path):
os.makedirs(config_dir_path)
os.makedirs(config_dir_path, exist_ok=True)
with open(config_path, "w") as config_file:
config_file.write(config_str)
config_file.write("\n\n# vim:ft=yaml")
Expand Down
6 changes: 2 additions & 4 deletions synapse/rest/media/v1/media_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ def store_into_file(
fname = os.path.join(self.local_media_directory, path)

dirname = os.path.dirname(fname)
if not os.path.exists(dirname):
os.makedirs(dirname)
os.makedirs(dirname, exist_ok=True)

finished_called = [False]

Expand Down Expand Up @@ -244,8 +243,7 @@ async def ensure_media_is_in_local_cache(self, file_info: FileInfo) -> str:
return legacy_local_path

dirname = os.path.dirname(local_path)
if not os.path.exists(dirname):
os.makedirs(dirname)
os.makedirs(dirname, exist_ok=True)

for provider in self.storage_providers:
res: Any = await provider.fetch(path, file_info)
Expand Down
3 changes: 1 addition & 2 deletions synapse/rest/media/v1/storage_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ async def store_file(self, path: str, file_info: FileInfo) -> None:
backup_fname = os.path.join(self.base_directory, path)

dirname = os.path.dirname(backup_fname)
if not os.path.exists(dirname):
os.makedirs(dirname)
os.makedirs(dirname, exist_ok=True)

await defer_to_thread(
self.hs.get_reactor(), shutil.copyfile, primary_fname, backup_fname
Expand Down