Skip to content

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

Open
xiaoyuyu6420 wants to merge 1 commit into
AstrBotDevs:masterfrom
xiaoyuyu6420:fix/9855-config-save-dir-makedirs
Open

fix: ensure config directory exists before mkstemp in _write_config_snapshot#9856
xiaoyuyu6420 wants to merge 1 commit into
AstrBotDevs:masterfrom
xiaoyuyu6420:fix/9855-config-save-dir-makedirs

Conversation

@xiaoyuyu6420

@xiaoyuyu6420 xiaoyuyu6420 commented Aug 28, 2026

Copy link
Copy Markdown

Problem

Three dashboard tests are intermittently failing on master (different ones fail on different runs):

  • test_t2i_set_active_template_syncs_all_configs
  • test_t2i_reset_default_template_syncs_all_configs
  • test_t2i_update_active_template_reloads_all_schedulers
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).

Root Cause

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

directory = os.path.dirname(os.path.abspath(self.config_path)) or "."
fd, temp_path = tempfile.mkstemp(dir=directory, ...)  # crashes if dir missing

When AstrBotConfig.__init__ (line 64-67) detects the config file doesn't exist yet, it calls save_config()_write_config_snapshotmkstemp(dir=directory). If the directory hasn't been created (fresh profile creation, test fixture race), mkstemp raises FileNotFoundError.

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.

Fix

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

Verification

Before fix (current master d2d7e5a):

3 failed, 79 deselected  (different tests fail each run)

After fix (5 consecutive runs):

3 passed, 79 deselected  (×5, stable)

Full suite: 2201 passed, 0 failed.

ruff format / ruff check clean.

Closes #9855.

Summary by Sourcery

Bug Fixes:

  • Prevent configuration snapshot writes from failing when the target configuration directory does not yet exist.

…napshot

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 AstrBotDevs#9855.
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Aug 28, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. If the path is wrong or directory creation has unintended side effects, merging can leave an extra directory on disk after the code is reverted. That state is bounded and can be removed manually; the normal failure mode is a config snapshot write error rather than irreversible data loss.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@xiaoyuyu6420

Copy link
Copy Markdown
Author

Friendly ping @Soulter — this is a 4-line fix (os.makedirs before mkstemp) for three intermittently-failing dashboard tests on master. CI workflows are awaiting fork-PR approval; Sourcery reviewed positively. Would appreciate a quick look when convenient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant