Repository navigation
Release Windows ML interfaces when a text generation session closes - #1520
Merged
Zhipeng Wang (timenick) merged 1 commit intoNov 5, 2026
Merged
Conversation
windowsml wraps each COM interface with ctypes.cast, which leaves the pointer in a reference cycle, so closing a wrapper releases the interface only when the cycle collector runs. Left to interpreter exit, comtypes no longer releases it, and Windows ML destroys its D3D12 transfer pools while its DLLs unload, after the GPU driver has unloaded: winml perf on a GGUF file finished every request and then exited with 0xC0000409 when the GPU target was an Intel iGPU. WinMLLlamaSession.close() and WinMLCGCSession.close() now run the cycle collector after closing their handles, so the interfaces are released while COM is initialized.
Qiong Wu (qiowu) (DingmaomaoBJTU)
approved these changes
Nov 5, 2026
Qiong Wu (qiowu) (DingmaomaoBJTU)
left a comment
Collaborator
There was a problem hiding this comment.
Reviewed the teardown change and both session close paths. Collection runs after wrapper handles are closed and cleared, outside generation timing; the shared helper adds no native import dependency. No blocking findings. Local validation: 137 tests passed across CGC session, llama session, and shared Task generation. Also disabled the new helper in-memory and confirmed both new regression tests fail, demonstrating that they exercise the intended cleanup behavior. The reported Intel/NVIDIA native shutdown crash was not reproduced locally. CI lint and completed test jobs pass; some remaining tests/CodeQL were still running at review time and should finish successfully before merging.
Zhipeng Wang (timenick)
deleted the
zhiwang/perf-release-winml-before-exit
branch
November 5, 2026 09:17
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.
Summary
winml perfon a.gguffile finishes every request and writes its report, then the process exits with0xC0000409when Windows ML puts the Task's GPU target on an Intel iGPU. On an RTX 5090 D desktop with Intel Graphics this happened on every run with windowsml 2.8.183.dev0, and also with 2.7.2021a0. A caller that checks the exit code sees a failed benchmark.WinMLLlamaSession.close()andWinMLCGCSession.close()now run the cycle collector after closing their handles, so the Windows ML interfaces are released while COM is still initialized.Cause
windowsmlwraps each COM interface withctypes.cast(out, POINTER(interface)), which records the pointer in its own_objects. The pointer stays in that reference cycle after its wrapper closes, so the interface is released only when the cycle collector runs. After a run,gc.garbageholds theIWinMLRuntime,IWinMLExecutionTarget,IWinMLPipeline,IWinMLStage,IWinMLTokenizerandIWinMLTextGenerationConfigurationpointers.WinMLRuntimeCore.dllthen destroys its D3D12 transfer pools fromDllMain(dllmain_crt_process_detach→CD3D12TransferPool::~CD3D12TransferPool) and releases command lists and queues in a GPU driver that has already unloaded. Inigd12um64xeh.dllthat is an access violation, which the driver turns into__fastfail(0xC0000409). With the target on the NVIDIA GPU the same use-after-free happens innvwgf2umx.dll, but it crashes only under a debugger._create_targetasks forExecutionTargetKind.GPUwithExecutionTargetPreference.DEFAULT; llama.cpp itself runs on CUDA device 0.The teardown order is a Windows ML bug. This change keeps winml-cli from leaving its objects to it.
Change
session/task_generation.py:_release_closed_interfaces()runsgc.collect()and documents why.WinMLLlamaSession.close()andWinMLCGCSession.close()call it after closing their handles.hold_cyclic_interfacegives a fake handle an interface in a reference cycle, asctypes.castleaves one. With automatic collection disabled, both sessions'close()now free it; both tests fail without the change.Results on an RTX 5090 D with Intel Graphics (windowsml 2.8.183.dev0)
winml perf -m Qwen3-0.6B-Q4_0.gguf --device gpu --max-new-tokens 16 --warmup 1 --iterations 1:0xC0000409on every runFollow-ups (not in this PR)
ExecutionTargetPreference.PERFORMANCE, as the CGC route already picks the high-performance GPU, so Windows ML does not create a D3D12 device on an iGPU that llama.cpp does not use.🤖 Generated with Claude Code