fix: ensure config directory exists before mkstemp in _write_config_snapshot (#9856)

AstrBotConfig._write_config_snapshot calls tempfile.mkstemp(dir=directory)
without verifying the directory exists. When a config profile is created
for the first time (e.g. create_conf instantiates AstrBotConfig with a
brand-new path), __init__ calls save_config before the directory is
guaranteed to exist, and mkstemp raises FileNotFoundError.

Add os.makedirs(directory, exist_ok=True) before mkstemp. This is the
standard defensive pattern for atomic-write helpers.

Fixes three intermittently-failing dashboard tests:
- test_t2i_set_active_template_syncs_all_configs
- test_t2i_reset_default_template_syncs_all_configs
- test_t2i_update_active_template_reloads_all_schedulers

Closes #9855.
This commit is contained in:
枭钰钰
2026-08-31 09:33:15 +08:00
committed by GitHub
parent f845a8d654
commit 0da69dd3f6
+4
View File
@@ -296,6 +296,10 @@ class AstrBotConfig(dict):
Whether the snapshot replaced the current configuration file.
"""
directory = os.path.dirname(os.path.abspath(self.config_path)) or "."
# The directory may not exist yet when a config profile is created for
# the first time (e.g. `create_conf` instantiates AstrBotConfig with a
# brand-new path). mkstemp would raise FileNotFoundError otherwise.
os.makedirs(directory, exist_ok=True)
fd, temp_path = tempfile.mkstemp(
dir=directory,
prefix=f".{os.path.basename(self.config_path)}.",