Skip to content

Narrow active_sessions_mutex_ scope around ETW callback registration - #32000

Merged
Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
fix/etw-callback-lock-order
Aug 13, 2026
Merged

Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
fix/etw-callback-lock-order

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

InferenceSession's constructor and destructor held active_sessions_mutex_ while calling into the telemetry and ETW sink callback registries, each of which takes its own registry lock. The ETW dispatch path acquires those locks in the opposite order: a capture-state notification runs the registered callback under the registry lock, and that callback reaches LogAllSessions, which takes active_sessions_mutex_. Two threads hitting these paths concurrently can form a lock cycle and hang the process.

Register and unregister the callbacks outside the active_sessions_mutex_ critical section so the two locks are never held at once. The registries are independently synchronized and do not require active_sessions_mutex_.

InferenceSession's constructor and destructor held active_sessions_mutex_ while calling into the telemetry and ETW sink callback registries, each of which takes its own registry lock. The ETW dispatch path acquires those locks in the opposite order: a capture-state notification runs the registered callback under the registry lock, and that callback reaches LogAllSessions, which takes active_sessions_mutex_. Two threads hitting these paths concurrently can form a lock cycle and hang the process.

Register and unregister the callbacks outside the active_sessions_mutex_ critical section so the two locks are never held at once. The registries are independently synchronized and do not require active_sessions_mutex_.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts InferenceSession ETW callback registration/unregistration on Windows to avoid a lock-order inversion between active_sessions_mutex_ and the ETW/telemetry callback registry locks, preventing potential deadlocks during ETW dispatch and session lifecycle events.

Changes:

  • Narrows the active_sessions_mutex_ critical section in ConstructorCommon so it isn’t held across ETW callback registration.
  • Updates teardown logic to avoid holding active_sessions_mutex_ while unregistering ETW callbacks.

πŸ’‘ Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@apsonawane
Akshay Sonawane (apsonawane) merged commit 39551be into main Aug 13, 2026
97 of 100 checks passed
@apsonawane
Akshay Sonawane (apsonawane) deleted the fix/etw-callback-lock-order branch August 13, 2026 17:54
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.

3 participants