Skip to content

feat(components): add a DYN_JEMALLOC preload opt-in for the router and indexer - #15880

Open
PeaBrane wants to merge 2 commits into
mainfrom
rupei/dyn-jemalloc-optin
Open

PeaBrane wants to merge 2 commits into
mainfrom
rupei/dyn-jemalloc-optin

Conversation

@PeaBrane

@PeaBrane PeaBrane commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Status (2026-10-09). John Thomson is taking these over. This PR is independent of the indexer stack and ready to merge. If #15884 also merges, whichever lands second should collapse the DYN_FRONTEND_JEMALLOC docs entry into an alias of DYN_JEMALLOC; the two entries currently repeat each other.

Summary

Today only the frontend can opt in to jemalloc: python -m dynamo.frontend reads DYN_FRONTEND_JEMALLOC, adds libjemalloc to LD_PRELOAD, and re-execs itself once. No other component reads a DYN_FRONTEND_* variable. The standalone router (python -m dynamo.router, including --serve-indexer) and the standalone KV indexer (python -m dynamo.indexer) have no opt-in, so operators have to set LD_PRELOAD to an architecture-specific library path by hand.

This PR adds DYN_JEMALLOC, a generic opt-in shared by all three entry points. It follows the DYN_<FEATURE> naming used for settings that apply across components.

  • Shared helper. The frontend's preload code moves into dynamo._jemalloc, a module that uses only the standard library.
    • It lives in the runtime wheel because dynamo.indexer ships there and cannot import ai-dynamo.
    • dynamo.common.utils was not an option: importing it loads the Rust extension, and the preload must happen before that.
    • Importing dynamo._jemalloc does not load the extension.
  • Unchanged behavior. These match the current frontend code:
    • accepted values: 1, true or yes, case-insensitive;
    • no re-exec if LD_PRELOAD already names a libjemalloc;
    • the warning when the library is missing;
    • one execve with jemalloc prepended to LD_PRELOAD.
  • What is new in the helper. It reads DYN_JEMALLOC, and an entry point can also pass its own alias.
    • Only the frontend passes DYN_FRONTEND_JEMALLOC, so existing frontend deployments keep working. The frontend preloads when either variable is enabled.
    • Warnings now name the variable that turned the preload on.
  • Entry points. dynamo.frontend, dynamo.router and dynamo.indexer each call the helper before importing the runtime.
    • The router's module body moves unchanged from __main__.py to main.py, matching the frontend's layout. python -m dynamo.router keeps the same CLI.
    • Side effect: router log lines now show the module as main instead of __main__.
  • Defaults. No change: jemalloc stays opt-in.
  • Docs.
    • The frontend configuration reference ("Host memory allocator") documents DYN_JEMALLOC, its use in the router and indexer, and the frontend-only alias.
    • The standalone indexer page links to that section.

Validation

  • pytest lib/bindings/python/tests/test_jemalloc_preload.py, covering the shared helper:
    • disabled values;
    • the alias is ignored unless the entry point passes it;
    • already preloaded, with : or space separators;
    • library missing: a warning that names the variable, then the process continues;
    • enabled: execve receives LD_PRELOAD with jemalloc prepended and the original argv, after stdout and stderr are flushed;
    • exec failure: a warning that names the variable, and LD_PRELOAD unchanged;
    • indexer entry point: the preload runs before dynamo.indexer.main is imported.
  • pytest components/src/dynamo/frontend/tests/test_jemalloc.py components/src/dynamo/router/tests/test_router_jemalloc.py components/src/dynamo/router/tests/test_standalone_router.py:
    • frontend: both variables re-exec before the runtime import, and importing __main__ without running it does not restart;
    • router: DYN_JEMALLOC re-execs before the runtime import, and DYN_FRONTEND_JEMALLOC is ignored.
  • Import-order check: running each module with os.execve stubbed shows that dynamo.frontend, dynamo.router and dynamo.indexer load only dynamo, dynamo._jemalloc and their own package before the re-exec. dynamo._core is not loaded.
  • Manual checks with a local bindings build (kv-indexer feature) and libjemalloc on the library path. MALLOC_CONF=stats_print:true was used to confirm that jemalloc loaded:
    • DYN_JEMALLOC=1 python -m dynamo.indexer --help loads jemalloc and re-execs exactly once.
    • The indexer without the variable, or with only DYN_FRONTEND_JEMALLOC=1, does not load jemalloc.
    • With the library missing, the indexer warns and still prints help.
    • DYN_JEMALLOC=1 python -m dynamo.router --help loads jemalloc, and its help output is identical to the previous module's.
    • python -m dynamo.frontend --help loads jemalloc with either variable.
  • pre-commit run --files <changed files>, mypy on the touched modules, and docs/fern/scripts/docs_lint.py.

Summary by CodeRabbit

  • New Features
    • Added opt-in jemalloc support for the frontend, standalone router, and KV indexer. When enabled, jemalloc is loaded at startup; if it is unavailable, services continue with the default allocator.
    • Added standalone router endpoints for generation, best-worker selection, and overlap scores.
  • Documentation
    • Added configuration guidance for enabling jemalloc and setting its library path.

…d indexer

Only `python -m dynamo.frontend` can preload jemalloc today, through
DYN_FRONTEND_JEMALLOC. The standalone router and the standalone KV indexer
have no opt-in and need a hand-written LD_PRELOAD with the library path.

Move the frontend's preload helper into the stdlib-only module
dynamo._jemalloc in the runtime wheel, so every entry point can call it
before importing the Rust extension, including dynamo.indexer, which ships
in that wheel. The helper reads the new DYN_JEMALLOC. An entry point can
also pass a component-specific alias; only the frontend passes
DYN_FRONTEND_JEMALLOC, and it preloads when either variable is enabled.
Truthy parsing, already-preloaded detection, the missing-library warning,
and the single re-exec are unchanged. Warnings now name the variable that
enabled the preload.

Call the helper before the runtime import in dynamo.frontend,
dynamo.router, and dynamo.indexer. The router's module body moves verbatim
from __main__.py to main.py, as in the frontend, so `python -m
dynamo.router` keeps the same CLI. jemalloc stays opt-in.

Signed-off-by: PeaBrane <yanrpei@gmail.com>
@PeaBrane
PeaBrane requested review from a team as code owners October 8, 2026 04:51
@github-actions github-actions Bot added feat documentation Improvements or additions to documentation frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` router Relates to routing, KV-aware routing, etc. labels Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: ai-dynamo/dynamo/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 85cadffe-355b-4278-8f41-490c003e0b48

📥 Commits

Reviewing files that changed from the base of the PR and between 1668037 and f8521df.


📒 Files selected for processing (12)
  • components/src/dynamo/frontend/__main__.py
  • components/src/dynamo/frontend/tests/test_jemalloc.py
  • components/src/dynamo/router/AGENTS.md
  • components/src/dynamo/router/__main__.py
  • components/src/dynamo/router/main.py
  • components/src/dynamo/router/tests/test_router_jemalloc.py
  • components/src/dynamo/router/tests/test_standalone_router.py
  • docs/fern/pages/developer-guide/knowledge-base/modular-components/router/standalone-indexer.md
  • docs/fern/pages/reference/components/frontend-configuration.mdx
  • lib/bindings/python/src/dynamo/_jemalloc.py
  • lib/bindings/python/src/dynamo/indexer/__main__.py
  • lib/bindings/python/tests/test_jemalloc_preload.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 8 remain after this review.



Walkthrough

The change adds a shared jemalloc preload helper and wires it into the indexer, standalone router, and frontend entry points. It moves the standalone router service implementation from __main__.py to main.py and updates tests and documentation.

Changes

Python entry points and standalone router

Layer / File(s) Summary
Shared jemalloc helper and indexer startup
lib/bindings/python/src/dynamo/_jemalloc.py, lib/bindings/python/src/dynamo/indexer/__main__.py, lib/bindings/python/tests/test_jemalloc_preload.py, docs/fern/pages/developer-guide/knowledge-base/modular-components/router/standalone-indexer.md, docs/fern/pages/reference/components/frontend-configuration.mdx
The helper checks the configured environment variables, skips re-execution when jemalloc is already in LD_PRELOAD, and otherwise looks up the library and re-executes with it prepended. The indexer calls the helper before importing its runtime. Tests and documentation cover the preload behavior.
Standalone router service and entry point
components/src/dynamo/router/main.py, components/src/dynamo/router/__main__.py, components/src/dynamo/router/tests/test_router_jemalloc.py, components/src/dynamo/router/tests/test_standalone_router.py, components/src/dynamo/router/AGENTS.md
The router handler, configuration parsing, worker, and endpoints are in main.py. The __main__ module preloads jemalloc before importing and invoking the service. Tests and the key-files table use the updated module roles.
Frontend preload wiring
components/src/dynamo/frontend/__main__.py, components/src/dynamo/frontend/tests/test_jemalloc.py
The frontend entry point calls the shared helper with DYN_FRONTEND_JEMALLOC as an alias before importing its runtime. Tests check disabled and enabled startup paths.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: ⚪ Minimal · up to f8521

The documented entry points retain their expected behavior, and no actionable issue remains before merge.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 27.59% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 29 functions across 9 files. (3 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check Warning The description provides detailed scope, implementation notes, behavior, documentation changes, and validation results. However, it omits the required Related Issues reference and does not include the… Add the required Related Issues section with a valid issue reference, such as Closes #15884 or Relates to #15884. Add the Overview, Details, and Where should the reviewer start sections, or clearly map the existing content to those head…
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly describes the main new capability: an opt-in DYN_JEMALLOC preload for the router and indexer. It does not mention the frontend alias, but the title need not cover every implementatio…

Full details: Docstring Coverage

Explanation

Docstring coverage is 27.59% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 29 functions across 9 files. (3 skipped: 3 unsupported.)


Full details: Description check

Explanation

The description provides detailed scope, implementation notes, behavior, documentation changes, and validation results. However, it omits the required Related Issues reference and does not include the template sections for Overview, Details, or reviewer starting points.

Resolution

Add the required Related Issues section with a valid issue reference, such as Closes #15884 or Relates to #15884. Add the Overview, Details, and Where should the reviewer start sections, or clearly map the existing content to those headings.



  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR


  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

Comment thread lib/bindings/python/tests/test_jemalloc_preload.py Outdated
Comment thread lib/bindings/python/tests/test_jemalloc_preload.py Outdated
The already-preloaded and restart paths do not depend on which variable
enabled the helper, so run them under DYN_JEMALLOC=1 only. Alias and
selection coverage stays in test_alias_ignored_unless_passed and the
ENABLED matrix in test_missing_library and test_exec_failure.

Signed-off-by: PeaBrane <yanrpei@gmail.com>
Comment thread lib/bindings/python/tests/test_jemalloc_preload.py
@devin-ai-integration

Copy link
Copy Markdown
Contributor

✅ Dynamo PR CI passed — run 37733043913 (attempt 1) on c6a20fa61c

Gate checks: ✅ backend-status-check · ✅ deploy-status-check · ✅ dynamo-status-check

Posted automatically by Devin for run 37733043913. Updated on every full-CI run of this PR.

@dynamo-review-agent dynamo-review-agent Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously reported defects still present:

  • Original discussion: The exec-failure test still runs the full ENABLED matrix against all three LD_PRELOAD states. The enabled-source warning contract is already retained by the ENABLED cases, while LD_PRELOAD preservation only needs absent, empty, and nonempty states under one enabled source, so the cross product remains non-discriminating.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation feat frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` router Relates to routing, KV-aware routing, etc. size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant