Skip to content

Fix stale lastThrownObjectHandle in DAC GetThreadData during active exception dispatch#127741

Merged
hoyosjs merged 3 commits intodotnet:mainfrom
hoyosjs:juhoyosa/fix-pe
May 5, 2026
Merged

Fix stale lastThrownObjectHandle in DAC GetThreadData during active exception dispatch#127741
hoyosjs merged 3 commits intodotnet:mainfrom
hoyosjs:juhoyosa/fix-pe

Conversation

@hoyosjs
Copy link
Copy Markdown
Member

@hoyosjs hoyosjs commented May 4, 2026

After #127300 removed ExInfo::m_hThrowable and SetThrowable(), Thread::m_LastThrownObjectHandle is no longer updated during active exception dispatch. This causes a staleclastThrownObjectHandle to ge returned by GetThreadData when a debugger breaks into the target mid-dispatch.

When debugging a .NET 11 process with SOS (e.g. via dotnet-dump or WinDbg), the following commands fail if the debuggee is stopped during exception dispatch (for example, with SXE CLR):

  • !PrintException — reports "Invalid exception object" because it dereferences the stale handle and gets a null or recycled pointer
  • !Threads — shows no exception column for threads that are actively throwing
  • !clrstack -a with exception context — may show incomplete or missing exception info

Copilot AI review requested due to automatic review settings May 4, 2026 11:04
@hoyosjs
Copy link
Copy Markdown
Member Author

hoyosjs commented May 4, 2026

@jkotas @max-charlamb - This was a regression when trying to update the last set of runtime changes in SOS tests

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 fixes debugger-visible exception reporting when the target is stopped mid-exception-dispatch by preferring the active exception stored in the ExInfo tracker (via a pseudo-handle to the exception slot) instead of relying on Thread::m_LastThrownObjectHandle, which can be stale during active dispatch after #127300.

Changes:

  • Update native DAC ClrDataAccess::GetThreadData to return the active exception pseudo-handle when available, falling back to m_LastThrownObjectHandle.
  • Update the cDAC thread contract to compute ThreadData.LastThrownObjectHandle from the active ExceptionInfo.ThrownObject slot when present (same pseudo-handle semantics), with fallback.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/Thread_1.cs Prefer ExInfo-derived pseudo-handle for ThreadData.LastThrownObjectHandle; factor pseudo-handle computation into a helper used by both GetThreadData and GetCurrentExceptionHandle.
src/coreclr/debug/daccess/request.cpp Prefer Thread::GetThrowableAsPseudoHandle() for DacpThreadData::lastThrownObjectHandle in GetThreadData, falling back to m_LastThrownObjectHandle.

@hoyosjs hoyosjs marked this pull request as ready for review May 4, 2026 18:43
Copilot AI review requested due to automatic review settings May 4, 2026 18:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

@hoyosjs hoyosjs enabled auto-merge (squash) May 4, 2026 19:22
@hoyosjs
Copy link
Copy Markdown
Member Author

hoyosjs commented May 5, 2026

/ba-g cDAC issue is #127774 and other legs don't get touched by this PR for anything other than builds, so timeouts are unrelated

@hoyosjs hoyosjs merged commit 2e59c91 into dotnet:main May 5, 2026
115 of 123 checks passed
@hoyosjs hoyosjs deleted the juhoyosa/fix-pe branch May 5, 2026 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants