Skip to content

[CUDA] Avoid releasing DXGI adapters during DLL teardown - #4867

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
strayberry:fix/cuda-wddm-process-exit
Nov 20, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
strayberry:fix/cuda-wddm-process-exit

Conversation

@strayberry

@strayberry strayberry commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor
  • I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Codex assisted with the implementation, regression tests, experiments, and PR description. I reviewed the changes and validation results and take responsibility for this contribution.

On Windows, a program can finish its CUDA work successfully and still exit with 2170 (0x87A) when mlx.dll unloads.

The function-local WddmMemoryBudget owns DXGI adapter references. Its destructor releases them during DLL teardown, under the loader lock. Microsoft documents that DXGI object creation and release must be avoided from DLLMain; the restriction also applies to static C++ destructors invoked by the CRT.

Keep the budget cache alive through process teardown. This intentionally retains its allocation and adapter references until process termination. Repeated explicit DLL unload/reload resource reclamation is outside this change.

Add a separate CTest executable for Windows CUDA shared builds. It verifies that the WDDM budget is initialized, evaluates a GPU reduction, clears streams and returns normally. CTest then checks the actual process exit. Unsupported devices skip with code 77; a 30-second timeout also catches a teardown hang.

Validation

Fresh upstream fc344c986, Windows x64, RTX 4060 Ti (sm_89, WDDM), driver 610.88, CUDA 13.0.48, cuDNN 9.19, MSVC 19.44, CPU+CUDA shared build.

Check Original Patched
Same standalone executable against separately installed DLLs 3/3 exit 2170 3/3 exit 0
New process-exit CTest Prints GPU work completed; testing process exit., then fails 10 consecutive runs pass
Existing WDDM assertions 5/5 pass, but process exits 2170 Included in the passing CUDA suite

All cases in cuda_tests.cpp pass after the fix: 4 cases, 16 assertions, process exit 0. Full pre-commit run --all-files passes with PYTHONUTF8=1. The full Python suite and explicit DLL unload/reload were not tested.

Follow-up LayerNorm racecheck runs initially hung during termination with Compute Sanitizer 2025.3. Changing only the sanitizer package to 2025.4.0.0 (build 36782660) resolves the observed hang with the same executable, DLLs, CUDA 13.0 build/runtime environment and driver. Both DLL variants include this WDDM fix: without #4866's kernel fix, the scratch test reports 8 failed assertions and 8 hazards, then exits 86; with that kernel fix, both new tests pass 56/56 assertions with 0 hazards and exit 0. Both CUDA Graphs settings complete, and the passing variant was run twice per setting. These controls isolate the hang to the older sanitizer configuration, consistent with NVIDIA's documented Windows exit-hang fixes.

After configuring a Windows CUDA shared build with tests enabled and its runtime dependencies on PATH:

cmake --build build --config Release --target cuda_wddm_exit_test
ctest --test-dir build -C Release -R "^cuda_wddm_process_exit$" --repeat until-fail:10 --output-on-failure

To reproduce the original failure, apply only the test changes to fc344c986 and run the same CTest. The original and patched SDKs were kept separate during validation.

@zcbenz
zcbenz force-pushed the fix/cuda-wddm-process-exit branch from ebc3700 to 9d73bc7 Compare November 20, 2026 04:44
@zcbenz
zcbenz force-pushed the fix/cuda-wddm-process-exit branch from 9d73bc7 to 455ebc2 Compare November 20, 2026 04:45
@zcbenz
zcbenz marked this pull request as ready for review November 20, 2026 04:45
@zcbenz
zcbenz merged commit 2905f33 into ml-explore:main Nov 20, 2026
24 checks passed
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants