Skip to content

fix(windows): environment set from Rust does not reach MLX's getenv #2803

Closed

Description

@inureyes

Problem

On Windows, std::env::set_var calls SetEnvironmentVariableW, while the C runtime's getenv reads its own copy of the environment. A probe measured getenv returning NULL after SetEnvironmentVariableW. MLX's env::get_var uses std::getenv (mlx/utils.cpp).

PR #2799 found this for MLX_PTX_CACHE_DIR and fixed that one variable by reading it with GetEnvironmentVariableW in the overlay.

Other startup writes from Rust that native code reads with getenv are therefore likely inert on Windows.

  • Reader confirmed: MLX_USE_CUDA_GRAPHS (src/loading/mod.rs:688, read in the CUDA device.cpp).
  • Readers not yet checked: MLX_CUDA_GRAPH_CACHE_SIZE and MLX_CUDA_SDPA_CACHE_SIZE (src/lib/mlxcel-core/src/hardware.rs), MLX_ENABLE_TF32 (src/lib.rs, src/lib/mlxcel-core/src/lib.rs), cuda_graph_budget.rs, autotune/ops/cuda_kernel_knobs.rs, MLX_MAX_OPS_PER_BUFFER in hardware.rs.

Possible failure, not reproduced: a long-lived Windows mlxcel-server keeps MLX's graph cache capacity of 400 instead of mlxcel's 2000 and reaches the fatal "Cache thrashing" abort from #818. The per-model graph capture disable and the autotuner's kernel knobs may also do nothing on Windows.

Work

Do all of this in one PR, with one verification pass at the end and as few pushes as possible.

  1. Audit every production write of an environment variable that native code reads, and list it in the PR.
  2. Add one helper in mlxcel-core that sets (and unsets) a variable for both Rust and the C runtime on Windows, and route those writes through it. No behavior change on other platforms. Failing test first: after the helper, a native getenv through the bridge sees the value.
  3. Add a guard, so a new direct write of an MLX_* variable read natively fails CI or a test.
  4. Because this changes which settings take effect on Windows, measure serving on an RTX Spark N1X before and after, and record any change.

Acceptance Criteria

  • The effective values are confirmed at run time on Windows for the audited variables, in mlxcel and mlxcel-server.
  • Docs that describe these defaults stay true on Windows.

Activity

  1. added
    status:ready
    type:bug
    priority:high
    area:core
    platform:windows
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Oct 31, 2026
  2. added
    status:done
    and removed
    status:review
    on Nov 1, 2026
  3. added a commit that references this issue on Nov 2, 2026
    f1839fd
  4. added this to the 0.8 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:coreplatform:windowspriority:highstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions