AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug] Flaky t2i dashboard tests: FileNotFoundError in config save when directory missing

Open xiaoyuyu6420 opened this issue 3 months ago • 0 comments

What happened / 发生了什么

Three dashboard tests are intermittently failing on master (different ones fail on different runs — classic flaky behavior):

  • tests/test_dashboard.py::test_t2i_set_active_template_syncs_all_configs
  • tests/test_dashboard.py::test_t2i_reset_default_template_syncs_all_configs
  • tests/test_dashboard.py::test_t2i_update_active_template_reloads_all_schedulers

Error

FileNotFoundError: [Errno 2] No such file or directory:
  '.../data/config/.abconf_<uuid>.json.<random>.tmp'

The traceback points to tempfile.mkstemp() inside AstrBotConfig._write_config_snapshot() (astrbot/core/config/astrbot_config.py:290), which is called when a new config profile is first persisted.

Root Cause

_write_config_snapshot creates a temp file in the config file's parent directory, but never ensures that directory exists:

# astrbot/core/config/astrbot_config.py:289-294
directory = os.path.dirname(os.path.abspath(self.config_path)) or "."
fd, temp_path = tempfile.mkstemp(
    dir=directory,          # ← crashes if directory doesn't exist yet
    prefix=f".{os.path.basename(self.config_path)}.",
    suffix=".tmp",
)

When AstrBotConfig.__init__ (line 64-67) detects the config file doesn't exist yet, it calls save_config() → _write_config_snapshot → mkstemp(dir=directory). If the directory hasn't been created (e.g., fresh profile creation, test fixture race), mkstemp raises FileNotFoundError because the OS can't create a file in a nonexistent directory.

The create_conf path in astrbot_config_mgr.py:208-210 triggers this — it constructs a new conf_path and instantiates AstrBotConfig(config_path=conf_path, ...), which calls save_config() during __init__ before the directory is guaranteed to exist.

Reproduce / 如何复现

uv run python -m pytest tests/test_dashboard.py -q -k "t2i"

Run it a few times — the failure is intermittent (different tests fail on different runs). On current master (d2d7e5a), all three fail consistently when run together.

Proposed Fix

Add os.makedirs(directory, exist_ok=True) before the mkstemp call in _write_config_snapshot. This is the standard defensive pattern for atomic-write helpers — the directory is almost always present, but when it's not, the save should create it rather than crash.

xiaoyuyu6420 avatar Aug 28 '26 07:08 xiaoyuyu6420