Skip to content

Commit e66fc97

Browse files
committed
hw-mgmgt: script: Fix race conditions for write in thermal updater
Prevent race conditions by using atomic file writes. Replace open()/write() with atomic_file_write() to ensure readers never see empty or partial content when thermal attributes are updated. Bug: 4647791
1 parent df80502 commit e66fc97

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

usr/usr/bin/hw_management_lib.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,30 @@
3838
import syslog
3939
from threading import Lock
4040
import time
41+
import tempfile
4142

4243

4344
# ----------------------------------------------------------------------
45+
46+
def atomic_file_write(file_name, value):
47+
"""
48+
@summary:
49+
Write value to file atomically
50+
@param file_name: name of file
51+
@param value: value to write
52+
"""
53+
fd, f_path_tmp = tempfile.mkstemp(dir=os.path.dirname(file_name), prefix='.tmp_')
54+
try:
55+
with os.fdopen(fd, 'w', encoding="utf-8") as f:
56+
f.write("{}".format(value))
57+
os.replace(f_path_tmp, file_name) # Atomic on POSIX
58+
except Exception as e:
59+
os.unlink(f_path_tmp) # Cleanup on failure
60+
raise Exception(f"Error writing {file_name}: {e}")
61+
62+
# ----------------------------------------------------------------------
63+
64+
4465
def current_milli_time():
4566
"""
4667
@summary:

usr/usr/bin/hw_management_sync.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import argparse
4545
import traceback
4646
from hw_management_lib import HW_Mgmt_Logger as Logger
47+
from hw_management_lib import atomic_file_write
4748
from collections import Counter
4849

4950
from hw_management_redfish_client import RedfishClient, BMCAccessor
@@ -680,9 +681,8 @@ def asic_temp_populate(arg_list, arg):
680681
# Write the temperature data to files
681682
for suffix, value in file_paths.items():
682683
f_name = "/var/run/hw-management/thermal/{}{}".format(asic_name, suffix)
683-
with open(f_name, 'w', encoding="utf-8") as f:
684-
f.write("{}\n".format(value))
685-
LOGGER.debug(f"Write {asic_name}{suffix}: {value}")
684+
atomic_file_write(f_name, str(value) + "\n")
685+
LOGGER.debug(f"Write {asic_name}{suffix}: {value}")
686686

687687
asic_chipup_completed_fname = os.path.join("/var/run/hw-management/config", "asic_chipup_completed")
688688
asic_num_fname = os.path.join("/var/run/hw-management/config", "asic_num")
@@ -704,11 +704,8 @@ def asic_temp_populate(arg_list, arg):
704704
else:
705705
asics_init_done = 0
706706

707-
with open(asics_init_done_fname, 'w+', encoding="utf-8") as f:
708-
f.write(str(asics_init_done) + "\n")
709-
710-
with open(asic_chipup_completed_fname, 'w', encoding="utf-8") as f:
711-
f.write(str(asic_chipup_completed) + "\n")
707+
atomic_file_write(asics_init_done_fname, str(asics_init_done) + "\n")
708+
atomic_file_write(asic_chipup_completed_fname, str(asic_chipup_completed) + "\n")
712709

713710
# ----------------------------------------------------------------------
714711

@@ -801,15 +798,13 @@ def module_temp_populate(arg_list, _dummy):
801798
for suffix, value in file_paths.items():
802799
f_name = "/var/run/hw-management/thermal/{}{}".format(module_name, suffix)
803800
if value is not None:
804-
with open(f_name, 'w', encoding="utf-8") as f:
805-
f.write("{}\n".format(value))
806-
LOGGER.debug(f"Write {module_name}{suffix}: {value}")
801+
atomic_file_write(f_name, str(value) + "\n")
802+
LOGGER.debug(f"Write {module_name}{suffix}: {value}")
807803
module_updated = True
808804

809805
if module_updated:
810806
LOGGER.debug("{} module_counter ({}) updated".format(module_name, module_count))
811-
with open("/var/run/hw-management/config/module_counter", 'w+', encoding="utf-8") as f:
812-
f.write("{}\n".format(module_count))
807+
atomic_file_write("/var/run/hw-management/config/module_counter", str(module_count) + "\n")
813808
return
814809

815810
# ----------------------------------------------------------------------

0 commit comments

Comments
 (0)