Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Move file reads out of the remaining asset write transactions (#16486)

Simon Pinfoldf427c3a28550
diff --git a/app/assets/scanner.py b/app/assets/scanner.py
index 5671df0f6..9e0f1f32e 100644
--- a/app/assets/scanner.py
+++ b/app/assets/scanner.py
@@ -420,7 +420,12 @@ def observe_asset_specs(specs: list[SeedAssetSpec]) -> dict[str, _SpecObservatio
try:
stat_result = os.stat(path, follow_symlinks=True)
snapshot = snapshot_hash(path) if hashing_is_enabled else None
- except OSError:
+ except FileNotFoundError:
+ logging.warning("Skipping vanished asset during scan: %s", path)
+ observed[path] = None
+ continue
+ except OSError as e:
+ _log_scan_error("seed_observation", e)
observed[path] = None
continue
if snapshot is not None:
@@ -444,8 +449,7 @@ def seed_asset_specs(
try:
with session.begin_nested():
observation = observed[path]
- if observation is None:
- logging.warning("Skipping vanished asset during scan: %s", path)
+ if observation is None: # observe_asset_specs already logged why
continue
stat_result = observation.stat_result
recovery = recover_missing_content(
diff --git a/app/assets/scanner_admission.py b/app/assets/scanner_admission.py
index 7c059b005..68fda54f3 100644
--- a/app/assets/scanner_admission.py
+++ b/app/assets/scanner_admission.py
@@ -14,8 +14,6 @@ import time
from dataclasses import dataclass
from typing import Final
-from sqlalchemy.orm import Session
-
from app.assets.services.path_utils import compute_loader_path, get_name_and_tags_from_asset_path
PARTIAL_DOWNLOAD_EXTENSIONS = frozenset({
@@ -66,10 +64,11 @@ def _two_stat_admit(paths_with_stats: list[tuple[str, os.stat_result]]) -> tuple
return admitted, watched
-def tick_watch_list(session: Session) -> None:
- from app.assets.scanner import seed_asset_specs, SeedAssetSpec
+def tick_watch_list() -> None:
+ from app.assets.scanner import insert_asset_specs, SeedAssetSpec
remaining: list[_WatchEntry] = []
+ settled: list[SeedAssetSpec] = []
for entry in _WATCH_LIST:
try:
current = os.stat(entry.path)
@@ -88,10 +87,11 @@ def tick_watch_list(session: Session) -> None:
"mime_type": mimetypes.guess_type(entry.path, strict=False)[0],
"job_id": None,
}
- seed_asset_specs(session, [spec])
+ settled.append(spec)
continue
entry.last_stat = current
entry.ticks += 1
if entry.ticks < _WATCH_SCAN_RETRIES:
remaining.append(entry)
+ insert_asset_specs(settled, set())
_WATCH_LIST[:] = remaining
diff --git a/app/assets/scanner_changes.py b/app/assets/scanner_changes.py
index b7f344a8e..5cfee7d83 100644
--- a/app/assets/scanner_changes.py
+++ b/app/assets/scanner_changes.py
@@ -161,10 +161,14 @@ def drain_pending_verifications(session: Session, limit: int | None = None) -> i
queued_count = min(len(_pending_verification_ids), limit or len(_pending_verification_ids))
processed = 0
for _ in range(queued_count):
+ # Commit the previous entry's writes so this entry's hash runs with no transaction open.
+ # That needs a create_session() session: on a write session the next read takes the lock.
+ session.commit()
content_id = _pending_verification_ids.pop(0)
content = session.get(AssetContent, content_id)
if content is None or content.is_missing:
continue
+ loaded = (content.hash, content.size_bytes, content.mtime_ns)
try:
os.stat(content.path, follow_symlinks=True)
except FileNotFoundError:
@@ -185,6 +189,10 @@ def drain_pending_verifications(session: Session, limit: int | None = None) -> i
continue
digest, verified_stat = snapshot
stored_hash = to_stored_hash(digest)
+ # Skip a row another writer retired or changed while the file was hashed.
+ content = session.get(AssetContent, content_id, populate_existing=True)
+ if content is None or content.is_missing or (content.hash, content.size_bytes, content.mtime_ns) != loaded:
+ continue
if content.hash == stored_hash or content.hash is None:
content.hash = stored_hash
diff --git a/app/assets/seeder.py b/app/assets/seeder.py
index a78f1abf4..c66ea7502 100644
--- a/app/assets/seeder.py
+++ b/app/assets/seeder.py
@@ -873,9 +873,7 @@ class _AssetSeeder:
last_progress_time = now
self._update_progress(scanned=len(specs), created=total_created)
- with create_session() as session:
- tick_watch_list(session)
- session.commit()
+ tick_watch_list()
logging.info(
"Fast scan complete: %.3fs total (created=%d, skipped=%d, total_paths=%d)",
time.perf_counter() - t_fast_start,
@@ -895,7 +893,8 @@ class _AssetSeeder:
scan_state = self._scan_state
with create_session() as session:
drain_pending_verifications(session)
- tick_watch_list(session)
+ session.commit()
+ tick_watch_list()
for _ in range(3):
drain_transition_queue(session)
session.commit()
diff --git a/app/assets/services/hash_mode_state.py b/app/assets/services/hash_mode_state.py
index 0e802a951..ff7f77179 100644
--- a/app/assets/services/hash_mode_state.py
+++ b/app/assets/services/hash_mode_state.py
@@ -120,6 +120,9 @@ def drain_transition_queue(session: Session) -> None:
pending_count = len(_PENDING_QUEUE)
for _ in range(pending_count):
+ # Commit the previous entry's writes so this entry's hash runs with no transaction open.
+ # That needs a create_session() session: on a write session the next read takes the lock.
+ session.commit()
entry = _PENDING_QUEUE.popleft()
_PENDING_PATHS.discard(entry.path)
path = entry.path
diff --git a/app/assets/services/ingest.py b/app/assets/services/ingest.py
index eaeea7285..8813573a4 100644
--- a/app/assets/services/ingest.py
+++ b/app/assets/services/ingest.py
@@ -200,6 +200,7 @@ def _create_upload_record(
mime_type: str | None,
user_metadata: UserMetadata,
preview_id: str | None,
+ system_metadata: dict[str, Any],
) -> Asset:
if preview_id is not None and session.get(Asset, preview_id) is None:
raise ValueError(f"preview_id {preview_id!r} does not reference an existing asset")
@@ -210,7 +211,7 @@ def _create_upload_record(
mime_type=mime_type,
loader_path=compute_loader_path(abs_path),
tags=list(tags),
- system_metadata=_extract_system_metadata_sync(abs_path, mime_type),
+ system_metadata=system_metadata,
)
if user_metadata:
record.user_metadata = dict(user_metadata)
@@ -408,6 +409,8 @@ def _reuse_qualified_content(
if content is None:
return None
content_id = content.id
+ # Read the file before the claim below opens the write transaction.
+ system_metadata = _extract_system_metadata_sync(content.path, spec.mime_type)
if not claim_qualified_content(session, content_id, stored_hash):
session.rollback()
return None
@@ -424,6 +427,7 @@ def _reuse_qualified_content(
spec.mime_type,
spec.user_metadata,
spec.preview_id,
+ system_metadata,
)
session.commit()
return _record_to_upload_result(session, record, created_new=True)
@@ -482,6 +486,7 @@ def upload_from_temp_path(
)
_move_temp_to_dest(temp_path, dest_abs)
size_bytes, mtime_ns = verified_stat.st_size, verified_stat.st_mtime_ns
+ system_metadata = _extract_system_metadata_sync(dest_abs, content_type)
with create_session() as session:
_reconcile_live_content_at_path(
session,
@@ -503,6 +508,7 @@ def upload_from_temp_path(
content_type,
user_metadata,
preview_id,
+ system_metadata,
)
session.commit()
except Exception:
@@ -560,6 +566,7 @@ def register_file_in_place(
digest, verified_stat = _snapshot_hash_with_retry(locator)
size_bytes, mtime_ns = verified_stat.st_size, verified_stat.st_mtime_ns
stored_hash = to_stored_hash(digest)
+ system_metadata = _extract_system_metadata_sync(locator, content_type)
with create_session() as session:
_reconcile_live_content_at_path(
session,
@@ -584,6 +591,7 @@ def register_file_in_place(
content_type,
None,
None,
+ system_metadata,
)
session.commit()
except Exception:
@@ -617,6 +625,8 @@ def create_from_hash(
logging.warning("create_from_hash: no asset found for hash %s", hash_str)
return None
content_id = content.id
+ # Read the file before the claim below opens the write transaction.
+ system_metadata = _extract_system_metadata_sync(content.path, mime_type)
if not claim_qualified_content(session, content_id, stored_hash):
session.rollback()
return None
@@ -633,6 +643,7 @@ def create_from_hash(
mime_type,
user_metadata,
preview_id,
+ system_metadata,
)
session.commit()
return _record_to_upload_result(session, record, created_new=True)
diff --git a/app/database/db.py b/app/database/db.py
index 1fcff1a76..96d5ee7c2 100644
--- a/app/database/db.py
+++ b/app/database/db.py
@@ -304,11 +304,17 @@ def _migrate_and_bind(db_url, db_path, db_exists):
command.upgrade(config, target_rev)
logging.info(f"Database upgraded from {current_rev} to {target_rev}")
except Exception as e:
+ logging.exception("Error upgrading database: ")
if backup_path:
# Restore the database from backup if upgrade fails
- _backup_database(backup_path, db_path)
- os.remove(backup_path)
- logging.exception("Error upgrading database: ")
+ try:
+ _backup_database(backup_path, db_path)
+ os.remove(backup_path)
+ except Exception:
+ logging.exception(
+ f"Restoring the database from its pre-upgrade backup, or removing the "
+ f"backup afterwards, failed; the pre-upgrade copy is kept at {backup_path}"
+ )
raise e
if backup_path and _upgrade_discards_the_catalog(script, target_rev, current_rev):
diff --git a/tests-unit/app_test/test_db_init_locking.py b/tests-unit/app_test/test_db_init_locking.py
index b9cfa8c42..1015ffaba 100644
--- a/tests-unit/app_test/test_db_init_locking.py
+++ b/tests-unit/app_test/test_db_init_locking.py
@@ -153,3 +153,25 @@ def test_setup_database_exits_for_driver_lock_when_assets_are_disabled(monkeypat
assert error.value.code == 1
assert "Database is locked. Another ComfyUI process is already using this database." in caplog.text
+
+
+def test_failed_restore_does_not_mask_the_upgrade_error(stale_db, monkeypatch, caplog):
+ real_backup = db_module._backup_database
+
+ def _upgrade_explodes(*_args, **_kwargs):
+ raise RuntimeError("upgrade exploded")
+
+ def _restore_explodes(source_path, destination_path):
+ if destination_path == stale_db:
+ raise OSError("restore exploded")
+ real_backup(source_path, destination_path)
+
+ monkeypatch.setattr(db_module.command, "upgrade", _upgrade_explodes)
+ monkeypatch.setattr(db_module, "_backup_database", _restore_explodes)
+
+ with caplog.at_level(logging.ERROR), pytest.raises(RuntimeError, match="upgrade exploded"):
+ db_module._init_file_db(db_module.args.database_url)
+
+ backup_path = stale_db + ".bkp"
+ assert os.path.exists(backup_path)
+ assert any(backup_path in record.getMessage() for record in caplog.records)
diff --git a/tests-unit/assets_test/services/test_admission_gate.py b/tests-unit/assets_test/services/test_admission_gate.py
index a34ae9c59..6bd82f15d 100644
--- a/tests-unit/assets_test/services/test_admission_gate.py
+++ b/tests-unit/assets_test/services/test_admission_gate.py
@@ -88,7 +88,7 @@ def test_never_stabilizes_dropped_after_cap(session, temp_dir: Path):
target_ns = max(path.stat().st_mtime_ns, previous_target_ns) + 1_000_000
os.utime(path, ns=(target_ns, target_ns))
previous_target_ns = target_ns
- tick_watch_list(session)
+ tick_watch_list()
assert _WATCH_LIST == []
assert session.scalars(select(AssetContent)).all() == []
@@ -109,14 +109,14 @@ def test_stable_scan_admission_removes_watch_entry_before_next_tick(session, tem
"app.assets.scanner_admission.get_name_and_tags_from_asset_path",
return_value=("stable.bin", []),
),
- patch("app.assets.scanner.seed_asset_specs") as seed_asset_specs,
+ patch("app.assets.scanner.insert_asset_specs") as insert_asset_specs,
):
- tick_watch_list(session)
+ tick_watch_list()
assert admitted == [str(path)]
assert watched == []
assert entries_after_admission == 0
- seed_asset_specs.assert_not_called()
+ assert insert_asset_specs.call_args.args[0] == []
def test_evicted_path_is_admitted_by_later_stable_scan(temp_dir: Path, monkeypatch):
@@ -168,3 +168,26 @@ def test_nonempty_candidate_batch_still_pays_stability_gap(temp_dir: Path, monke
assert sleeps == [0.1]
assert admitted == [str(path)]
assert watched == []
+
+
+def test_settled_entries_are_seeded_in_one_write_session_batch(temp_dir: Path):
+ settled = [temp_dir / "first.bin", temp_dir / "second.bin"]
+ moving = temp_dir / "moving.bin"
+ for path in (*settled, moving):
+ path.write_bytes(path.name.encode())
+ _WATCH_LIST[:] = [_WatchEntry(str(path), path.stat()) for path in settled]
+ _WATCH_LIST.append(_WatchEntry(str(moving), (temp_dir / "first.bin").stat()))
+ with (
+ patch("app.assets.scanner_admission.compute_loader_path", side_effect=os.path.basename),
+ patch(
+ "app.assets.scanner_admission.get_name_and_tags_from_asset_path",
+ side_effect=lambda path: (os.path.basename(path), []),
+ ),
+ patch("app.assets.scanner.insert_asset_specs") as insert_asset_specs,
+ ):
+ tick_watch_list()
+
+ insert_asset_specs.assert_called_once()
+ specs, _ = insert_asset_specs.call_args.args
+ assert [spec["abs_path"] for spec in specs] == [str(path) for path in settled]
+ assert [entry.path for entry in _WATCH_LIST] == [str(moving)]
diff --git a/tests-unit/assets_test/services/test_detection_gate.py b/tests-unit/assets_test/services/test_detection_gate.py
index 02794e0ce..92367956a 100644
--- a/tests-unit/assets_test/services/test_detection_gate.py
+++ b/tests-unit/assets_test/services/test_detection_gate.py
@@ -4,8 +4,10 @@ from unittest.mock import patch
import pytest
from sqlalchemy import select
+from sqlalchemy.orm import Session
from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries.records import mark_content_missing
from app.assets.helpers import to_stored_hash
from app.assets.scanner import (
clear_pending_verifications,
@@ -233,3 +235,64 @@ def test_observation_is_skipped_when_the_row_changed_before_it_was_applied(
assert len(contents) == 1
assert contents[0].is_missing is False
assert contents[0].size_bytes == len(b"v3 is longer still")
+
+
+def test_drain_commits_each_entry_before_hashing_the_next(session, temp_dir: Path, monkeypatch):
+ gone = temp_dir / "gone.bin"
+ kept = temp_dir / "kept.bin"
+ gone.write_bytes(b"gone")
+ kept.write_bytes(b"kept")
+ gone_content, _ = _seed_content(session, gone, None)
+ kept_content, _ = _seed_content(session, kept, None)
+ gone.unlink()
+ queue_pending_verification(gone_content.id)
+ queue_pending_verification(kept_content.id)
+ in_transaction_while_hashing = []
+
+ def recording_snapshot_hash(path: str):
+ in_transaction_while_hashing.append(session.connection().connection.driver_connection.in_transaction)
+ return snapshot_hash(path)
+
+ monkeypatch.setattr("app.assets.scanner_changes.snapshot_hash", recording_snapshot_hash)
+
+ assert drain_pending_verifications(session) == 2
+ assert in_transaction_while_hashing == [False]
+
+
+def _retire_during_hash(monkeypatch, session, content_id: str, replace: bool) -> None:
+ def competing_write_then_hash(path: str):
+ with Session(session.get_bind()) as other:
+ mark_content_missing(other, content_id)
+ if replace:
+ other.add(AssetContent(path=path, hash="blake3:" + "f" * 64, size_bytes=1, mtime_ns=1))
+ other.commit()
+ return snapshot_hash(path)
+
+ monkeypatch.setattr("app.assets.scanner_changes.snapshot_hash", competing_write_then_hash)
+
+
+ ("replace", "seeded_hash"),
+ [(False, None), (True, "blake3:" + "0" * 64)],
+ ids=["retired", "replaced"],
+)
+def test_drain_skips_a_row_retired_while_hashing(
+ session, temp_dir: Path, monkeypatch, replace: bool, seeded_hash: str | None
+):
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ path = temp_dir / "raced.bin"
+ path.write_bytes(b"raced bytes")
+ content, _ = _seed_content(session, path, seeded_hash)
+ content_id = content.id
+ queue_pending_verification(content_id)
+ _retire_during_hash(monkeypatch, session, content_id, replace)
+
+ processed = drain_pending_verifications(session)
+ session.commit()
+
+ session.expire_all()
+ assert session.get(AssetContent, content_id).hash == seeded_hash
+ live = session.scalars(select(AssetContent).where(AssetContent.is_missing.is_(False))).all()
+ assert len(live) == (1 if replace else 0)
+ assert len(session.scalars(select(Asset)).all()) == 1
+ assert processed == 0
diff --git a/tests-unit/assets_test/services/test_from_hash.py b/tests-unit/assets_test/services/test_from_hash.py
index 53914a644..453c26c2b 100644
--- a/tests-unit/assets_test/services/test_from_hash.py
+++ b/tests-unit/assets_test/services/test_from_hash.py
@@ -1,7 +1,9 @@
+from contextlib import contextmanager
from unittest.mock import patch
from sqlalchemy import select
+import app.assets.services.ingest as ingest_module
from app.assets.database.models import Asset, AssetContent
from app.assets.database.queries.records import create_content, mark_content_missing
from app.assets.helpers import to_stored_hash
@@ -116,3 +118,38 @@ def test_file_vanishing_between_claim_and_refresh_mints_nothing(
with mock_create_session() as session:
assert list(session.scalars(select(Asset))) == []
assert session.get(AssetContent, content_id) is not None
+
+
+def test_create_from_hash_reads_the_file_before_the_claim_transaction(
+ mock_create_session, monkeypatch, temp_dir
+):
+ digest = "e" * 64
+ path = temp_dir / "claimed.bin"
+ path.write_bytes(b"claimed bytes")
+ monkeypatch.setattr("app.assets.mode.hashing_enabled", lambda: True)
+ _seed_live_content(mock_create_session, path, digest)
+ sessions = []
+
+ @contextmanager
+ def recording_create_session():
+ with mock_create_session() as session:
+ sessions.append(session)
+ yield session
+
+ in_transaction_while_reading = []
+ real_extract = ingest_module._extract_system_metadata_sync
+
+ def recording_extract(*args, **kwargs):
+ in_transaction_while_reading.append(
+ sessions[-1].connection().connection.driver_connection.in_transaction
+ )
+ return real_extract(*args, **kwargs)
+
+ monkeypatch.setattr(ingest_module, "create_session", recording_create_session)
+ monkeypatch.setattr(ingest_module, "_extract_system_metadata_sync", recording_extract)
+
+ result = create_from_hash(digest, "derived.bin")
+
+ assert result is not None
+ assert result.ref.system_metadata["content_length"] == len(b"claimed bytes")
+ assert in_transaction_while_reading == [False]
diff --git a/tests-unit/assets_test/services/test_scanner_seed_resilience.py b/tests-unit/assets_test/services/test_scanner_seed_resilience.py
index 9dc72e82e..aafa019bb 100644
--- a/tests-unit/assets_test/services/test_scanner_seed_resilience.py
+++ b/tests-unit/assets_test/services/test_scanner_seed_resilience.py
@@ -82,7 +82,7 @@ def _delete_during_recovery(monkeypatch: pytest.MonkeyPatch, path: Path) -> None
def _hash_or_raise(candidate_path: str) -> str | None:
if candidate_path == str(path):
path.unlink()
- raise OSError("file vanished during recovery")
+ raise FileNotFoundError("file vanished during recovery")
return snapshot_hash(candidate_path)
monkeypatch.setattr("app.assets.scanner.snapshot_hash", _hash_or_raise)
@@ -115,6 +115,33 @@ def test_seed_logs_once_for_each_vanished_path(
assert messages == [f"Skipping vanished asset during scan: {vanished_path}"]
+def test_seed_logs_the_real_error_for_an_unreadable_path(
+ session: Session,
+ temp_dir: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ caplog: pytest.LogCaptureFixture,
+) -> None:
+ specs, unreadable_path = _specs_with_vanished_path(temp_dir)
+
+ def _hash_or_raise(path: str) -> str | None:
+ if path == str(unreadable_path):
+ raise PermissionError("permission denied")
+ return snapshot_hash(path)
+
+ monkeypatch.setattr("app.assets.scanner.snapshot_hash", _hash_or_raise)
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ created = seed_asset_specs(session, specs)
+ session.commit()
+
+ assert created == 2
+ messages = [record.getMessage() for record in caplog.records]
+ assert "Asset scan error: phase=seed_observation error_type=permission_denied" in messages
+ assert not any(
+ "vanished" in message and str(unreadable_path) in message for message in messages
+ ), "an unreadable file still exists and must not also be reported as vanished"
+
+
def test_seed_isolates_a_poisoned_spec_and_persists_the_specs_around_it(
session: Session, temp_dir: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
diff --git a/tests-unit/assets_test/services/test_transition_drain.py b/tests-unit/assets_test/services/test_transition_drain.py
index 3c1213499..b6c9ff4dd 100644
--- a/tests-unit/assets_test/services/test_transition_drain.py
+++ b/tests-unit/assets_test/services/test_transition_drain.py
@@ -419,3 +419,31 @@ def test_transition_drain_retires_only_the_unreadable_path_and_hashes_the_health
assert session.get(AssetContent, unreadable_id).hash is None
assert hash_mode_state.pending_transition_count() == 0
assert read_stored_mode(session) == "on"
+
+
+def test_drain_commits_each_entry_before_hashing_the_next(session, temp_dir, monkeypatch):
+ gone = temp_dir / "gone.bin"
+ kept = temp_dir / "kept.bin"
+ for path in (gone, kept):
+ path.write_bytes(path.name.encode())
+ create_content(session, str(path), size_bytes=path.stat().st_size, mtime_ns=path.stat().st_mtime_ns)
+ session.commit()
+ gone.unlink()
+ for path in (gone, kept):
+ hash_mode_state._PENDING_QUEUE.append(hash_mode_state._PendingEntry(str(path)))
+ hash_mode_state._PENDING_PATHS.add(str(path))
+ in_transaction_while_hashing = []
+
+ def recording_snapshot_hash(path: str):
+ in_transaction_while_hashing.append(session.connection().connection.driver_connection.in_transaction)
+ return snapshot_hash(path)
+
+ monkeypatch.setattr(hash_mode_state, "snapshot_hash", recording_snapshot_hash)
+
+ drain_transition_queue(session)
+ session.commit()
+
+ assert in_transaction_while_hashing == [False, False]
+ live = {content.path: content for content in session.scalars(select(AssetContent)) if not content.is_missing}
+ assert list(live) == [str(kept)]
+ assert live[str(kept)].hash == _stored_hash(kept)
diff --git a/tests-unit/assets_test/services/test_upload_b.py b/tests-unit/assets_test/services/test_upload_b.py
index 3e3cdc055..2f8d916c9 100644
--- a/tests-unit/assets_test/services/test_upload_b.py
+++ b/tests-unit/assets_test/services/test_upload_b.py
@@ -1400,3 +1400,47 @@ def test_multipart_upload_persists_the_stat_hashing_verified(
"the digest, not the pair, feeds hash-mode destination naming"
)
assert lookup_for_view(session, content.hash) is not None
+
+
+def test_reupload_reads_the_reused_file_before_the_claim_transaction(
+ mock_create_session, hashing_on, monkeypatch
+):
+ payload = b"claim-then-read-upload-bytes"
+ temp1 = _write_temp(payload)
+ temp2 = _write_temp(payload)
+ sessions = []
+
+ @contextmanager
+ def recording_create_session():
+ with mock_create_session() as session:
+ sessions.append(session)
+ yield session
+
+ in_transaction_while_reading = []
+ real_extract = ingest_module._extract_system_metadata_sync
+
+ def recording_extract(*args, **kwargs):
+ in_transaction_while_reading.append(
+ sessions[-1].connection().connection.driver_connection.in_transaction
+ )
+ return real_extract(*args, **kwargs)
+
+ stored_paths = []
+ try:
+ first = upload_from_temp_path(temp_path=temp1, name="claim.bin", tags=["output"], client_filename="claim.bin")
+ stored_paths.append(first.ref.file_path)
+ monkeypatch.setattr(ingest_module, "create_session", recording_create_session)
+ monkeypatch.setattr(ingest_module, "_extract_system_metadata_sync", recording_extract)
+
+ second = upload_from_temp_path(
+ temp_path=temp2, name="claim.bin", tags=["output"], client_filename="claim.bin"
+ )
+
+ assert second.ref.system_metadata["content_length"] == len(payload)
+ assert in_transaction_while_reading == [False]
+ with mock_create_session() as session:
+ assert session.scalar(select(func.count()).select_from(AssetContent)) == 1
+ finally:
+ for path in (temp1, temp2, *stored_paths):
+ if path and os.path.exists(path):
+ os.unlink(path)
diff --git a/tests-unit/seeder_test/test_seeder.py b/tests-unit/seeder_test/test_seeder.py
index 2637c74d9..e18a224ee 100644
--- a/tests-unit/seeder_test/test_seeder.py
+++ b/tests-unit/seeder_test/test_seeder.py
@@ -155,7 +155,7 @@ def test_enrich_phase_does_not_count_returned_ids_as_failures(
)
monkeypatch.setattr(seeder_module, "create_session", lambda: nullcontext(session))
monkeypatch.setattr(seeder_module, "drain_pending_verifications", lambda _session: None)
- monkeypatch.setattr(seeder_module, "tick_watch_list", lambda _session: None)
+ monkeypatch.setattr(seeder_module, "tick_watch_list", lambda: None)
monkeypatch.setattr(seeder_module, "drain_transition_queue", lambda _session: None)
monkeypatch.setattr(
seeder_module,
@@ -447,7 +447,7 @@ def test_batch_insert_failure_emits_only_the_exception_type(
monkeypatch.setattr(seeder_module, "insert_asset_specs", fail_insert)
monkeypatch.setattr(seeder_module, "create_session", lambda: nullcontext(session))
- monkeypatch.setattr(seeder_module, "tick_watch_list", lambda current_session: None)
+ monkeypatch.setattr(seeder_module, "tick_watch_list", lambda: None)
with caplog.at_level(logging.INFO):
scan_seeder._run_fast_phase(("models",))
@@ -457,3 +457,23 @@ def test_batch_insert_failure_emits_only_the_exception_type(
]
tagged = "\n".join(record.getMessage() for record in caplog.records if TAG in record.getMessage())
assert "/private/models/asset.safetensors" not in tagged
+
+
+def test_enrich_phase_commits_pending_verifications_before_ticking_watch_list(
+ scan_seeder: _AssetSeeder,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ calls: list[str] = []
+ session = Mock()
+ session.commit.side_effect = lambda: calls.append("commit")
+ monkeypatch.setattr(seeder_module, "create_session", lambda: nullcontext(session))
+ monkeypatch.setattr(
+ seeder_module, "drain_pending_verifications", lambda _session: calls.append("drain_pending")
+ )
+ monkeypatch.setattr(seeder_module, "tick_watch_list", lambda: calls.append("tick_watch_list"))
+ monkeypatch.setattr(seeder_module, "drain_transition_queue", lambda _session: None)
+ monkeypatch.setattr(scan_seeder, "_check_pause_and_cancel", lambda _stage: True)
+
+ scan_seeder._run_enrich_phase(("models",))
+
+ assert calls[:3] == ["drain_pending", "commit", "tick_watch_list"]