Repository navigation
[cherry-pick][2.60.0][ci][doc] Stop MyST eval-rst blocks from pickling the build environment and restore full Sphinx parallelism on Read the Docs (#66784, #66793) - #67797
Merged
Conversation
…nto doctrees (#66784) ## Why are these changes needed? Every Markdown page with an `{eval-rst}` block pickles a full copy of the Sphinx `BuildEnvironment` into its doctree. On master that's 25 doctrees at 28 to 40 MB each, about 0.78 GB, where the same pages are tens of KB without the leak. The parallel HTML writer loads those doctrees back into memory, which pushes the Read the Docs build close to the builder's memory limit. #66744 splits the Serve and Train API pages into 23 Markdown pages with `{eval-rst}` blocks. Its preview build was killed for running out of memory twice, at the start of the write phase ([4526993](https://app.readthedocs.com/projects/anyscale-ray/builds/4526993/), [4527163](https://app.readthedocs.com/projects/anyscale-ray/builds/4527163/)). Measured locally with RtD's command (`sphinx-build -a -E -W -j 2`), peak memory across the Sphinx processes was 9.4 GB on master and 11.5 GB on that PR. The ongoing RST-to-MyST conversion adds `{eval-rst}` pages with most files it converts, so master would hit the same limit on its own. ### Cause `myst_parser.mdit_to_docutils.base.render_restructuredtext` parses the block into a throwaway docutils document that shares the page's `settings` object, then moves the parsed nodes into the page with `extend()`. docutils' `setup_child` re-points `node.document` only on the direct children it appends. Deeper nodes that docutils gave an explicit `document` reference during parsing, such as bullet list items, Python domain signatures, and autosummary table rows, keep pointing at the throwaway document. That document's `settings.env` is the live `BuildEnvironment`. Sphinx sets `settings.env = None` on the page's own doctree before pickling it, but not on the throwaway document. Pickling the page therefore pickles the whole environment, including `sphinx.ext.viewcode`'s cache of Python module sources. myst-parser 5.1.0, the version Ray pins, is the latest release. The closest upstream report, executablebooks/MyST-Parser#953, covers the same incomplete merge after `eval-rst` but only its footnote symptom. ### Fix A new in-repo extension, `doc/source/_ext/myst_eval_rst_doctree.py`, connects to `doctree-read`, the last event before Sphinx pickles a doctree. It points every node that still references another document at the page's own doctree. The nodes are part of that doctree by then, so this is the reference docutils would have set had it re-pointed the whole subtree. Re-pointing the nodes in saved doctrees from a full build shrinks them as follows: | Doctree | Before | After | |---|---|---| | `ray-contribute/stability` | 39.6 MB | 50 KB | | `cluster/vms/user-guides/community/spark` | 28.0 MB | 115 KB | | `serve/api/exceptions` (from #66744) | 32.2 MB | 12 KB | `doc/source/_ext/test_myst_eval_rst_doctree.py` is a hermetic test, following the pattern of `test_llms_txt.py`. It builds a one-page MyST project and checks the following: - Without the extension, nodes reference the throwaway document and the pickle carries a `BuildEnvironment`. If a MyST release fixes the leak, this half fails, which flags the extension for removal. - With the extension, every node references the page's doctree and the pickle carries no `BuildEnvironment`. It runs in a new `doc_test_myst_eval_rst_doctree` step next to `doc_test_llms_txt`. ## Verification - `python doc/source/_ext/test_myst_eval_rst_doctree.py` passes locally. - This PR's Read the Docs preview build, [4531787](https://app.readthedocs.com/projects/anyscale-ray/builds/4531787/), passed. - **Memory.** A local build with RtD's command (`sphinx-build -a -E -W -j 2`), sampling total RSS across the Sphinx processes every 2 seconds: | Measurement | Master | With this PR | |---|---|---| | Peak memory, preparing documents | 6.61 GB | 1.90 GB | | Peak memory, writing output | 9.44 GB | 2.53 GB | | Total doctree size | 930 MB | 130 MB | | Doctrees over 5 MB | 25 | 0 | | Build time | 1,522 s | 918 s | - **Output.** Diffing the full HTML output of the two builds: - The 25 pages whose doctrees carried the environment render byte-for-byte identically. - 30 of 9,088 output files differ, and none because of this change: - 19 trace to upstream commits between the two builds' bases: `ray.data` and `ray.train` source edits, `ray-core/sandboxes.md`, and the aggregate files they feed (`api-nav.html`, `llms-full.txt`, `genindex`, `objects.inv`). - 11 differ between any two builds: random gallery icons, UUID equation anchors, type-hint field order, and sitemap write order. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit c6d7e72) Signed-off-by: Ronny Roland <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
## Why are these changes needed? #65643 pinned the Read the Docs build to `SPHINX_JOBS=2`. At `-j auto` the build peaked within about 600 MB of the builder's 6.64 GiB cgroup memory ceiling and was sometimes OOM-killed. The pin roughly doubled the time of the Sphinx step, which takes about 700 of the 770 to 820 seconds in recent master builds. #66784 fixed the leak behind most of that memory. MyST `{eval-rst}` blocks were pickling the whole build environment into each page's doctree, and every write worker loaded those doctrees back. With that fixed, this PR drops the pin, so the RtD build uses the `doc/Makefile` default of `-j auto`, matching local builds and the Buildkite doc build. It also rewrites the `.readthedocs.yaml` comment, and the `doc/Makefile` comment that named RtD as the one caller overriding `SPHINX_JOBS`. The comment keeps the recovery path: if bare `EOFError` failures from `multiprocessing/connection.py` come back, set `SPHINX_JOBS=2` on the make command. ### Other build overhead checked Per-command timings from master build [4532929](https://app.readthedocs.com/projects/anyscale-ray/builds/4532929/): | Step | Seconds | |---|---| | `git clone` | 38 | | pip installs (pip and setuptools, Sphinx, docs lock) | 30 | | Sphinx build at `-j 2` | 702 | Nothing outside the Sphinx step is worth changing. Intersphinx already loads committed snapshots, notebook execution is off, and `llms.txt` generation is skipped on PR builds. RtD's default step also upgrades Sphinx before the lock reinstalls it, but that costs about 5 seconds and removing it means overriding the whole install step. ## Duplicate check No open `ray-project/ray` PR touches `.readthedocs.yaml` or the `SPHINX_JOBS` setting. Searched `SPHINX_JOBS`, `readthedocs.yaml in:title`, and `RtD parallelism`. The three PRs the `SPHINX_JOBS` search returned don't change either file. ## Verification Local full builds of master at 1a9ba71 with this job's flags (`python -m sphinx --keep-going -b html -a -E -W -j N -T -D language=en`), in a Python 3.11 venv installed from `doc/requirements-doc.lock.txt`, sampling total RSS across the Sphinx processes every 2 seconds, the same method #66784 used: | Workers | Result | Wall clock | Peak summed RSS | |---|---|---|---| | 4 | `build succeeded.`, 0 warnings | 425 s | 4.74 GB | | 2 | `build succeeded.`, 0 warnings | 914 s | 2.67 GB | The 2-worker run reproduces #66784's numbers (2.53 GB, 918 s). Summed RSS overstates the cgroup figure because it counts pages shared between forked workers more than once. Before #66784, the same method read 9.44 GB at 2 workers, while RtD's cgroup measured 5.57 GB for the same configuration. #65643 measured 4 CPUs on the builder (`nproc` and `nproc --all`), and I haven't re-measured that. This PR's Read the Docs preview build, [4533416](https://app.readthedocs.com/projects/anyscale-ray/builds/4533416/), ran `sphinx-build ... -j auto` on the real builder and passed: | Build | Sphinx step | Total | |---|---|---| | Master [4532929](https://app.readthedocs.com/projects/anyscale-ray/builds/4532929/), `-j 2` | 702 s | 793 s | | This PR [4533416](https://app.readthedocs.com/projects/anyscale-ray/builds/4533416/), `-j auto` | 411 s | 520 s | That's one build on each side. PR builds also skip `llms.txt` generation, so the master saving might be slightly smaller. AI assistance (Claude Code) was used to measure, write, and describe this change. I reviewed every changed line. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit cec3972) Signed-off-by: Ronny Roland <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
sai-miduthuri
approved these changes
Nov 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Cherry-picks #66784 and #66793 onto
releases/2.60.0, so the release branch builds its docs on Read the Docs the same way master does.The
releases-2.60.0docs have never built on Read the Docs. All four attempts on 2026-11-03 were cancelled partway throughmake htmlwith "Build terminated due to inactivity", so RtD kept no Sphinx output: 4668524, 4668563, 4668796, and 4668932.The branch was cut on 2026-09-30, before these two master changes:
{eval-rst}blocks from pickling the whole build environment into each page's doctree, which every Sphinx worker then loaded back.SPHINX_JOBS=2pin from [ci][doc] Build the docs with 2 Sphinx workers on Read the Docs #65643, which the leak had made necessary, so RtD builds with one Sphinx worker per CPU again.Without them, this branch still builds with the leak and two workers. 2.59.0 built that way in 811 and 912 seconds, against about 500 seconds for master's builds on 2026-11-03.
This may not be the whole fix. Master has both changes and still lost about 1 in 5 RtD builds to the same inactivity kill on 2026-11-03. But 2.60.0 lost all four of its builds in the same period, so its build setup is the likeliest difference.
Changes
This is
git cherry-pick -x -sof c6d7e72 (#66784) and then cec3972 (#66793), with no conflicts. The diff matches the two PRs exactly: 6 files, +219/−24. Only the docs build and its CI change, and nothing that ships in the wheel:doc/source/_ext/myst_eval_rst_doctree.pyand its test, registered indoc/source/conf.py..buildkite/doc.rayci.yml, which runs the new test..readthedocs.yamlanddoc/Makefile, which dropSPHINX_JOBS=2.The two commits need to land together. #66793 on its own would run the leaky build at full parallelism, which RtD sometimes OOM-killed before #66784.
This branch pins the same Sphinx 8.2.3, myst-parser 5.1.0, myst-nb 1.4.0, and docutils 0.21.2 that #66784 was built against. The docs lock differs only in
certifi. Master's only later changes to these files, #66807 and #66756, updatedoc/Makefilecleanup paths for pages that moved after the cut, so they aren't included.Testing
Both commands ran on this branch in a Python 3.11.15 venv built from
doc/requirements-doc.lock.txt.rtd-buildfirst checks the venv against the RtD build. Python, Sphinx, myst-parser, myst-nb, and the theme all match; only the operating system differs, since this ran on macOS. The build is the clean-a -E -Wbuild that RtD runs.-j 4matches the RtD builder's 4 CPUs, which is what the defaultSPHINX_JOBS=autogives there. It succeeded in 458 seconds with no warnings.Summed RSS across the Sphinx processes, sampled every 2 seconds, peaked at 3.56 GB. Summed RSS overstates real use, because forked workers share pages. #66793 measured 4.74 GB the same way on master at
-j 4.Not a duplicate
No open PR targets
releases/2.60.0, and no other PR references #66784 or #66793.AI assistance: Claude Code investigated the failed builds, made the cherry-pick, ran the tests above, and drafted this description.
🤖 Generated with Claude Code