[ILLink] Add regression test for KeyNotFoundException crash in async partial methods with capturing lambdas#126733
Draft
[ILLink] Add regression test for KeyNotFoundException crash in async partial methods with capturing lambdas#126733
Conversation
…h capturing lambdas Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/2e76c507-3b5f-49ad-9dc8-d7865f98a645 Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Reproduce ILLink trimmer crash for async state machines
[ILLink] Fix KeyNotFoundException crash for async partial methods with capturing lambdas
Apr 10, 2026
Member
|
@copilot Please revert the fix for now, just add the test |
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/18a5443f-c80e-4ac5-931b-d8ebe7171db3 Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Contributor
Author
Copilot
AI
changed the title
[ILLink] Fix KeyNotFoundException crash for async partial methods with capturing lambdas
[ILLink] Add regression test for KeyNotFoundException crash in async partial methods with capturing lambdas
Apr 10, 2026
This was referenced Apr 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Roslyn does not emit
[AsyncStateMachineAttribute]onasync partialmethods. ILLink'sTryGetStateMachineTyperelies exclusively on this attribute, so the state machine type is never added togeneratedTypeToTypeArgs. When a capturing lambda insideMoveNextcreates a display class whose generic parameter'sOwneris the missing state machine type,MapGeneratedTypeTypeParameterscrashes withKeyNotFoundException.Changes
CompilerGeneratedTypes.cs— AddedPartialAsyncMethodWithLambdaregression test: a nested partial class with a genericasync partialmethod that captures a lambda referencing the generic type parameter. This test reproduces the crash described in the linked issue.Note
This PR was authored with the assistance of GitHub Copilot.
Original prompt
[Chronological Review] 1. Initial request: User wanted to reproduce the ILLink trimmer crash from #122800 - a KeyNotFoundException in MapGeneratedTypeTypeParameters when processing compiler-generated async state machine types for generic methods.Exploration phase: Read the issue, related issues ([area-Tools-ILLink]: Key Not Found: 'QuickGraph.Collections.FibonacciHeap`2/<GetEnumerator>d__8' #109821), Kebechet/Maui.Health fix commits, and deeply analyzed CompilerGeneratedState.cs (the buggy code), CompilerGeneratedNames.cs (name matching), and existing test cases.
First repro attempts: Created test project at C:\Users\angocke\tmp\link-crash with ReproLib (class library), ReproApp (console with trimming), and Inspector (reflection tool). Multiple attempts with generic async methods + lambdas did NOT crash the trimmer.
Key breakthrough: Downloaded the actual Maui.Health NuGet package (v2.0.0-preview9, the pre-fix version) and inspected it with Cecil. Discovered that
GetHealthDataandWriteHealthDataare async methods with state machines but are MISSING[AsyncStateMachineAttribute]. OnlyGetCumulativeHealthDataAsync(a non-partial async method) had the attribute.Root cause identified: Roslyn does NOT emit
[AsyncStateMachineAttribute]onasync partialmethods. ILLink'sTryGetStateMachineTyperelies exclusively on this attribute to discover state machines. Without it, the state machine type is never added togeneratedTypeToTypeArgs, but the display class (created by a lambda in MoveNext) IS added — and its generic parameter's Owner references the missing state machine, causing KeyNotFoundException.Minimal repro created: A partial method declaration + async partial implementation with a generic-capturing lambda. Confirmed crash:
KeyNotFoundException: The given key 'ReproLib.Service/<GetData>d__01' was not present in the dictionary.`Test case added: Added the repro as a test in CompilerGeneratedTypes.cs in the ILLink test suite.
Test validation: Tried running tests. Analyzer tests (ILLink.RoslynAnalyzer.Tests) built and passed (2/2). The full linker tests (Mono.Linker.Tests) couldn't build due to missing VS C++ tools on the machine, and the prebuilt test runner had a pre-existing version mismatch error (ProducedBy not found) unrelated to the changes.
Most recent: User said "cd ~/code/runtime", agent changed directory. Then user requested conversation summary.
[Intent Mapping]
[Technical Inventory]
[Code Archaeology]
[Progress Assessment]
[Context Validation]
All critical information captured including exact file paths, line numbers, crash mechanism, and test code.
[Recent Commands Analysis]
The most recent commands were:
cd C:\Users\angocke\code\runtime; Get-Location- Changed to the runtime directorygit --no-pager diffshowing the final diff of CompilerGeneratedTypes.cs1. Conversation Overview: - Primary Objectives: "I'd like to repro the user error in https://github.com//issues/122800" — reproduce the ILLink trimmer crash (KeyNotFoundException in MapGeneratedTypeTypeParameters) and "add this as a test case in ~/code/runtime/src/illink" - Session Context: Started with issue analysis → explored ILLink source code → created repro project → multiple failed attempts → downloaded actual Maui.Health NuGet and inspected IL with Cecil → discovered root cause (missing AsyncStateMachineAttribute on async partial methods) → created minimal repro → confirmed crash → added test case to CompilerGeneratedTypes.cs → validated with analyzer tests - User Intent Evolution: Started as "reproduce the crash" → evolved to "add as test case" after repro was achieved- Technical Foundat...
Created from Copilot CLI via the copilot delegate command.