From 88f16a128adc3ec5549e6aef20e844c057f7a92e Mon Sep 17 00:00:00 2001 From: Chris Burr Date: Thu, 12 Dec 2024 15:06:15 +0100 Subject: [PATCH] sweep: #7946 Make writing the CS atomic --- src/DIRAC/ConfigurationSystem/private/ConfigurationData.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/DIRAC/ConfigurationSystem/private/ConfigurationData.py b/src/DIRAC/ConfigurationSystem/private/ConfigurationData.py index 6f9abb1c8cf..46b2d524d1a 100755 --- a/src/DIRAC/ConfigurationSystem/private/ConfigurationData.py +++ b/src/DIRAC/ConfigurationSystem/private/ConfigurationData.py @@ -7,6 +7,7 @@ import _thread import time import datetime +import secrets from diraccfg import CFG @@ -342,11 +343,15 @@ def __backupCurrentConfiguration(self, backupName): def writeRemoteConfigurationToDisk(self, backupName=False): configurationFile = os.path.join(DIRAC.rootPath, "etc", f"{self.getName()}.cfg") + configurationFileTmp = f"{configurationFile}.{secrets.token_hex(8)}" try: - with open(configurationFile, "w") as fd: + with open(configurationFileTmp, "w") as fd: fd.write(str(self.remoteCFG)) + os.rename(configurationFileTmp, configurationFile) except Exception as e: gLogger.fatal("Cannot write new configuration to disk!", f"file {configurationFile} exception {repr(e)}") + if os.path.isfile(configurationFileTmp): + os.remove(configurationFileTmp) return S_ERROR(f"Can't write cs file {configurationFile}!: {repr(e).replace(',)', ')')}") if backupName: self.__backupCurrentConfiguration(backupName)