3 Commits

Author SHA1 Message Date
Georges-Antoine Assi
c75437cc01 refactor(adapters): send provider requests through httpx2 (#5851)
* refactor(adapters): send provider requests through httpx2

The metadata provider adapters (IGDB, ScreenScraper, MobyGames,
RetroAchievements, Steam, SteamGridDB, libretro thumbnails) used aiohttp
while the rest of the backend used httpx2. They now share the request's
httpx2 client through `send_request` in provider_http.py, which bounds
the whole exchange with one timeout and follows redirects, as aiohttp
did. Each provider's auth middleware becomes an httpx2.Auth flow.

aiohttp was adopted because httpx logged request URLs carrying API
keys at INFO. The log formatter now masks every credential parameter
those URLs carry, so that reason no longer holds.

Behavior changes:
- Provider requests go through the SSRF-guarded client. Only tests
  ever pointed a provider at a custom base URL.
- A dropped connection is no longer resent once by the client; it
  surfaces as the existing 503.
- SteamGridDB's default base URL is www.steamgriddb.com. The bare
  domain answers with a cross-host 301, and httpx2 drops the
  Authorization header on that hop.

aiohttp, its OpenTelemetry instrumentation, and the packages only
aiohttp needed (aiohappyeyeballs, aiosignal, attrs, frozenlist) leave
the lockfile. The scripted test server becomes an httpx2.MockTransport,
and the aiohttp-recorded cassettes are adapted to what httpx2 sends:
masked credentials in the query, empty bodies as "", and no stale gzip
Content-Encoding headers on already-decompressed bodies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(logging): keep provider API keys out of Sentry

The log formatter masks credential query parameters on stdout, but
Sentry's logging integration reads the raw record before any handler
formats it. Two paths sent provider keys to Sentry when SENTRY_DSN is
set:

- httpx2's INFO request line, which carries the authenticated URL, as a
  breadcrumb in the web process.
- An adapter's `log.error(exc)` on an error status, since an
  HTTPStatusError's message carries the same URL, as an event in the
  web and worker processes.

The `romm` logger now masks the record itself in a filter, which runs
before handlers and Sentry's hook. httpx2's request lines are no longer
emitted. Sentry's before_send and before_breadcrumb mask every string in
a payload, which covers exceptions captured directly, including their
messages and stack locals. The masking regex also stops at a single
quote, so httpx2's quoted URLs keep their closing quote.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Set the RomM user agent and convert httpx2 timeouts to TimeoutError in send_request, so adapters drop their copies and send_with_retries catches a single timeout type. Read the ScreenScraper status code once in _handle_client_error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: review-polish

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose (code-review follow-up)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(logging): keep the log template when redacting a record

RedactFilter replaced msg with the formatted text and dropped args, so
Sentry grouped each redacted URL or game id as its own issue. It now
redacts the template and each argument in place, and falls back to the
flat text only when a secret spans the template and an argument.
pprint records keep their structured msg.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(screenscraper): pin dev credentials for the cassette tests

The cassettes now record devid and devpassword, since httpx2's auth flow
runs before VCR sees the request. CI has no developer credentials, so the
requests carried a blank devid= that VCR drops when matching, and all
seven integration tests failed to find their recording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:43:32 -05:00
Claude
103c59e9c0 fix: keep a consumer on the streaming queue when streaming is off
config.yml is re-read on every request, so streaming can be turned on
without a restart, and exit save pulls queued before it was turned off
still need a worker. With streaming off at boot, the general RQ worker
now listens on the streaming queue too instead of leaving it without a
consumer; the dedicated streaming worker still starts only when
streaming is on.

The check reads the exit status inline rather than through a function
in an `if !`, which shellcheck flags (SC2310) since it disables errexit.

Adds a fresh-interpreter test that init_sentry() never imports the SDK
without a DSN, and drops a comment that restated the PR's rationale.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gh1SRMfuK7BZmf6YxMqSyT
2026-10-01 01:40:44 +00:00
Claude
78379718c7 perf: trim idle memory of the backend processes
Skip sentry_sdk.init when no SENTRY_DSN is set. Without a DSN the SDK
reports nothing, yet init still loads every default integration (httpx,
rich, jinja2, mako, pygments...), about 10 MB per process.

Import alembic.config only in main.py's __main__ block, the one place
that uses it, instead of in every gunicorn worker (about 8 MB).

Don't start the RQ streaming worker when config.yml leaves streaming
off (the default). It only runs streaming teardowns and exit save pulls,
and costs about 115 MB idle.

Measured on a gunicorn worker importing main: 213 MB -> 193 MB RSS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gh1SRMfuK7BZmf6YxMqSyT
2026-09-30 23:54:42 +00:00