openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Linux Docker Issues: Fresh clone does not boot: web exits 3 writing _testing-prs.json into a bind mount it cannot write

Open pnelson1337 opened this issue 2 months ago • 9 comments

Problem

A fresh clone doesn't boot. docker compose up -d brings every service up, then web exits with code 3 and nothing serves on :8080.

File "/openlibrary/openlibrary/plugins/openlibrary/status.py", line 695, in _ensure_testing_state_file
    TESTING_STATE_FILE.write_text(json.dumps({"last_deploy_at": "", "prs": []}, indent=2))
PermissionError: [Errno 13] Permission denied: '_testing-prs.json'
[ERROR] Worker (pid:9) exited with code 3
[ERROR] Shutting down: Master

_ensure_testing_state_file() runs at import time via setup() and writes _testing-prs.json into the repo root. The repo root is the bind mount, owned by the host user, while the container runs as openlibrary uid 999 — so the write fails and gunicorn's worker never boots.

_testing-prs.json is gitignored (.gitignore:75), so it is never present in a fresh clone and the if not TESTING_STATE_FILE.exists() guard always fires.

This looks like it contradicts the rule stated in docker/Dockerfile.olbase itself: "We use 999:999 for the openlibrary user. Any volume mounts which require read/write access by the container should be set to this user." The repo bind mount isn't, and the app now writes to it on startup.

I suspect this is invisible on macOS and Docker Desktop, where the file sharing layer maps ownership so the write succeeds. It reproduces reliably on native Docker under Linux/WSL2, where the host user is typically uid 1000.

Reproducing the bug

  1. Clone master fresh (I was on e5b6d22c8) and run make git
  2. docker compose up -d on native Docker (not Docker Desktop), host user uid 1000
  3. curl http://localhost:8080/
  • Expected behavior: the site serves
  • Actual behavior: web exited (3), connection refused; docker compose logs web shows the traceback above

Deterministic in both directions: rm _testing-prs.json && docker compose restart web fails every time, and recreating the file and restarting brings it straight back up.

Context

  • Browser (Chrome, Safari, Firefox, etc): n/a
  • OS (Windows, Mac, etc): Windows 11 + WSL2, Ubuntu 26.04
  • Logged in (Y/N): n/a
  • Environment (prod, dev, local): local — Docker Engine 29.1.3, Compose 2.40.3, no Docker Desktop

Breakdown

Implementation Details (for maintainers)

The file backs the /status page and the testing-status API rather than anything core, so tolerating an unwritable path and letting that page degrade seems proportionate. Writing to a container-writable location instead would also fix it. I didn't want to assume which you'd prefer.

Workaround for anyone blocked meanwhile: create _testing-prs.json in the repo root before first boot.

Requirements Checklist

  • [ ] docker compose up -d on a fresh clone serves :8080 with no manually created files
  • [ ] startup no longer fails when the repo root isn't writable by uid 999

Related files

  • openlibrary/plugins/openlibrary/status.py:695 — _ensure_testing_state_file(), added in #13278
  • docker/Dockerfile.olbase:22-23 — pins the container user to uid 999
  • .gitignore:75 — /_testing-prs.json

Stakeholders

  • @RayBB (#13278)

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.

pnelson1337 avatar Sep 08 '26 01:09 pnelson1337

Thank you @pnelson1337 for submitting this issue!

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

  • [ ] Triage — maintainers will add Priority: * and Lead: @*
  • [ ] Needs: Staff Decision — should the fix (a) let startup tolerate an unwritable state-file path and degrade the /status page / testing-status API gracefully, or (b) relocate the write target off the bind-mounted repo root (e.g. a container-writable path)? The report deliberately leaves this open.

Context

  • _ensure_testing_state_file() (https://github.com/internetarchive/openlibrary/blob/e5b6d22c8efe99714efa974acdf7d8c77b165c1c/openlibrary/plugins/openlibrary/status.py#L687-L695) does an unconditional, unguarded write when TESTING_STATE_FILE doesn't exist — no try/except around the PermissionError path, confirming the reported traceback.
  • It's only invoked when local dev is detected: setup() at https://github.com/internetarchive/openlibrary/blob/e5b6d22c8efe99714efa974acdf7d8c77b165c1c/openlibrary/plugins/openlibrary/status.py#L885-L888 gates the call on get_ol_env().LOCAL_DEV, and compose.override.yaml:28 (and 4 other services) sets LOCAL_DEV=true — so this write path fires on every local dev boot, not just some.
  • compose.override.yaml:26 confirms the web service bind-mounts ${OL_MOUNT_DIR:-.}:/openlibrary with no user: override, so the container runs as the Dockerfile's uid 999 against a host-owned mount — matching the reporter's diagnosis exactly. docker/Dockerfile.olbase:17-23 is the uid 999 rule/comment quoted in the report.
  • _save_testing_state() a few lines above (https://github.com/internetarchive/openlibrary/blob/e5b6d22c8efe99714efa974acdf7d8c77b165c1c/openlibrary/plugins/openlibrary/status.py#L682-L684) already writes this same file atomically (tmp file + rename) from both a FastAPI GET handler and a web.py POST handler — worth preserving that pattern if the fix relocates the write target.
  • The code was introduced in #13278 ("Add FastAPI testing-environment status API (maintainer-gated)", merged 2026-08-18), matching the report's citation.
  • No duplicate found: searched issues for _testing-prs.json, bind-mount/uid-999 permission wording, and "fresh clone does not boot" — only turned up #13474 (a follow-on UI request against the same status page) and #12950 (the parent "Testing Deployment Process" epic), neither of which covers this boot failure.

Issue Assessment

Open questions

  • [ ] Which of the two remediation directions in the report should be taken — degrade gracefully on an unwritable path, or move the write target? (Requires a maintainer decision; see Needs: Staff Decision above.)
Full triage checklist
  • [x] Triage — labels applied (Needs: Triage, Needs: Lead, Needs: Staff Decision, Module: Docker, Affects: Configuration, Affects: Server)
  • [x] Problem / opportunity — clear: fresh native-Docker-Linux/WSL2 clones fail to boot at all; root cause identified down to the exact line and confirmed by independent research
  • [x] Scope — narrowly scoped to one startup failure; does not bundle unrelated work
  • [x] Justification — impact is severe (blocks first-run for all native-Docker Linux/WSL2 contributors) and self-evident from the traceback
  • [x] Success & testing criteria — requirements checklist in the report is concrete and independently verifiable (docker compose up -d on a fresh clone must serve :8080)
  • [ ] Proposal & breakdown — root cause and requirements are concrete, but the report intentionally does not commit to one of two fix directions; a contributor cannot start coding until that's decided
  • [x] If bug: environment & repro steps — OS, Docker/Compose versions, and exact repro steps are given, plus a deterministic remove/restore test
  • [x] Risks, concerns, open questions — the open design decision is surfaced by the reporter and confirmed above
  • [x] Richy context — relevant code, prior PR, and duplicate search reviewed; no duplicate found
  • [x] References — code citations and file paths present and verified against current master

🔔 Stakeholders

@RayBB — dev experience/FastAPI migration lead; also the author of #13278, which introduced this code path.


[!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 Sep 08 '26 01:09 openlibrary-bot

Small addition, same root cause showing up in a second place. Running the tests in the dev container, pytest can't write its cache into the repo root either:

PytestCacheWarning: could not create cache path /openlibrary/.pytest_cache/v/cache/nodeids:
[Errno 13] Permission denied: '/openlibrary/pytest-cache-files-6ujlkqv5'

That one is only a warning and the tests still pass, so it isn't blocking anything on its own. It may be worth weighing against the two options in triage though: tolerating an unwritable path fixes the state file specifically, whereas the underlying condition — uid 999 can't write the bind-mounted repo root — is evidently broader than the single call site and will keep surfacing.

pnelson1337 avatar Sep 08 '26 01:09 pnelson1337

A third instance, and this one blocks rather than warns. npm run test, the JS half of make test, fails the same way on clean master:

Error: EACCES: permission denied, open '/openlibrary/static/icons/sprite.svg.tmp'
    at scripts/build_icon_sprite.mjs:186

Same mechanism — static/icons is owned by the host user (1000) at 755, the container is uid 999, so the sprite build can't write its temp file.

Worth flagging because it shifts the picture a little: make test is make test-py && npm run test && make test-i18n, and on native Docker it currently can't complete for two independent reasons — this one, plus the LOCAL_DEV issue I filed separately as #13587. The sprite build also isn't a status page that can degrade; it's part of the normal build path.

pnelson1337 avatar Sep 08 '26 02:09 pnelson1337

One more instance, and together I think they point at a pattern rather than a set of call sites.

make lint fails the same way — uv run creates its virtualenv in the working directory:

error: failed to create directory `/openlibrary/.venv`: Permission denied (os error 13)
make: *** [Makefile:89: lint] Error 2

That one arrived 2026-08-26 in #13461, the change that routed lint through uv.

What makes it look systemic is that the project already has the pattern for this. static/build, node_modules and vendor are named volumes (ol-build, ol-nodemodules, ol-vendor), so the container owns them and can write freely — which is exactly why npm run watch works fine on the same setup. The paths that fail are the ones not covered: the repo root, static/icons, and now .venv.

So the question may be less "should this call site tolerate an unwritable path" and more "which paths does the dev container need to write, and are they all covered". Three separate changes in the last few weeks have each landed on one that isn't.

pnelson1337 avatar Sep 08 '26 02:09 pnelson1337

Can we switch writes to a named volume instead of mounted directory?

This concerns write access into the mounted openlibrary/ directory from within the container

@cdrini hits this in claude-sandbox-land and may have thoughts.

mekarpeles avatar Sep 08 '26 19:09 mekarpeles

I have a native-Docker setup here, so I tried it. Short version: it works per-path, but it doesn't generalise as neatly as I'd hoped.

A named volume isn't sufficient on its own. Mounting a fresh one at /openlibrary/.venv still failed, because Docker initialises a volume from the image's directory at that mount point — and that path doesn't exist in the image, so the volume came up root-owned. The existing volumes work precisely because Dockerfile.olbase creates /openlibrary as openlibrary:openlibrary (line 62) and copies the tree in with --chown (line 85), so ol-build, ol-nodemodules and ol-vendor inherit uid 999. Any new volume would need the same, i.e. a mkdir/chown in the image alongside it.

Once I chowned the volume manually, make lint got further and then hit the next one:

error: failed to write to file `/openlibrary/uv.lock`: Permission denied

uv run --no-project skips the venv and lockfile, and then ruff wants /openlibrary/.ruff_cache. So for lint it's one volume per tool artifact, and the list grows whenever a tool is added — CI avoids this entirely by using UV_SYSTEM_PYTHON with uv pip rather than uv run.

Two of the paths can't be volumes at all, as far as I can tell. sprite.svg and icons.generated.js are generated files that live inside directories of tracked sources (static/icons/src/, openlibrary/components/lit/), so mounting over the parent would shadow the inputs the build reads.

Which might point the other way — making the bind-mounted root writable by uid 999 rather than enumerating paths. That's effectively what Docker Desktop's file sharing does, which would explain why none of this shows up on macOS. Happy to test whichever direction you'd prefer; I have the environment set up.

pnelson1337 avatar Sep 08 '26 19:09 pnelson1337

I hit this repeatedly setting up and working on #13666, and I think there is a dimension to it that hasn't come up yet.

Beyond the _testing-prs.json boot failure, the same root cause bit me in five more places:

  • npx eslint --fix and npx stylelint --fix in the container rewrite the file as the container user, after which VS Code cannot save it
  • scripts/i18n-messages extract fails with PermissionError on openlibrary/i18n/messages.pot
  • running the container as the host user instead (-u $(id -u):$(id -g)) fixes those but then cannot write static/build/, so the asset build fails
  • git rebase silently re-breaks it — git rewrites files with the host user's default group and without the group-write bit, so the container loses write access again

That last one is why sudo chown -R $USER:999 . && sudo chmod -R g+w . has to be re-run rather than applied once. I have now run it three times in a single working session.

On @mekarpeles's named-volume suggestion and @pnelson1337's finding that it does not generalise: I think the git behaviour explains why. The problem is not really a list of write paths to redirect. It is that any ownership scheme depending on a specific uid/gid and mode cannot survive ordinary git operations, which reset both.

Two directions that would survive that, neither of which I have tested at scale:

  1. Run the container as the host uid/gid (user: "${UID}:${GID}" in the compose file) so no ownership translation is needed. Build output directories would need to be created writable, or moved to named volumes.
  2. Keep the current user but add a make fix-perms target, documented in AGENTS.md next to make git, so at least the recovery is discoverable instead of folklore.

I would like to work on this. Which direction do you prefer, or is there a third you had in mind?

MohitSAGAR11 avatar Sep 25 '26 05:09 MohitSAGAR11

What is the _testing-prs.json feature? What environments should it run on?

I can't find clear documentation of what has_testing_state is, or what environments it applies to.

There seems to be some assumed integration with github and having github keys that doesn't seem applicable to all environments, but there's not much stated to untangle it.

Edit: to be more specific how this is confusing, the explicit check is for LOCAL_DEV

    if get_ol_env().LOCAL_DEV:
        _ensure_testing_state_file() 

but it appears this only is meaningful on deployed testing or staging server?

hornc avatar Oct 12 '26 20:10 hornc

@hornc I traced this while working on #13587, and I think the gate is backwards. Everything below is from the repo; someone with access to the deploy config should confirm point 2.

What has_testing_state is. Not an environment check — just whether _testing-prs.json exists:

has_testing_state = _load_testing_state() is not None   # status.py:49
show_testing = has_testing_state and is_maintainer_user

Its only consumer is the /status template, which renders the TestingEnvironment panel for maintainers and, for everyone else, the notice "The testing environment panel is only visible to maintainers." (status.html:42-47).

Why the LOCAL_DEV gate looks inverted. LOCAL_DEV=true is set in exactly two files — compose.override.yaml (six services, the local dev stack) and compose.near-prod.yaml. It isn't in compose.yaml. So _ensure_testing_state_file() runs on contributors' laptops and not on the deployed testing server.

On the testing server the file arrives by a different route anyway: the maintainer-gated handlers that call _save_testing_state() (status.py:94, 107, 120, 152, 187, 670). So the auto-create does nothing where the feature lives, and where it does run, the only effect a non-maintainer sees is a notice about a panel that has no local meaning.

It also isn't needed. _load_testing_state() returns None when the file is missing, and all nine call sites already handle that — lines 49, 87, 101, 114, 138, 159, 580, 583, and 643 (or TestingState(...)). Nothing breaks without the file; /status/testing.json already 404s with "No testing state file found".

Which makes the smallest fix for this issue deleting _ensure_testing_state_file() and its call in setup(). No startup write, no PermissionError, no behaviour change on the testing server, and the docstring's goal — "functional from first boot" — is already met by the None handling.

That resolves the Needs: Staff Decision on this issue without choosing between the two options in the report: there's no write to tolerate or relocate.

It does not fix the broader problem. As I noted above and @pnelson1337 documented, uid 999 can't write the bind-mounted repo root at all, so npm run test, make lint, i18n extraction and eslint/stylelint --fix still fail, and git rebase resets ownership each time. That needs its own decision. But it unblocks first boot, which is the part that stops people cold.

@RayBB you wrote #13278 — was the LOCAL_DEV gate meant to be the inverse, i.e. create it on testing/staging rather than locally? If the panel is supposed to appear on a dev machine for some reason I've missed, that changes the answer.

MohitSAGAR11 avatar Oct 13 '26 03:10 MohitSAGAR11