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

Commit 6c83c27

Browse files
authored
Fix race conditions when creating media store and config directories (#10913)
1 parent d138187 commit 6c83c27

File tree

4 files changed

+6
-13
lines changed

4 files changed

+6
-13
lines changed

changelog.d/10913.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix race conditions when creating media store and config directories.

synapse/config/_base.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,7 @@ def check_file(cls, file_path, config_name):
200200
@classmethod
201201
def ensure_directory(cls, dir_path):
202202
dir_path = cls.abspath(dir_path)
203-
try:
204-
os.makedirs(dir_path)
205-
except OSError as e:
206-
if e.errno != errno.EEXIST:
207-
raise
203+
os.makedirs(dir_path, exist_ok=True)
208204
if not os.path.isdir(dir_path):
209205
raise ConfigError("%s is not a directory" % (dir_path,))
210206
return dir_path
@@ -693,8 +689,7 @@ def load_or_generate_config(cls, description, argv):
693689
open_private_ports=config_args.open_private_ports,
694690
)
695691

696-
if not path_exists(config_dir_path):
697-
os.makedirs(config_dir_path)
692+
os.makedirs(config_dir_path, exist_ok=True)
698693
with open(config_path, "w") as config_file:
699694
config_file.write(config_str)
700695
config_file.write("\n\n# vim:ft=yaml")

synapse/rest/media/v1/media_storage.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def store_into_file(
132132
fname = os.path.join(self.local_media_directory, path)
133133

134134
dirname = os.path.dirname(fname)
135-
if not os.path.exists(dirname):
136-
os.makedirs(dirname)
135+
os.makedirs(dirname, exist_ok=True)
137136

138137
finished_called = [False]
139138

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

246245
dirname = os.path.dirname(local_path)
247-
if not os.path.exists(dirname):
248-
os.makedirs(dirname)
246+
os.makedirs(dirname, exist_ok=True)
249247

250248
for provider in self.storage_providers:
251249
res: Any = await provider.fetch(path, file_info)

synapse/rest/media/v1/storage_provider.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ async def store_file(self, path: str, file_info: FileInfo) -> None:
138138
backup_fname = os.path.join(self.base_directory, path)
139139

140140
dirname = os.path.dirname(backup_fname)
141-
if not os.path.exists(dirname):
142-
os.makedirs(dirname)
141+
os.makedirs(dirname, exist_ok=True)
143142

144143
await defer_to_thread(
145144
self.hs.get_reactor(), shutil.copyfile, primary_fname, backup_fname

0 commit comments

Comments
 (0)