Skip to content

Fix batched thumbnail cache clearing - #9

Merged
ewlarson merged 1 commit into
developfrom
agent/fix-thumbnail-cache-batch-loop
Aug 13, 2026
Merged

Fix batched thumbnail cache clearing#9
ewlarson merged 1 commit into
developfrom
agent/fix-thumbnail-cache-batch-loop

Conversation

@ewlarson

Copy link
Copy Markdown
Collaborator

Summary

  • Process every resource ID supplied to clear_thumbnail_cache.py inside one async event loop.
  • Preserve the existing single-ID/multi-ID CLI and cleared/total summary.
  • Add regression coverage proving every ID uses the same running loop and the CLI invokes one batch coroutine.

Production incident

A Kamal production run attempted to clear thumbnail caches for 13 UNR resources:

python /app/backend/scripts/clear_thumbnail_cache.py RESOURCE_ID_1 RESOURCE_ID_2 ...

The first resource completed, but the second aborted with:

RuntimeError: Event loop is closed
Task ... got Future ... attached to a different loop

Root cause

main() called asyncio.run(clear_thumbnail_for_resource(rid)) once for every resource. Each call created and closed a new event loop, while the process-global SQLAlchemy async engine/pool retained asyncpg connections bound to the previous loop. Checking out one of those connections for the next resource triggered the cross-loop failure.

Fix

The resource loop now lives in clear_thumbnails_for_resources(), and main() calls asyncio.run() once for the whole batch. Exceptions retain the resource-specific log message and continue to propagate as before.

User impact

Multi-resource cache purge commands now process the full resource list rather than failing after the first ID. Single-resource behavior and CLI syntax are unchanged.

Validation

  • BTAA_SKIP_TEST_DB=1 python -m pytest backend/tests/scripts/test_clear_thumbnail_cache.py -q — 2 passed
  • ruff check passed
  • ruff format --check passed
  • git diff --check passed

Issue tracking

A standalone issue could not be created because GitHub Issues are disabled for ewlarson/ogm-api (GitHub API returned HTTP 410). This PR includes the complete issue write-up so the incident remains documented.

@ewlarson
ewlarson marked this pull request as ready for review August 13, 2026 21:06
@ewlarson
ewlarson merged commit 301f60b into develop Aug 13, 2026
1 check passed
@ewlarson
ewlarson deleted the agent/fix-thumbnail-cache-batch-loop branch August 13, 2026 21:29
ewlarson added a commit that referenced this pull request Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant