openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

[Reliability/Security] Many outbound requests calls have no timeout=, blocking workers indefinitely

Open ragini-pandey opened this issue 6 months ago • 6 comments

Description

Problem

A grep for requests.(get|post|put|patch|delete)(...) inside openlibrary/ returns ~25 callsites. Most pass no timeout=, meaning a slow upstream (IA, Wikidata, covers, Solr, etc.) will hang the calling worker until the TCP socket is reset by the OS — typically minutes.

A representative (non-exhaustive) list of unguarded calls:

File Line Call
openlibrary/plugins/upstream/account.py 604 requests.get(url).json()
openlibrary/plugins/upstream/data.py 16 requests.get(url).json()
openlibrary/plugins/upstream/covers.py 124 requests.post(upload_url, …)
openlibrary/plugins/upstream/utils.py 1429 ET.fromstring(requests.get(url).text)
[openlibrary/plugins/openlibrary/code.py](openlibrary/p [openlibrary/plugins/openlibrary 4 [openlibrsts.g
openlibrary/plugins/importapi/import_ui.py 203 requests.get(url, …, stream=True)
openlibrary/coverstore/code.py 310, 525 requests.get(url).json()
openlibrary/views/showmarc.py 27, 39, 125 requests.get(url, …)
openlibrary/solr/data_provider.py 348 requests.get(...).json()
openlibrary/core/wikidata.py 199 `requests.get(f"{WIKIDATA_API_
openlibrary/catalog/get_ia.py 21 requests.get(url, …)
openlibrary/plugins/admin/code.py 221 requests.get(s).text
[openli [openli [openli
openlibrary/plugins/ol_infobase.py 356 requests.get(url, params=json_data)

A handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handedA handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handful of callsites do set a timeout (e.g. [openlibrary/plugins/recy A handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handful of callsites dos A handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handful of callsites do set a t

handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handful of callsites do set a timeout (e.g. [openlibrary/pluginssib handful of callsites do set a timeout (e.g. [openlibrary/plugins/recaptchaA handful of callsites do set a timeout (e.g. [openve to the helper.

  • Add a Ruff/Bandit rule (S113/B113) to the CI config to prevent regressions.

Related files:

  • openlibrary/utils/http.py (new helper to create)
  • openlibrary/plugins/upstream/account.py
  • openlibrary/plugins/upstream/data.py
  • openlibrary/plugins/upstream/covers.py
  • openlibrary/plugins/upstream/utils.py
  • openlibrary/plugins/openlibrary/code.py
  • openlibrary/plugins/importapi/import_ui.py
  • openlibrary/coverstore/code.py
  • openlibrary/views/showmarc.py
  • openlibrary/solr/data_provider.py
  • openlibrary/core/wikidata.py
  • openlibrary/core/lending.py
  • openlibrary/accounts/model.py
  • openlibrary/catalog/utils/query.py
  • openlibrary/catalog/get_ia.py
  • openlibrary/plugins/admin/code.py
  • openlibrary/plugins/admin/services.py
  • openlibrary/plugins/ol_infobase.py
  • pyproject.toml (lint rule addition)

Requirements checklist:

  • [ ] Create openlibrary/utils/http.py with a shared requests.Session wrapper and a default timeout=10
  • [ ] Migrate each callsite in the table above to the new helper
  • [ ] Add a Bandit/Ruff S113 rule to pyproject.toml so unguarded calls fail CI
  • [ ] Document the helper in docs/ai/
  • [ ] No requests.<verb>( call in openl- [ ] No requests.(call inopment

ragini-pandey avatar May 10 '26 14:05 ragini-pandey

Thank you @ragini-pandey for submitting this issue!

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

  • [x] Triage — maintainers will add Priority: * and Lead: @* to replace Needs: Triage and Needs: Lead
  • [x] Needs: Staff Decision — two design questions must be resolved before a contributor can proceed (see below)

Context

  • openlibrary/utils/http.py does not exist yet. The openlibrary/utils/ directory already contains utility modules (retry.py, request_context.py, async_utils.py) — the proposed path is consistent with established patterns.
  • openlibrary/core/ia.py:39 already uses requests.Session() with timeout=3 for IA calls, and openlibrary/plugins/recaptcha/recaptcha.py:45 uses timeout=3. These are the only established patterns; per-service values vary rather than following a single constant.
  • PRs #12382 (BWB timeout, merged) and #11692 (coverstore timeout, merged) addressed individual callsites piecemeal. The remaining ~30 callsites listed in the issue are still unguarded.
  • The flake8-bandit "S" rule category is explicitly commented out in pyproject.toml:142. Enabling S113 will require adding it to the select list as a targeted rule, not simply uncommenting "S" (which would introduce the entire Bandit ruleset and likely require widespread # noqa suppression).

Issue Assessment

Open questions

  1. The proposal uses a single default of timeout=10. Callsites span internal services (IA, Solr — sub-second expected latency) and external services (Wikidata, external covers — variable). Should timeout values be configurable per upstream rather than applying a single constant? A too-generous default for internal calls may mask performance regressions; a too-strict default for external calls may introduce new failures.
  2. Should the helper use requests.Session() (consistent with the existing pattern in ia.py, and beneficial for connection pooling) or a simpler wrapper that just injects a default timeout into bare requests.* calls?
  3. openlibrary/plugins/importapi/import_ui.py:203 uses stream=True. In streaming calls, timeout governs connection establishment but not total read duration. Does the helper need a separate interface or documentation for this case?

Action items

  • [ ] Resolve the per-service vs. uniform timeout design question before implementation begins
  • [ ] Confirm the Session-based vs. standalone wrapper design
  • [ ] Clarify expected handling of the stream=True callsite in import_ui.py:203

Full triage checklist (maintainers / Pam)
  • [ ] Triage — correct labels applied or removed: Needs: Breakdown, Needs: Investigation, Needs: Design, Needs: Staff Decision, Needs: Staff / Admin, Good First Issue, Blocked, Blocker
  • [x] Problem / opportunity — problem is clear, audience identified, actionable
  • [x] Scope — issue is well-scoped; does not try to do too many things; split suggested if needed
  • [x] Justification — reasoning and impact stated; sufficient for leads to prioritize (prior hotfix PRs #11692 and #12382 confirm real worker saturation impact)
  • [ ] Success & testing criteria — the CI lint rule provides automated verification, but no runtime test coverage for timeout behavior is specified
  • [x] Proposal & breakdown — concrete plan present; checklist and related files provided
  • [ ] If bug: environment & repro steps — not applicable (latent reliability issue, not a reproducible crash)
  • [x] Risks, concerns, open questions — identified above; no security/privacy implications requiring Blocker
  • [x] Pam context — relevant code, commits, PRs reviewed; no duplicate issue found
  • [ ] References — no designs needed; issue references relevant files inline

[!NOTE] This comment was automatically generated by Pam, Open Library's Project AI Manager, on behalf of @mekarpeles. Pam is designed to provide status visibility, perform basic project management functions and relevant codebase research, and provide actionable feedback so contributors aren't left waiting.

mekarpeles avatar May 10 '26 14:05 mekarpeles

@RayBB if we switched to httpx, would this auto enforce a timeout? Otherwise, maybe it makes sense to add a few defaults here of e.g. ~5 second (for normal requests that should resolve ~instantly -- e.g. not data dumps)

Ray can help inform which timeouts should be used for which endpoints.

mekarpeles avatar May 11 '26 19:05 mekarpeles

httpx has a default timeout of 5 seconds.

This issue seems very poorly generated by AI as all the links are broken and there are various spots with jumbled text so it's not easy to trust or check the examples given. I'm not inclined to think it's worth the effort to do this for any internal (ol) calls. We are actively moving all external calls to be async which will have timeouts anyway.

So unless the team sees any of these as actively causing a problem I wouldn't prioritize this. I know there are challenges with our dependency on IA and maybe this could help for that specific call but more data is needed because from my experience as a user it seems it would often be better to let something like for 7 seconds than require a retry.

RayBB avatar May 11 '26 20:05 RayBB

Hi! I'd like to work on this issue.

My understanding of the problem: There are ~25+ outbound requests calls across the codebase that lack a timeout= parameter. This means if an upstream service hangs, the worker process blocks indefinitely — potentially taking down the entire app.

My proposed approach:

  1. Create openlibrary/utils/http.py with a shared default timeout constant (e.g. OL_REQUEST_TIMEOUT = 10)
  2. Update all identified call sites to pass timeout=OL_REQUEST_TIMEOUT
  3. Enable Ruff rule S113 in pyproject.toml to prevent future unguarded calls

Files I plan to modify:

  • openlibrary/utils/http.py (new file)
  • openlibrary/core/wikidata.py
  • openlibrary/core/lending.py
  • openlibrary/core/admin.py
  • openlibrary/plugins/upstream/covers.py, data.py, account.py, utils.py
  • openlibrary/plugins/openlibrary/code.py, pd.py
  • openlibrary/coverstore/code.py, utils.py
  • openlibrary/solr/data_provider.py, updater/edition.py
  • openlibrary/accounts/model.py
  • openlibrary/catalog/get_ia.py, utils/query.py, add_book/__init__.py
  • openlibrary/plugins/admin/services.py, code.py
  • openlibrary/plugins/ol_infobase.py
  • openlibrary/plugins/importapi/import_ui.py
  • pyproject.toml (enable S113)

I have the local dev environment set up. Happy to start right away!

joannajiang711 avatar Jun 08 '26 21:06 joannajiang711

I saw @RayBB's concern on the issue. Should I close this PR, or is there a subset of callsites (e.g. external IA calls) worth addressing?

joannajiang711 avatar Jun 08 '26 21:06 joannajiang711

This issue in particular isn't something we're going to work on, as we haven't heard back from anyone pointing to real issues caused by this. So I’m going to go ahead and close it.

In the future, please follow our contributing guidelines, which ask that you request to be assigned to an issue before opening a pull request.

Thanks!

RayBB avatar Jun 08 '26 21:06 RayBB