Skip to content

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

fix(config): release deferred flush reservation when threads cannot start

Rudy Celeklic7d3a5620c0b
diff --git a/app/config/config.py b/app/config/config.py
index f7dedea4..1fdbc46d 100644
--- a/app/config/config.py
+++ b/app/config/config.py
@@ -240,11 +240,19 @@ def _schedule_deferred_config_flush():
return
_pending_config_flush_scheduled = True
- threading.Thread(
- target=_run_deferred_config_flush,
- name="mpt-config-flush",
- daemon=True,
- ).start()
+ try:
+ threading.Thread(
+ target=_run_deferred_config_flush,
+ name="mpt-config-flush",
+ daemon=True,
+ ).start()
+ except Exception:
+ # No worker owns this reservation when construction/start fails.
+ # Leave queued updates and the save request intact so a later call
+ # can schedule another flush rather than silently waiting forever.
+ with _pending_config_lock:
+ _pending_config_flush_scheduled = False
+ raise
def try_save_config():
diff --git a/test/services/test_config_flush_start.py b/test/services/test_config_flush_start.py
new file mode 100644
index 00000000..9b777bf7
--- /dev/null
+++ b/test/services/test_config_flush_start.py
@@ -0,0 +1,46 @@
+from unittest.mock import Mock, patch
+
+import pytest
+
+from app.config import config
+
+
+def test_failed_config_flush_thread_start_can_be_retried():
+ section = config._SynchronizedConfig({"model": "old"})
+ with config._pending_config_lock:
+ original_scheduled = config._pending_config_flush_scheduled
+ original_updates = dict(config._pending_config_updates)
+ config._pending_config_flush_scheduled = False
+ config._pending_config_updates[config._pending_update_key(section, "model")] = (section, "model", "new")
+ first = Mock()
+ first.start.side_effect = RuntimeError("cannot start thread")
+ second = Mock()
+ try:
+ with patch.object(config.threading, "Thread", side_effect=[first, second]) as factory:
+ with pytest.raises(RuntimeError, match="cannot start thread"):
+ config._schedule_deferred_config_flush()
+ assert not config._pending_config_flush_scheduled
+ assert config.snapshot_config_with_pending(section)["model"] == "new"
+ config._schedule_deferred_config_flush()
+ assert config._pending_config_flush_scheduled
+ assert factory.call_count == 2
+ second.start.assert_called_once_with()
+ finally:
+ with config._pending_config_lock:
+ config._pending_config_flush_scheduled = original_scheduled
+ config._pending_config_updates.clear()
+ config._pending_config_updates.update(original_updates)
+
+
+def test_failed_config_flush_thread_construction_clears_reservation():
+ with config._pending_config_lock:
+ original = config._pending_config_flush_scheduled
+ config._pending_config_flush_scheduled = False
+ try:
+ with patch.object(config.threading, "Thread", side_effect=RuntimeError("cannot construct thread")):
+ with pytest.raises(RuntimeError, match="cannot construct thread"):
+ config._schedule_deferred_config_flush()
+ assert not config._pending_config_flush_scheduled
+ finally:
+ with config._pending_config_lock:
+ config._pending_config_flush_scheduled = original