The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
fix(db): wait briefly for the database lock at startup (#16602)
Simon Pinfolda35fcbac26b8
diff --git a/app/database/db.py b/app/database/db.pyindex f0ee2255a..81ee0880a 100644--- a/app/database/db.py+++ b/app/database/db.py@@ -155,12 +155,16 @@ def _backup_database(source_path, destination_path):_db_lock = None+_LOCK_WAIT_SECONDS = 5.0def _acquire_file_lock(db_path):"""Acquire an OS-level file lock to prevent multi-process access.Uses filelock for cross-platform support (macOS, Linux, Windows).The OS automatically releases the lock when the process exits, even on crashes.+ If the lock is held, waits up to _LOCK_WAIT_SECONDS for it to be released, since a+ relaunch can start while the previous process is still exiting. The lock is never+ taken from a holder."""global _db_locklock_path = db_path + ".lock"@@ -168,11 +172,15 @@ def _acquire_file_lock(db_path):try:_db_lock.acquire(timeout=0)except Timeout:- raise RuntimeError(- f"Could not acquire lock on database '{db_path}'. "- "Another ComfyUI process may already be using it. "- "Use --database-url to specify a separate database file."- )+ logging.info(f"Database lock is held; waiting up to {_LOCK_WAIT_SECONDS:g}s for it to be released")+ try:+ _db_lock.acquire(timeout=_LOCK_WAIT_SECONDS)+ except Timeout:+ raise RuntimeError(+ f"Could not acquire lock on database '{db_path}'. "+ "Another ComfyUI process may already be using it. "+ "Use --database-url to specify a separate database file."+ )def _is_memory_db(db_url):diff --git a/tests-unit/app_test/test_db_init_locking.py b/tests-unit/app_test/test_db_init_locking.pyindex 05f02406e..c31065432 100644--- a/tests-unit/app_test/test_db_init_locking.py+++ b/tests-unit/app_test/test_db_init_locking.py@@ -83,7 +83,8 @@ def test_failed_init_releases_the_lock(stale_db, monkeypatch):contender.release()-def test_held_lock_blocks_before_any_migration_work(stale_db):+def test_held_lock_blocks_before_any_migration_work(stale_db, monkeypatch):+ monkeypatch.setattr(db_module, "_LOCK_WAIT_SECONDS", 0.1)holder = FileLock(stale_db + ".lock")holder.acquire(timeout=0)try:diff --git a/tests-unit/app_test/test_db_lock_wait.py b/tests-unit/app_test/test_db_lock_wait.pynew file mode 100644index 000000000..facdd53fa--- /dev/null+++ b/tests-unit/app_test/test_db_lock_wait.py@@ -0,0 +1,93 @@+import logging+import subprocess+import sys+from pathlib import Path++import pytest+from filelock import FileLock++from app.database import db as db_module++REPO_ROOT = Path(__file__).resolve().parents[2]+WAITING = "Database lock is held; waiting up to"++# Holds the lock until a line arrives on stdin, then releases it half a second later,+# so only a caller that actually waits can get it.+RELEASE_ON_SIGNAL_SCRIPT = (+ "import sys, time; "+ "from filelock import FileLock; "+ "lock = FileLock(sys.argv[1]); lock.acquire(timeout=0); "+ "print('held', flush=True); "+ "sys.stdin.readline(); "+ "time.sleep(0.5); "+ "lock.release()"+)+++class _ReleaseWhenWaiting(logging.Handler):+ """Tells the holder to let go once the lock call has logged that it is waiting, so+ the wait path is exercised regardless of scheduling."""++ def __init__(self, holder):+ super().__init__()+ self.holder = holder++ def emit(self, record):+ if WAITING in record.getMessage():+ self.holder.stdin.write("release\n")+ self.holder.stdin.flush()++[email protected](autouse=True)+def restore_module_lock(monkeypatch):+ monkeypatch.setattr(db_module, "_db_lock", None)+ yield+ if db_module._db_lock is not None:+ db_module._db_lock.release(force=True)+++def test_waits_for_a_holder_that_is_shutting_down(tmp_path, caplog):+ db_path = str(tmp_path / "comfyui.db")+ holder = subprocess.Popen(+ [sys.executable, "-c", RELEASE_ON_SIGNAL_SCRIPT, db_path + ".lock"],+ cwd=REPO_ROOT,+ stdin=subprocess.PIPE,+ stdout=subprocess.PIPE,+ text=True,+ )+ release = _ReleaseWhenWaiting(holder)+ logging.getLogger().addHandler(release)+ try:+ assert holder.stdout.readline().strip() == "held"++ with caplog.at_level(logging.INFO):+ db_module._acquire_file_lock(db_path)+ finally:+ logging.getLogger().removeHandler(release)+ holder.kill()+ holder.wait()++ assert db_module._db_lock.is_locked+ assert WAITING in caplog.text+++def test_gives_up_when_the_lock_stays_held(tmp_path, monkeypatch, caplog):+ db_path = str(tmp_path / "comfyui.db")+ monkeypatch.setattr(db_module, "_LOCK_WAIT_SECONDS", 0.3)+ holder = FileLock(db_path + ".lock")+ holder.acquire(timeout=0)+ try:+ with caplog.at_level(logging.INFO), pytest.raises(RuntimeError, match="Could not acquire lock"):+ db_module._acquire_file_lock(db_path)+ finally:+ holder.release()++ assert WAITING in caplog.text+++def test_free_lock_is_taken_without_waiting(tmp_path, caplog):+ with caplog.at_level(logging.INFO):+ db_module._acquire_file_lock(str(tmp_path / "comfyui.db"))++ assert db_module._db_lock.is_locked+ assert WAITING not in caplog.text