Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(config): support bind-mounted config files

harry0703e5bb283d2f3c
diff --git a/app/config/config.py b/app/config/config.py
index f87c4637..b66d620f 100644
--- a/app/config/config.py
+++ b/app/config/config.py
@@ -1,3 +1,4 @@
+import errno
import os
import shutil
import socket
@@ -248,6 +249,11 @@ def save_config():
另一个线程可能读取到只写了一部分的 TOML 内容。这里使用进程内可重入锁串行化
保存,并先写入同目录临时文件,再通过 os.replace 原子替换目标文件。
+ Docker Desktop 单文件 bind mount 会把 config.toml 本身作为挂载点,
+ Linux 内核不允许通过 rename/replace 替换挂载点,因此会返回 EBUSY。
+ 该场景下只能在锁内原地覆盖文件;其它异常仍然抛出,避免掩盖权限、磁盘
+ 或路径错误。
+
这仍然保留项目现有的单用户全局配置语义,不额外引入复杂的多用户配置系统;
主要用于避免多标签页或快速 rerun 时损坏配置文件。
"""
@@ -283,7 +289,20 @@ def save_config():
f.write(serialized_config)
f.flush()
os.fsync(f.fileno())
- os.replace(temp_path, config_file)
+ try:
+ os.replace(temp_path, config_file)
+ except OSError as exc:
+ if exc.errno != errno.EBUSY:
+ raise
+
+ logger.warning(
+ "atomic config replacement is unavailable for the mounted "
+ f"file, fallback to in-place write: {config_file}"
+ )
+ with open(config_file, mode="w", encoding="utf-8") as f:
+ f.write(serialized_config)
+ f.flush()
+ os.fsync(f.fileno())
_cfg.clear()
_cfg.update(config_to_save)
finally:
diff --git a/test/services/test_config.py b/test/services/test_config.py
index 3e5327a7..94d83a8c 100644
--- a/test/services/test_config.py
+++ b/test/services/test_config.py
@@ -1,3 +1,4 @@
+import errno
import threading
import tomllib
from pathlib import Path
@@ -89,6 +90,79 @@ class TestConfigPersistence:
config._cfg.clear()
config._cfg.update(original_cfg)
+ def test_save_config_falls_back_for_bind_mounted_file(self):
+ """
+ Docker Desktop 的单文件挂载点不能被 os.replace 替换。遇到 EBUSY 时
+ 应在锁内原地覆盖,并确保最终内容完整、可解析且不遗留临时文件。
+ """
+ original_cfg = dict(config._cfg)
+ original_app = dict(config.app)
+ try:
+ with TemporaryDirectory() as temp_dir:
+ config_path = Path(temp_dir) / "config.toml"
+ config_path.write_text("[app]\nold_value = true\n", encoding="utf-8")
+ config.app["bind_mount_save_test"] = "ok"
+
+ with (
+ patch.object(config, "root_dir", temp_dir),
+ patch.object(config, "config_file", str(config_path)),
+ patch.object(
+ config.os,
+ "replace",
+ side_effect=OSError(
+ errno.EBUSY,
+ "Device or resource busy",
+ ),
+ ),
+ patch.object(config.logger, "warning") as warning_mock,
+ ):
+ config.save_config()
+
+ saved_config = tomllib.loads(config_path.read_text(encoding="utf-8"))
+ assert saved_config["app"]["bind_mount_save_test"] == "ok"
+ assert list(Path(temp_dir).glob(".config-*.toml.tmp")) == []
+ warning_mock.assert_called_once()
+ finally:
+ config.app.clear()
+ config.app.update(original_app)
+ config._cfg.clear()
+ config._cfg.update(original_cfg)
+
+ def test_save_config_does_not_hide_other_replace_errors(self):
+ """非 EBUSY 错误必须继续抛出,不能把权限或磁盘故障伪装成保存成功。"""
+ original_cfg = dict(config._cfg)
+ original_app = dict(config.app)
+ try:
+ with TemporaryDirectory() as temp_dir:
+ config_path = Path(temp_dir) / "config.toml"
+ config_path.write_text("[app]\nold_value = true\n", encoding="utf-8")
+ config.app["replace_error_test"] = "not-saved"
+
+ with (
+ patch.object(config, "root_dir", temp_dir),
+ patch.object(config, "config_file", str(config_path)),
+ patch.object(
+ config.os,
+ "replace",
+ side_effect=OSError(errno.EACCES, "Permission denied"),
+ ),
+ ):
+ try:
+ config.save_config()
+ except OSError as exc:
+ assert exc.errno == errno.EACCES
+ else:
+ raise AssertionError("expected config save to fail")
+
+ saved_config = tomllib.loads(config_path.read_text(encoding="utf-8"))
+ assert saved_config["app"]["old_value"] is True
+ assert list(Path(temp_dir).glob(".config-*.toml.tmp")) == []
+ finally:
+ config.app.clear()
+ config.app.update(original_app)
+ config._cfg.clear()
+ config._cfg.update(original_cfg)
+
def test_runtime_config_lock_blocks_concurrent_config_writes(self):
"""长任务持有运行锁时,其它会话不能在任务中途改写全局配置。"""
write_started = threading.Event()