Skip to content

Send profiler ClassUnload callbacks for array types - #136862

Merged
jkotas merged 2 commits into
dotnet:mainfrom
pcshrosbree:fix/classunload-arrays
Nov 3, 2026
Merged

jkotas merged 2 commits into
dotnet:mainfrom
pcshrosbree:fix/classunload-arrays

Conversation

@pcshrosbree

@pcshrosbree pcshrosbree commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: Profilers get load callbacks for array types but no matching unload callbacks. This PR adds the missing unload callbacks and updates two profiler tests to cover array unloading.

Related to #120799.

This PR makes ClassLoader::NotifyUnload report array types, so profilers get the unload callbacks that match the array load callbacks added in #120796. It also updates two profiler tests: classload now counts arrays, and getappdomainstaticaddress now runs up to ten collections after Unload() and skips arrays in its unload callback.

The gap

#114107 added ClassLoader::NotifyUnload and the classload test. NotifyUnload skipped the same types that NotifyLoad skipped then: method tables that contain generic variables, and arrays. #120796 then made NotifyLoad report array method tables, but NotifyUnload still returns early for arrays. When a collectible module unloads, Module::FreeClassTables calls NotifyUnload for each array type in the module's table. So for T[], with T in a collectible AssemblyLoadContext, a profiler gets ClassLoadStarted and ClassLoadFinished but never ClassUnloadStarted or ClassUnloadFinished. The ClassID it was given stops being valid without notice.

Change

NotifyUnload no longer checks IsArray(), so it skips the same types as NotifyLoad: method tables that contain generic variables.

Test

The classload profiler test could not see the gap. It counts only types whose name contains UnloadLibrary.TestClass, and the shared test profiler's GetClassIDName names every array "ArrayClass". The test now names an array by its element type (through IsArrayClass), so it counts TestClass[], which List<TestClass> allocates. It now expects 8 loads and 8 unloads instead of 7 and 7.

The fix also showed a gap in the getappdomainstaticaddress test. In ClassUnloadStarted, its profiler calls GetClassIDInfo2. That call returns CORPROF_E_CLASSID_IS_ARRAY for the array types the runtime now reports. The profiler now skips arrays there, as its ClassLoadFinished already does. The test missed this: it called GC.Collect() before Unload() and did not collect again afterward, and the earlier runs show no module-unload callback. The test now holds a WeakReference to the context and runs up to ten collections, stopping once the context is gone.

Validation

I ran the validation with a Checked runtime and Release libraries at main c772c4c. The table compares runs without the runtime fix, with it, and after reverting it again. For the last run, I rebuilt the runtime a second time and ran classload on its own.

platform classload without the fix with the fix fix reverted again
linux-x64 fails: 8 loads, 7 unloads passes fails: 8 loads, 7 unloads
osx-arm64 fails: 8 loads, 7 unloads passes fails: 8 loads, 7 unloads
windows-arm64 fails: 8 loads, 7 unloads passes fails: 8 loads, 7 unloads

I ran the updated getappdomainstaticaddress test four ways per platform. It passes without the runtime fix. With the runtime fix but without the profiler's array skip, it fails with CORPROF_E_CLASSID_IS_ARRAY. It passes with the runtime fix and the skip. It fails again when only the skip is removed.

With the runtime fix and both updated tests, every test under src/tests/profiler (Pri 1 build) passes: 31 on linux-x64 and osx-arm64, and 32 on windows-arm64, where ijw also runs. The four JitOptimizationSensitive tests (DynamicOptimization, eventpipe, multiple, rejit) ran with DOTNET_TieredCompilation=0. In the earlier full runs without the runtime fix, made before the getappdomainstaticaddress change, only classload failed.

Note

This PR was prepared with AI assistance (Anthropic Claude), under my direction. I reviewed the change and this description before posting.

dotnet#120796 made ClassLoader::NotifyLoad report array method tables, but
NotifyUnload still returns early for arrays. When a collectible module
unloads, Module::FreeClassTables calls NotifyUnload for each array type
in the module's table. So a profiler got ClassLoadStarted and
ClassLoadFinished for T[], but never ClassUnloadStarted or
ClassUnloadFinished.

Drop the IsArray check, so NotifyUnload skips the same types as
NotifyLoad.

The classload profiler test could not see this, because the test
profiler names every array "ArrayClass". The test now names arrays by
their element type, so it counts TestClass[], which List<TestClass>
allocates. It expects 8 loads and 8 unloads; without the runtime change
it sees 8 loads and 7 unloads.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 2, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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.

🟡 Changes recommended

The new array callbacks cause the profiler’s unload handler to fail on array class IDs, while the test’s collection before Unload() can miss this failure.

1 open issue
What changed in this PR

This PR restores matching profiler load and unload notifications for array types in CoreCLR, completing the array callback support introduced in #120796.

Changes:

  • Allow array types through ClassLoader::NotifyUnload, while still excluding types with unbound generic variables.
  • Update the classload profiler test to recognize arrays by element type and expect eight load/unload pairs.
  • Correct a comment in module cleanup to describe the skipped TypeDesc entries.
File Description
src/​tests/​profiler/​native/​classload/​classload.h Declares the array-aware type-name helper.
src/​tests/​profiler/​native/​classload/​classload.cpp Counts array callbacks and updates expected totals.
src/​coreclr/​vm/​clsload.cpp Enables profiler unload callbacks for array types.
src/​coreclr/​vm/​ceeload.cpp Corrects the cleanup comment; no behavior change.

Review effort: Balanced

Comment thread src/coreclr/vm/clsload.cpp Resolved
With array types now reported to ClassUnloadStarted, the
getappdomainstaticaddress test profiler calls GetClassIDInfo2 on them.
That returns CORPROF_E_CLASSID_IS_ARRAY, and the profiler counts it as a
failure. Skip array classes there, as ClassLoadFinished already does.

The test did not catch this: it called GC.Collect before Unload and did
not collect again afterward. Earlier runs show no module-unload
callback. The test now holds a WeakReference to the context and runs up
to ten collections after Unload, stopping once the context is gone.

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@jkotas
jkotas merged commit af7109c into dotnet:main Nov 3, 2026
107 of 109 checks passed
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Thank you for the review and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-TypeSystem-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants