Skip to content

Look up archive.org metadata off the event loop in /api/volumes - #14585

Merged
RayBB merged 1 commit into
masterfrom
fix/api-volumes-nonblocking-ia-metadata
Nov 19, 2026
Merged

RayBB merged 1 commit into
masterfrom
fix/api-volumes-nonblocking-ia-metadata

Conversation

@RayBB

@RayBB RayBB commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

No issue: found from production monitoring.

Fix: /api/volumes/... no longer freezes the FastAPI process while it looks up archive.org metadata.

The problem

ReadProcessor.process (behind /api/volumes/brief|full/...) is async, but it called ia.get_metadata (sync memcache plus a sync archive.org request with a 3 s timeout) once per scan of each requested work, up to 500, directly on the event loop. While those calls run, nothing else in the process does, /health included, so a work with many uncached scans can stall the whole process for seconds.

Technical

  • ReadProcessor.process now runs the same {iaid: ia.get_metadata(iaid) for iaid in iaids} lookup with await run_in_threadpool(...), the pattern already used in openlibrary/fastapi/ for blocking calls.
  • Because it still calls ia.get_metadata, caching (memcache keys, the 5-minute background refresh) stays shared with web.py and the output is unchanged. run_in_threadpool copies context vars, so the archive.org and memcache timings still show up in the request's Server-Timing.
  • The lookups still run one after another in one worker thread, so an uncached big work takes as long as before; it just no longer blocks other requests. Fetching them concurrently is possible later, but it would need either more threadpool tokens per request or a second async cache path next to memcache_memoize, which is more code than this fix needs.

Follow-ups (other blocking calls left in ReadProcessor.process, not changed here):

  • Needs a follow-up issue: make the archive.org metadata lookup truly async (async httpx call plus an async memcache client) instead of running it in a worker thread. That would also let the lookups for a work run concurrently.
  • lookup_iaids calls site.things({"ocaid": [...]}) in chunks of 10, one after another, on the event loop.
  • dynlinks.ol_get_many_as_dict (site.get_many) for those editions.

Testing

  • New test in openlibrary/plugins/books/tests/test_readlinks.py: a mocked ia.get_metadata waits for a coroutine on the event loop to run. It passes with this change and fails on master (the call holds the loop, so the coroutine never runs).
  • pytest openlibrary/plugins/books/tests/ openlibrary/tests/core/test_ia.py openlibrary/tests/core/test_cache.py openlibrary/tests/fastapi/test_books.py: 89 passed. pre-commit passes on the changed files.
  • testing.openlibrary.org, this commit deployed:
    • An uncached War and Peace (/api/volumes/brief/isbn/0140447938.json, 43 archive.org lookups) took 44 s on testing's slow link to archive.org. /health was polled 67 times meanwhile and never took more than 0.24 s. The request's Server-Timing shows the archive.org time as ia-metadata and no longer under blocking.
    • Against openlibrary.org, for the Odyssey (0140268863), Pride and Prejudice (0141439513), War and Peace, a Dover ISBN and olid/OL22782947M?show_all_items=true: the same records, and the same items except a few single scans. War and Peace on testing is missing exactly the 6 scans whose archive.org lookups failed there; the Odyssey and Pride and Prejudice differ by one or two scans each way, most likely a failed lookup cached as {} on one side (this change still calls the same ia.get_metadata).

Stakeholders

@RayBB

@RayBB RayBB added Module: FastAPI Theme: Performance Issues related to UI or Server performance. [managed] Theme: Public APIs Issues related to APIs accessible to external parties. [managed] labels Nov 19, 2026
@openlibrary-bot
openlibrary-bot requested a review from a team November 19, 2026 01:55

@openlibrary-bot openlibrary-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 finding on 6bbf98f

Prompt for AI agents (1 open item)
Check if each issue is valid — if so, understand the root cause and fix it. If one isn't valid or won't be fixed in this PR, say why in its thread, or in a comment on the PR if it has none.

<file name="openlibrary/core/cache.py">
<item location="184">Stale-refresh fan-out ignores max_concurrency: In `_refresh_async`, `refresh()` calls `asyncio.to_thread(self.memcache.add, ...)` and, on the way out, `asyncio.to_thread(self.memcache.delete, ...)` before/after the `async with semaphore` block, so the semaphore only throttles the `f_async` fetch, not these memcache round trips. `asyncio.gather` over `args_list` therefore submits one `to_thread` call per stale key, all at once, regardless of `max_concurrency`. `readlinks.py` can pass up to ~500 iaids per request (`get_solr_fields_for_works("ia", self.works, 500)`), so a request where many entries go stale together can submit hundreds of blocking calls to the process's shared default `to_thread` executor (no custom executor is configured anywhere in the app), queuing and delaying unrelated `asyncio.to_thread` work across the whole app, not just this feature. Consider moving the `add`/`delete` calls inside the semaphore, or batching them like the hit/miss path already does with `_memcache_get_multi`/`_memcache_set_multi`.</item>
</file>

Please address the feedback above before a maintainer reviews this PR · How this bot works · Reviews new pushes after 10 minutes

Comment thread openlibrary/core/cache.py Resolved
ReadProcessor.process is async, but it called the sync, memcache-memoized
ia.get_metadata once per scanned item of the work, on the event loop. For
works with many scans that froze the whole FastAPI process for seconds.
Run the same lookups in FastAPI's threadpool, so caching and output stay
the same and the loop stays free.
@RayBB
RayBB force-pushed the fix/api-volumes-nonblocking-ia-metadata branch from 6bbf98f to 1b6811d Compare November 19, 2026 02:13
@RayBB RayBB changed the title Fetch archive.org metadata concurrently and off the event loop in /api/volumes Look up archive.org metadata off the event loop in /api/volumes Nov 19, 2026
@openlibrary-bot
openlibrary-bot requested a review from a team November 19, 2026 02:45

@openlibrary-bot openlibrary-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found on 1b6811d.

✅ Earlier findings: 1 addressed.

A maintainer will review this PR next · How this bot works

@RayBB
RayBB merged commit 25f9fc4 into master Nov 19, 2026
20 checks passed
marley pushed a commit to marley/openlibrary that referenced this pull request Nov 19, 2026
…rnetarchive#14585)

ReadProcessor.process is async, but it called the sync, memcache-memoized
ia.get_metadata once per scanned item of the work, on the event loop. For
works with many scans that froze the whole FastAPI process for seconds.
Run the same lookups in FastAPI's threadpool, so caching and output stay
the same and the loop stays free.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Module: FastAPI Theme: Performance Issues related to UI or Server performance. [managed] Theme: Public APIs Issues related to APIs accessible to external parties. [managed]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants