openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

IA Import failing incorrectly with "identifier not found"

Open cdrini opened this issue 4 months ago • 3 comments

Problem

This specific IA id is failing with a surprising error.

Image

Reproducing the bug

  1. Go to https://openlibrary.org/import/preview?source=ia%253Agraphicdesign0000tapp
  2. Optionally enable the debug parameter on the form
  3. Click "Preview import"
  • Expected behavior: it should show a preview of the import
  • Actual behavior: it errors with BookImportError ('invalid-ia-identifier', 'graphicdesign0000tapp not found')

Context

  • Browser (Chrome, Safari, Firefox, etc):
  • OS (Windows, Mac, etc):
  • Logged in (Y/N):
  • Environment (prod, dev, local): prod

Breakdown

Implementation Details (for maintainers)

Requirements Checklist

  • [ ]

Related files

Stakeholders


Instructions for Contributors

  • Please run these commands to ensure your repository is up to date before creating a new branch to work on this issue and each time after pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.

cdrini avatar Jul 31 '26 14:07 cdrini

Issue Assessment

  • Problem — present and complete: clear repro steps, screenshot, expected vs. actual behavior.
  • Context fields — Browser/OS/Logged-in left blank, but this reproduces server-side regardless of client, so not blocking.
  • Breakdown — Requirements Checklist, Related files, and Stakeholders are all unfilled placeholders (*) — missing entirely, consistent with the existing Needs: Breakdown label.

⚠️ Contributors, this issue will be ready to work on once:

  • [ ] Triage — maintainers will add Priority: * and Lead: @*
  • [ ] Needs: Breakdown — issue lacks concrete implementation steps

Context

The error originates in get_ia_import_record:

metadata = ia.get_metadata(identifier)
if not metadata:
    raise BookImportError("invalid-ia-identifier", f"{identifier} not found")

ia.get_metadata is a 5-minute memcache_memoize wrapper around get_metadata_direct, which calls get_api_response:

try:
    r = session.get(url, params=params, timeout=3)
    if r.status_code == httpx.codes.OK:
        api_response = r.json()
    else:
        logger.info(f"{r.status_code} response received from {url}")
except Exception:
    logger.exception(f"Exception occurred accessing {url}.")
...
return api_response

Any non-200 response, network error, or timeout (fixed at 3 seconds) is swallowed and returns {} — identical to what a genuinely nonexistent identifier returns. That {} is then cached by memcache_memoize for 5 minutes, so a single transient hiccup (or a metadata response that consistently takes >3s, e.g. for an item with a very large file list) gets memoized and reported as invalid-ia-identifier for the full TTL, indistinguishable from a real 404. There's no code path that differentiates "confirmed not found" from "failed to fetch."

ia.get_metadata/get_metadata_direct is shared well beyond the import flow — it's also used in openlibrary/plugins/upstream/models.py, openlibrary/plugins/openlibrary/connection.py, openlibrary/plugins/openlibrary/home.py, openlibrary/plugins/books/readlinks.py, and openlibrary/catalog/get_ia.py — so any fix to the caching behavior touches more than just the import preview page.

Related open issues (not exact duplicates, but same general failure class):

  • #5693 — "Error on OL when IA item removed or invalid"
  • #10756 — "IA import fails due to missing publisher"
  • #12356 — "Import failing to match on amazon ASIN" (already has Lead: @cdrini)

No existing issue/PR specifically addresses the negative-result caching behavior in get_api_response/memcache_memoize.

Note: the checked-out history for this research only has one commit available, so git blame/git log -S couldn't be used to trace prior authorship on these lines.


Open questions

  • [ ] Can a maintainer with Sentry/production log access confirm whether the archive.org/metadata/graphicdesign0000tapp request actually timed out or returned a non-200 at the time of failure (consistent with the hypothesis above), as opposed to some other failure mode?
  • [ ] Is the 3-second timeout in get_api_response (openlibrary/core/ia.py#L40) known to be tight for archive.org metadata responses on items with large file lists, or was it chosen for another reason?

Suggested breakdown

  • [ ] Confirm root cause: reproduce/inspect whether archive.org/metadata/graphicdesign0000tapp is timing out or erroring under the current 3s timeout
  • [ ] In get_metadata_direct/get_api_response (openlibrary/core/ia.py), distinguish a genuine "item not found" from a transient failure (timeout, network exception, non-200 status) so the latter isn't memoized as a permanent negative for 5 minutes
  • [ ] Consider raising the timeout and/or adding a retry before treating a fetch as failed
  • [ ] Add a regression test asserting a transient get_api_response failure isn't cached as an empty/"not-found" result

None of these look like a Good First Issue on their own — ia.get_metadata is a shared, widely-consumed cache, so any change to its failure semantics needs care to avoid regressing the other call sites listed above.


🔔 Stakeholders

@hornc — imports and big data are your listed area, and Module: Import is the entry point for this bug, though the underlying issue lives in the shared openlibrary/core/ia.py caching layer.


Full triage checklist
  • [x] Triage — correct labels applied or removed
  • [x] Problem / opportunity — problem is clear, audience identified, actionable
  • [x] Scope — issue is well-scoped; does not try to do too many things
  • [ ] Justification — reasoning and impact stated; sufficient for leads to prioritize
  • [ ] Success & testing criteria — defines what "done" looks like, verifiable by a contributor
  • [ ] Proposal & breakdown — concrete plan present; checklist actionable for a contributor
  • [x] If bug: environment & repro steps — browser, version, local vs prod; steps to reproduce
  • [x] Risks, concerns, open questions — identified and surfaced
  • [x] Richy context — relevant code, commits, PRs reviewed; no duplicate found
  • [x] References — screenshots, designs, docs, and links present where needed

[!NOTE] This comment was automatically generated by Richy, Open Library's issue enrichment assistant, on behalf of @mekarpeles. This is NOT a sign-off. This issue needs lead review before work begins.

openlibrary-bot avatar Jul 31 '26 15:07 openlibrary-bot

The item isn't actually invalid, the metadata fetch is timing out and that failure is being reported as "not found."

get_api_response in openlibrary/core/ia.py has a hardcoded 3s timeout, and on a timeout (or any non-200/network error) it swallows the exception and returns {}. The problem is that {} is also exactly what archive.org returns for a genuinely missing item- it answers HTTP 200 with an empty body so a transient failure and a real 404 are indistinguishable. That empty result then gets memoized by get_metadata, and get_ia_import_record sees the empty dict and raises invalid-ia-identifier.

I reproduced it against production. graphicdesign0000tapp is obviously a real item but the cold fetch took 4 seconds which blows the 3 second timeout; warm fetches were ~0.3s. So it's not a large/slow item, it's just cold-cache latency on archive.org's side exceeding a very tight timeout.

Couple of notes on the earlier triage:

  • It's not payload size as the metadata response is tiny (12KB), so raising the timeout would just be a bandaid
  • "Cached for 5 minutes" undersells it. memcache_set sets no TTL, the 5 min is just the stale-while-revalidate threshold, so the bad {} can persist longer, and the request that would refresh it still serves the stale value once.

Suggestion: in get_api_response/get_metadata_direct, distinguish a transient failure (timeout/non-200/network error) from a genuine "not found" so the former is never memoized as a permanent negative and give the import path an honest error when archive.org is just unreachable, instead of "invalid identifier." Bumping the 3s timeout wouldn't hurt either.

Happy to open a PR along these lines if a lead thinks this is the right direction.

comaeclipse avatar Aug 03 '26 13:08 comaeclipse

@comaeclipse I think you're on the right track (we don't want to indefinitely cache missed lookups).

cc: @cdrini to triage

mekarpeles avatar Aug 04 '26 03:08 mekarpeles