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

This commit is contained in:
Sean Quah 2021-09-27 11:29:23 +01:00 committed by GitHub
parent d138187045
commit 6c83c27107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 13 deletions

1
changelog.d/10913.bugfix Normal file
View file

@ -0,0 +1 @@
Fix race conditions when creating media store and config directories.

View file

@ -200,11 +200,7 @@ class Config:
@classmethod @classmethod
def ensure_directory(cls, dir_path): def ensure_directory(cls, dir_path):
dir_path = cls.abspath(dir_path) dir_path = cls.abspath(dir_path)
try: os.makedirs(dir_path, exist_ok=True)
os.makedirs(dir_path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if not os.path.isdir(dir_path): if not os.path.isdir(dir_path):
raise ConfigError("%s is not a directory" % (dir_path,)) raise ConfigError("%s is not a directory" % (dir_path,))
return dir_path return dir_path
@ -693,8 +689,7 @@ class RootConfig:
open_private_ports=config_args.open_private_ports, open_private_ports=config_args.open_private_ports,
) )
if not path_exists(config_dir_path): os.makedirs(config_dir_path, exist_ok=True)
os.makedirs(config_dir_path)
with open(config_path, "w") as config_file: with open(config_path, "w") as config_file:
config_file.write(config_str) config_file.write(config_str)
config_file.write("\n\n# vim:ft=yaml") config_file.write("\n\n# vim:ft=yaml")

View file

@ -132,8 +132,7 @@ class MediaStorage:
fname = os.path.join(self.local_media_directory, path) fname = os.path.join(self.local_media_directory, path)
dirname = os.path.dirname(fname) dirname = os.path.dirname(fname)
if not os.path.exists(dirname): os.makedirs(dirname, exist_ok=True)
os.makedirs(dirname)
finished_called = [False] finished_called = [False]
@ -244,8 +243,7 @@ class MediaStorage:
return legacy_local_path return legacy_local_path
dirname = os.path.dirname(local_path) dirname = os.path.dirname(local_path)
if not os.path.exists(dirname): os.makedirs(dirname, exist_ok=True)
os.makedirs(dirname)
for provider in self.storage_providers: for provider in self.storage_providers:
res: Any = await provider.fetch(path, file_info) res: Any = await provider.fetch(path, file_info)

View file

@ -138,8 +138,7 @@ class FileStorageProviderBackend(StorageProvider):
backup_fname = os.path.join(self.base_directory, path) backup_fname = os.path.join(self.base_directory, path)
dirname = os.path.dirname(backup_fname) dirname = os.path.dirname(backup_fname)
if not os.path.exists(dirname): os.makedirs(dirname, exist_ok=True)
os.makedirs(dirname)
await defer_to_thread( await defer_to_thread(
self.hs.get_reactor(), shutil.copyfile, primary_fname, backup_fname self.hs.get_reactor(), shutil.copyfile, primary_fname, backup_fname