openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Recent feature flag changes have broken `store_counts.py` script

Open jimchamp opened this issue 5 months ago • 3 comments

After today's deployment, our statistics gathering script began failing. This appears to be due to the recent feature flag changes (#12984):

Traceback (most recent call last):
  File "/openlibrary/scripts/store_counts.py", line 16, in <module>
    sys.exit(stats.main(*sys.argv[1:]))
             ~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/openlibrary/openlibrary/admin/stats.py", line 134, in main
    setup_ol_config(openlibrary_config)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/openlibrary/openlibrary/admin/stats.py", line 117, in setup_ol_config
    infogami._setup()
    ~~~~~~~~~~~~~~~^^
  File "/openlibrary/infogami/__init__.py", line 63, in _setup
    delegate._load()
    ~~~~~~~~~~~~~~^^
  File "/openlibrary/infogami/utils/delegate.py", line 203, in _load
    __import__(plugin.module + '.code', globals(), locals(), ['plugins'])
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openlibrary/openlibrary/code.py", line 74, in <module>
    setup()
    ~~~~~^^
  File "/openlibrary/openlibrary/code.py", line 25, in setup
    import openlibrary.plugins.openlibrary.code  # noqa: I001 registers endpoints
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openlibrary/openlibrary/plugins/openlibrary/code.py", line 27, in <module>
    from openlibrary.core.features import features as ol_features
  File "/openlibrary/openlibrary/core/features.py", line 75, in <module>
    features: Features = _load_features()
                         ~~~~~~~~~~~~~~^^
  File "/openlibrary/openlibrary/core/features.py", line 72, in _load_features
    return Features.from_yaml(path)
           ~~~~~~~~~~~~~~~~~~^^^^^^
  File "/openlibrary/openlibrary/core/features.py", line 48, in from_yaml
    data = yaml.safe_load(Path(path).read_text()) or {}
                          ~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.14/pathlib/__init__.py", line 787, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors, newline=newline) as f:
         ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.14/pathlib/__init__.py", line 771, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: 'conf/openlibrary.yml'

Other scripts/functionality may also be broken.

jimchamp avatar Jul 02 '26 22:07 jimchamp

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

  • [ ] Triage — maintainers will add Priority: * and Lead: @* to replace Needs: Triage and Needs: Lead

Context

Root cause traced to openlibrary/core/features.py (added in #12984, commit dac833d73):

def _load_features() -> Features:
    path = os.environ.get("OL_CONFIG", "conf/openlibrary.yml")
    return Features.from_yaml(path)

features: Features = _load_features()  # runs at import time
  • This module-level singleton executes at import time, as a side effect of infogami._setup() loading the openlibrary.plugins.openlibrary plugin (openlibrary/plugins/openlibrary/code.py imports openlibrary.core.features).
  • store_counts.py → openlibrary/admin/stats.py:setup_ol_config() calls infogami.load_config(openlibrary_config_file) with an explicit config path passed as a CLI argument (sys.argv[2]), then calls infogami._setup(). The new features.py module completely ignores that explicit path — it only reads OL_CONFIG from the environment or falls back to the relative path "conf/openlibrary.yml".
  • If the script runs from a cwd other than the repo root, or in an environment where OL_CONFIG isn't exported, Path("conf/openlibrary.yml").read_text() raises exactly the kind of error seen in the traceback. Cron jobs commonly run with a minimal environment that does not inherit a container's docker-compose environment: block — the cron-jobs service (compose.production.yaml:157, docker/ol-cron-start.sh) loads its crontab from the private olsystem repo (/etc/cron.d/openlibrary.ol_home0), which isn't visible from here, so I can't confirm whether OL_CONFIG is set there. Flagging as an open question rather than asserting it.
  • The parent epic (#12946) explicitly settled: "Does the new system need environment-aware overrides? No... The OL_FEATURES_YAML_PATH env var is sufficient." The merged PR used OL_CONFIG instead, and didn't account for CLI-script invocation — grep -rln "_setup()\|delegate._load\|infogami.load_config" shows store_counts.py, oldump.py, solr_updater.py, manage-imports.py, and affiliate_server.py all trigger the same infogami plugin-loading path and are equally exposed in theory.
  • #12984's testing section only covers the web container (stats-header HTTP header, ?debug param) — no test exercised a standalone CLI script, which is how this slipped through.

Issue Assessment

Open questions

  • Does the private olsystem crontab export OL_CONFIG for cron-invoked jobs, or is that the actual gap?
  • Are oldump.py, solr_updater.py, manage-imports.py, affiliate_server.py also affected in production, or only store_counts.py?
  • Should features.py accept an explicit config path (mirroring infogami.load_config()'s argument) instead of only reading env/relative-default, or should the module-level load be made lazy so it runs after the caller has set OL_CONFIG?

Action items

  • [x] Confirm in olsystem whether cron jobs have OL_CONFIG set (Answer: No, they do not.)
  • [ ] Decide on fix direction: pass config path explicitly vs. lazy-load features vs. always require OL_CONFIG to be set process-wide (including for cron)
Full triage checklist (maintainers / Richy)
  • [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
  • [x] Justification — reasoning and impact stated; sufficient for leads to prioritize
  • [ ] Success & testing criteria — not stated in issue; proposed above
  • [ ] Proposal & breakdown — no fix proposal yet; root cause identified above
  • [x] If bug: environment & repro steps — full traceback with commit context provided
  • [x] Risks, concerns, open questions — identified and surfaced above
  • [x] Richy context — relevant code, commits, PRs reviewed; no duplicate issue found
  • [ ] References — no screenshots/designs needed for this bug

[!NOTE] This comment was automatically generated by Richy, via PAM, Open Library's Project AI Manager, on behalf of @mekarpeles. Richy 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.

openlibrary-bot avatar Jul 02 '26 22:07 openlibrary-bot

OL_CONFIG is not set for cron jobs.

jimchamp avatar Jul 02 '26 22:07 jimchamp

update_stale_work_references.py is failing with the same error.

jimchamp avatar Jul 02 '26 23:07 jimchamp