Files
romm/backend/handler/database/screenshots_handler.py
Zach Clendenen b1f4ac4888 fix(db): keep iterated query results away from the cyclic GC (#5068)
* fix(roms): read filter values with .all() so the GC never frees the cursor

Looping over the filter-values Result directly makes SQLAlchemy 2.1 memoize
an iterator closure that captures the Result, which puts the CursorResult,
its execution context and the mariadb cursor in a reference cycle. The
cyclic GC then frees them on whatever thread triggers a collection, and
under concurrent gallery requests that segfaults the mariadb connector
(1.1.14, Python 3.14) inside the pool checkout. .all() never builds that
closure, so nothing is left for the GC.

* fix(roms): address pr-ready code review

* style(roms): pr-ready polish

* fix(db): read every iterated query result with .all()

The same reference cycle that segfaulted the filter values also hits smart
collection matching on every save, the screenshot binding check, playlist and
favourite adds, the recommendation reads and the stopword probe. The IGDB
related-games stream keeps yield_per and closes its Result with a with block.
A source scan test now fails on any Result iterated directly.

* fix(roms): address Greptile review

Keep gc.garbage entries that predate the filter-values cursor test: only the
objects this test collects are inspected and removed, so a runner that already
saves garbage keeps its list intact (Greptile, test_rom_facets.py).

* pr-ready: code-review fixes

Keep a generator's begin_session open until it finishes, read the
streamed IGDB scan by partition, and widen the iterated-Result guard to
names, unpacking, yield from and iterating methods.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* pr-ready: simplify

Share the app-source AST scan between the two guard tests, fold the
generator session's two branches into one nullcontext, and drop the
call-site comment the guard test already carries.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(db): flag iterated legacy session.query() results

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 10:06:49 -04:00

201 lines
6.4 KiB
Python

from collections.abc import Collection, Sequence
from typing import Any
import pydash
from sqlalchemy import case, delete, func, or_, select, update
from sqlalchemy.orm import Session
from sqlalchemy.sql import Delete, Select, Update
from decorators.database import INJECTED_SESSION, begin_session
from models.assets import Save, Screenshot, State
from models.base import with_file_name_parts
from .base_handler import DBBaseHandler
class DBScreenshotsHandler(DBBaseHandler):
def filter[QueryT: (Select[Screenshot], Update, Delete)](
self,
query: QueryT,
*,
rom_id: int,
user_id: int,
filenames: Sequence[str] = (),
exclude_filenames: Sequence[str] = (),
) -> QueryT:
query = query.filter(
Screenshot.rom_id == rom_id,
Screenshot.user_id == user_id,
)
if filenames:
query = query.filter(
or_(
Screenshot.file_name.in_(filenames),
Screenshot.file_name_no_ext.in_(filenames),
)
)
if exclude_filenames:
query = query.filter(
Screenshot.file_name.not_in(exclude_filenames),
Screenshot.file_name_no_ext.not_in(exclude_filenames),
)
return query
@begin_session
def add_screenshot(
self,
screenshot: Screenshot,
session: Session = INJECTED_SESSION,
) -> Screenshot:
return session.merge(screenshot)
@begin_session
def get_screenshots(
self,
*,
user_id: int,
rom_ids: Collection[int],
session: Session = INJECTED_SESSION,
) -> Sequence[Screenshot]:
return session.scalars(
select(Screenshot).filter(
Screenshot.user_id == user_id, Screenshot.rom_id.in_(rom_ids)
)
).all()
@begin_session
def get_screenshot(
self,
*,
rom_id: int,
user_id: int,
file_name: str,
file_name_no_ext: str | None = None,
session: Session = INJECTED_SESSION,
) -> Screenshot | None:
query = self.filter(
select(Screenshot),
rom_id=rom_id,
user_id=user_id,
filenames=pydash.compact([file_name, file_name_no_ext]),
)
# Prefer exact stem matches first
query = query.order_by(
case((Screenshot.file_name_no_ext == file_name, 0), else_=1),
Screenshot.id.desc(),
)
return session.scalars(query.limit(1)).first()
@begin_session
def get_screenshot_by_id(
self,
id: int,
session: Session = INJECTED_SESSION,
) -> Screenshot | None:
return session.get(Screenshot, id)
@begin_session
def is_bound(
self,
screenshot: Screenshot,
ignoring: Save | State | None = None,
session: Session = INJECTED_SESSION,
) -> bool:
"""Whether a save or state other than `ignoring` shows the screenshot
as its thumbnail."""
names = pydash.compact([screenshot.file_name, screenshot.file_name_no_ext])
for model in (Save, State):
query = select(model.file_name, model.file_name_no_ext).filter(
model.rom_id == screenshot.rom_id,
model.user_id == screenshot.user_id,
or_(model.file_name.in_(names), model.file_name_no_ext.in_(names)),
)
if isinstance(ignoring, model):
query = query.filter(model.id != ignoring.id)
# A name can match several screenshots, and the asset shows only
# the one its lookup prefers.
for file_name, file_name_no_ext in session.execute(query).all():
shown = self.get_screenshot(
rom_id=screenshot.rom_id,
user_id=screenshot.user_id,
file_name=file_name,
file_name_no_ext=file_name_no_ext,
session=session,
)
if shown is not None and shown.id == screenshot.id:
return True
return False
@begin_session
def get_name_variants(
self,
screenshot: Screenshot,
session: Session = INJECTED_SESSION,
) -> Sequence[Screenshot]:
"""Other screenshots in the same folder whose name differs only in case."""
query = self.filter(
select(Screenshot), rom_id=screenshot.rom_id, user_id=screenshot.user_id
).filter(
Screenshot.id != screenshot.id,
Screenshot.file_path == screenshot.file_path,
func.lower(Screenshot.file_name) == screenshot.file_name.lower(),
)
return session.scalars(query).all()
@begin_session
def get_rom_gallery_screenshots(
self,
rom_id: int,
user_id: int,
public_only: bool = False,
session: Session = INJECTED_SESSION,
) -> Sequence[Screenshot]:
"""Gallery (intentionally-uploaded) screenshots for a ROM, visible to
the requesting user. Mirrors `db_rom_handler.get_rom_notes`: own
screenshots (public + private) plus other users' public ones. Excludes
the auto-captured save/state thumbnails (`is_gallery == False`)."""
query = select(Screenshot).filter(
Screenshot.rom_id == rom_id,
Screenshot.is_gallery,
)
if public_only:
query = query.filter(Screenshot.is_public)
else:
query = query.filter(
or_(Screenshot.user_id == user_id, Screenshot.is_public)
)
query = query.order_by(Screenshot.created_at.desc())
return session.scalars(query).all()
@begin_session
def update_screenshot(
self,
id: int,
data: dict[str, Any],
session: Session = INJECTED_SESSION,
) -> Screenshot:
session.execute(
update(Screenshot)
.where(Screenshot.id == id)
.values(**with_file_name_parts(data))
.execution_options(synchronize_session="evaluate")
)
return session.scalars(select(Screenshot).filter_by(id=id)).one()
@begin_session
def delete_screenshot(
self,
id: int,
session: Session = INJECTED_SESSION,
) -> None:
session.execute(
delete(Screenshot)
.where(Screenshot.id == id)
.execution_options(synchronize_session="evaluate")
)