Repository navigation
Look up archive.org metadata off the event loop in /api/volumes - #14585
Merged
Merged
Conversation
openlibrary-bot
requested changes
Nov 19, 2026
openlibrary-bot
left a comment
Collaborator
There was a problem hiding this comment.
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
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
force-pushed
the
fix/api-volumes-nonblocking-ia-metadata
branch
from
November 19, 2026 02:13
6bbf98f to
1b6811d
Compare
openlibrary-bot
approved these changes
Nov 19, 2026
openlibrary-bot
left a comment
Collaborator
There was a problem hiding this comment.
No issues found on 1b6811d.
✅ Earlier findings: 1 addressed.
A maintainer will review this PR next · How this bot works
5 tasks
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 calledia.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,/healthincluded, so a work with many uncached scans can stall the whole process for seconds.Technical
ReadProcessor.processnow runs the same{iaid: ia.get_metadata(iaid) for iaid in iaids}lookup withawait run_in_threadpool(...), the pattern already used inopenlibrary/fastapi/for blocking calls.ia.get_metadata, caching (memcache keys, the 5-minute background refresh) stays shared with web.py and the output is unchanged.run_in_threadpoolcopies context vars, so the archive.org and memcache timings still show up in the request'sServer-Timing.memcache_memoize, which is more code than this fix needs.Follow-ups (other blocking calls left in
ReadProcessor.process, not changed here):lookup_iaidscallssite.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
openlibrary/plugins/books/tests/test_readlinks.py: a mockedia.get_metadatawaits 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-commitpasses on the changed files./api/volumes/brief/isbn/0140447938.json, 43 archive.org lookups) took 44 s on testing's slow link to archive.org./healthwas polled 67 times meanwhile and never took more than 0.24 s. The request'sServer-Timingshows the archive.org time asia-metadataand no longer underblocking.0140268863), Pride and Prejudice (0141439513), War and Peace, a Dover ISBN andolid/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 sameia.get_metadata).Stakeholders
@RayBB