[Reliability/Security] Many outbound requests calls have no timeout=, blocking workers indefinitely
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.pyopenlibrary/plugins/upstream/data.pyopenlibrary/plugins/upstream/covers.pyopenlibrary/plugins/upstream/utils.pyopenlibrary/plugins/openlibrary/code.pyopenlibrary/plugins/importapi/import_ui.pyopenlibrary/coverstore/code.pyopenlibrary/views/showmarc.pyopenlibrary/solr/data_provider.pyopenlibrary/core/wikidata.pyopenlibrary/core/lending.pyopenlibrary/accounts/model.pyopenlibrary/catalog/utils/query.pyopenlibrary/catalog/get_ia.pyopenlibrary/plugins/admin/code.pyopenlibrary/plugins/admin/services.pyopenlibrary/plugins/ol_infobase.pypyproject.toml(lint rule addition)
Requirements checklist:
- [ ] Create
openlibrary/utils/http.pywith a sharedrequests.Sessionwrapper and a defaulttimeout=10 - [ ] Migrate each callsite in the table above to the new helper
- [ ] Add a Bandit/Ruff
S113rule topyproject.tomlso unguarded calls fail CI - [ ] Document the helper in
docs/ai/ - [ ] No
requests.<verb>(call inopenl- [ ] Norequests.( call inopment
Thank you @ragini-pandey for submitting this issue!
⚠️ Contributors, this issue will be ready to work on once:
- [x] Triage — maintainers will add
Priority: *andLead: @*to replaceNeeds: TriageandNeeds: Lead - [x] Needs: Staff Decision — two design questions must be resolved before a contributor can proceed (see below)
Context
openlibrary/utils/http.pydoes not exist yet. Theopenlibrary/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:39already usesrequests.Session()withtimeout=3for IA calls, andopenlibrary/plugins/recaptcha/recaptcha.py:45usestimeout=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 inpyproject.toml:142. EnablingS113will require adding it to theselectlist as a targeted rule, not simply uncommenting"S"(which would introduce the entire Bandit ruleset and likely require widespread# noqasuppression).
Issue Assessment
Open questions
- 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. - Should the helper use
requests.Session()(consistent with the existing pattern inia.py, and beneficial for connection pooling) or a simpler wrapper that just injects a default timeout into barerequests.*calls? openlibrary/plugins/importapi/import_ui.py:203usesstream=True. In streaming calls,timeoutgoverns 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=Truecallsite inimport_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.
@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.
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.
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:
- Create
openlibrary/utils/http.pywith a shared default timeout constant (e.g.OL_REQUEST_TIMEOUT = 10) - Update all identified call sites to pass
timeout=OL_REQUEST_TIMEOUT - Enable Ruff rule
S113inpyproject.tomlto prevent future unguarded calls
Files I plan to modify:
openlibrary/utils/http.py(new file)openlibrary/core/wikidata.pyopenlibrary/core/lending.pyopenlibrary/core/admin.pyopenlibrary/plugins/upstream/covers.py,data.py,account.py,utils.pyopenlibrary/plugins/openlibrary/code.py,pd.pyopenlibrary/coverstore/code.py,utils.pyopenlibrary/solr/data_provider.py,updater/edition.pyopenlibrary/accounts/model.pyopenlibrary/catalog/get_ia.py,utils/query.py,add_book/__init__.pyopenlibrary/plugins/admin/services.py,code.pyopenlibrary/plugins/ol_infobase.pyopenlibrary/plugins/importapi/import_ui.pypyproject.toml(enable S113)
I have the local dev environment set up. Happy to start right away!
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?
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!