Skip to content

fix: Don't free tracer context in DllMain during process termination - #544

Merged
rwmcguir merged 2 commits into
oneapi-src:masterfrom
mschilling0:dev_windows
Dec 2, 2026
Merged

rwmcguir merged 2 commits into
oneapi-src:masterfrom
mschilling0:dev_windows

Conversation

@mschilling0

@mschilling0 mschilling0 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Matches what's being done here: https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain

lpvReserved is "non-NULL" when process is terminating and its not safe to call the tracer context destructor there.

Guards against a use-after-free occasionally on PyTorch Profiler on Windows.

@mschilling0 mschilling0 changed the title fix: Don't free contexts in DllMain during process termination fix: Don't free tracer context in DllMain during process termination Oct 22, 2026
@mschilling0
mschilling0 marked this pull request as ready for review October 22, 2026 17:42
@rwmcguir

Copy link
Copy Markdown
Contributor

At first glance this issue may also be a Linux issue, so we should explore this more and find a more robust solution...

@mschilling0

Copy link
Copy Markdown
Contributor Author

No problem. This is just a stability improvement. If users don't call L0 late in process exit, I think it should be fine.

@rwmcguir

rwmcguir commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

So I am ok with this as a Windows stability fix, the lpvReserved check matches the DllMain guidance.

One ask, could you add a short comment at the early return explaining the skipped delete is intentional? Something like:

// Process is terminating: other threads may already be gone and
// tracing may still be called late in exit, so intentionally skip
// freeing the tracer context. The OS reclaims the memory.
// This is not a leak; leak/static analysis findings here are expected.

Tools like the MSVC debug heap leak dump or Coverity may flag this at some point, so having the reason in the code will make triage much easier, i.e. we don't want someone "fixing" the leak later and bringing the UAF back :-)

Side note, the FreeLibrary path and Linux side still have the same class of issue, I'll follow up on those in a separate PR so this one isn't blocked.

@mschilling0
mschilling0 force-pushed the dev_windows branch 2 times, most recently from 29eae86 to ad893a9 Compare December 2, 2026 02:31
Matches what's being done here: https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain

`lpvReserved` is "non-NULL" when process is terminating and its not safe
to call the tracer context destructor there.

Signed-off-by: Schilling, Matthew <matthew.schilling@intel.com>
Co-authored-by: Russell McGuire <russell.w.mcguire@intel.com>
@mschilling0

mschilling0 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

No problem. Done, thanks! That's a good point. Yeah I tried to stay targeted at the issue I was seeing rather than just use AI to do everything. Keeps me relative confident in my manual validation :)

rwmcguir
rwmcguir previously approved these changes Dec 2, 2026
Update Copyright
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Level Zero loader ABI check

last-release (v1.36.0): ✅ no ABI problems. Report

Creating compatibility report ...
Binary compatibility: 100%
Source compatibility: 100%
Total binary compatibility problems: 0, warnings: 0
Total source compatibility problems: 0, warnings: 0

master (master): ✅ no ABI problems. Report

Creating compatibility report ...
Binary compatibility: 100%
Source compatibility: 100%
Total binary compatibility problems: 0, warnings: 0
Total source compatibility problems: 0, warnings: 0

Workflow run

If an ABI change is intended, a reviewer with write access can accept it: leave an approval review on the PR head commit with the keyword abi-failure-approved in the review body. That re-runs the check.

@rwmcguir
rwmcguir merged commit 824a62f into oneapi-src:master Dec 2, 2026
71 checks passed
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