1
0
mirror of https://github.com/comfyanonymous/ComfyUI.git synced 2026-10-24 02:49:04 +03:00
Files
ComfyUI/tests-unit/conftest.py
Simon Pinfold ae913fdcc4 fix(assets): write scan inserts in short transactions so uploads and output saves aren't locked out + use WAL synchronous=NORMAL (#16748)
* fix(assets): insert scanned files in batches of 200 with a pause between them

The fast scan wrote 500 files per write transaction and took the lock back
within milliseconds for the next batch. A foreground write (an upload's
registration, an output being recorded after a prompt) waits in SQLite's
busy handler, which retries after sleeps of up to 100 ms, so it almost
never saw the lock free and failed with "database is locked" after the
5 s busy timeout, while /upload/image held the event loop the whole time.

The scan now inserts 200 files per transaction and, while more follow,
sleeps for SQLite's longest busy-handler sleep plus a Windows timer tick
(0.12 s). Each batch is still one call to insert_asset_specs with its
existing error handling, and pause and cancel are checked between batches
as before.

* perf(db): sync the WAL at checkpoints instead of on every commit

With synchronous=FULL, every commit fsyncs the WAL while it still holds
the write lock, about 100 ms on a hard drive, which a waiting foreground
write has to sit through. In WAL mode, synchronous=NORMAL syncs at
checkpoints instead. An app crash loses nothing; a power loss or OS crash
can roll back commits the OS had not yet flushed, typically the last tens
of seconds, and cannot corrupt the database. Behind WAL_SYNCHRONOUS so it
can be set back to "FULL" on its own, and applied only once WAL mode is
confirmed, so a database in rollback-journal mode is left as it was.
2026-10-04 17:27:24 -07:00

19 lines
699 B
Python

import pytest
@pytest.fixture
def file_db(tmp_path, monkeypatch):
"""The real file database: WAL mode, both engines and their hooks, in ``tmp_path``."""
from app.database import db as db_module
db_path = str(tmp_path / "comfyui.db")
monkeypatch.setattr(db_module.args, "database_url", f"sqlite:///{db_path}")
monkeypatch.setattr(db_module, "Session", None)
monkeypatch.setattr(db_module, "WriteSession", None)
monkeypatch.setattr(db_module, "_db_lock", None)
db_module._init_file_db(db_module.args.database_url)
yield db_path
db_module.Session.kw["bind"].dispose()
db_module.WriteSession.kw["bind"].dispose()
db_module._db_lock.release(force=True)