Skip to content

[cDAC] Include the highest assigned ID in IdToThread lookup#130889

Merged
max-charlamb merged 2 commits into
dotnet:mainfrom
max-charlamb:max-charlamb/cdac-idtothread-highest-id
Jul 17, 2026
Merged

[cDAC] Include the highest assigned ID in IdToThread lookup#130889
max-charlamb merged 2 commits into
dotnet:mainfrom
max-charlamb:max-charlamb/cdac-idtothread-highest-id

Conversation

@max-charlamb

Copy link
Copy Markdown
Member

Summary

IdDispenser::IdToThread in the runtime resolves a thread by ID using an
inclusive upper bound (if (id <= m_highestId)). The cDAC
IThread.IdToThread implementation used an exclusive < comparison and
therefore never resolved the thread occupying the highest assigned ID slot --
an off-by-one that returned a null thread pointer for the highest valid ID.

Native semantic parity

  • Native IdDispenser::IdToThread (src/coreclr/vm/threads.h):
    if (id <= m_highestId) result = m_idToThread[id];
  • Thread_1.cs now uses id <= idDispenserObj.HighestId, matching the
    inclusive bound.
  • The Thread.md contract pseudocode is updated to match.

This change is limited to the IdToThread lookup boundary. Recycled
dispenser slot validation (the free-list check in
IdToThreadWithValidation) is intentionally out of scope.

Testing

  • Added ThreadTests.IdToThread_IncludesHighestId (runs on all four
    architecture combinations) verifying the highest ID resolves to the
    expected thread while HighestId + 1 returns null.
  • Full cDAC unit suite: 2705 passed, 16 skipped, 0 failed.

Note

This PR was generated with the assistance of GitHub Copilot.

The runtime's IdDispenser::IdToThread resolves a thread by ID using an
inclusive bound (`if (id <= m_highestId)`). The cDAC IThread.IdToThread
implementation used an exclusive `<` comparison, so it failed to resolve
the thread occupying the highest assigned ID slot -- an off-by-one that
made the highest valid ID always return a null thread pointer.

Match the native inclusive bound and add a boundary unit test that
verifies the highest ID resolves to the expected thread while the next
ID beyond it still returns null.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f2aa5dc4-0c99-43f0-af14-734143849c4a
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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.

@max-charlamb max-charlamb requested a review from rcj1 July 16, 2026 17:22

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 fixes an off-by-one boundary in the cDAC IThread.IdToThread contract implementation so that the lookup includes the highest assigned thread ID (inclusive upper bound), and adds coverage + updates the Thread contract documentation to reflect the intended behavior.

Changes:

  • Update Thread_1 contract implementation to use id <= HighestId when indexing IdToThread.
  • Add a new unit test validating that HighestId resolves while HighestId + 1 returns null.
  • Update the Thread contract spec pseudocode to use the inclusive bound.

Reviewed changes

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

File Description
src/native/managed/cdac/tests/UnitTests/ThreadTests.cs Adds a regression test covering IdToThread behavior at HighestId and HighestId + 1.
src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/Thread_1.cs Fixes the IdToThread upper-bound check to be inclusive.
docs/design/datacontracts/Thread.md Updates the spec pseudocode to match the inclusive bound behavior.

Comment thread docs/design/datacontracts/Thread.md Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 16, 2026 17:28
@max-charlamb max-charlamb marked this pull request as ready for review July 16, 2026 17:28
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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

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

Comment thread docs/design/datacontracts/Thread.md
@max-charlamb max-charlamb marked this pull request as draft July 16, 2026 18:26
@max-charlamb max-charlamb marked this pull request as ready for review July 16, 2026 20:20
@max-charlamb max-charlamb enabled auto-merge (squash) July 16, 2026 20:20
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.
Some pipeline(s) encountered errors during trigger evaluation.

@max-charlamb

Copy link
Copy Markdown
Member Author

/ba-g OSX timeout

@max-charlamb max-charlamb disabled auto-merge July 17, 2026 00:10
@max-charlamb

Copy link
Copy Markdown
Member Author

/ba-g OSX timeout

@max-charlamb max-charlamb merged commit 3a7bba7 into dotnet:main Jul 17, 2026
76 of 79 checks passed
@max-charlamb max-charlamb deleted the max-charlamb/cdac-idtothread-highest-id branch July 17, 2026 00:11
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