-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider adding a runtime diagnostic debug feature for trimmed applications #50130
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
cc @agocke |
I think we should start with defining the developer experience, and how it fits with all other existing developer experiences around trimming. Do we have real world examples that show where the existing developer experiences around trimming fall short? |
I am worried that investing into diagnostics like this will just encourage bad behavior and distract the focus on providing predictable experience. |
Tagging subscribers to 'linkable-framework': @eerhardt, @vitek-karas, @LakshanF, @tannergooding, @sbomer Issue DetailsTrimming a .NET Core application is expected to give size wins without compromising correctness. However, there are some coding patterns, largely centered around arbitrary use of reflections, that make it challenging for the Trimmer to reason about correctness. Serializers (such as XML and DataContractSerilaizers), feature area like EventSource, and other make heavy use of such patterns. The trimmer does a good job of warning in these cases but for scenarios where these warnings are incorrectly suppressed, trimmed applications can fail in production environment that will be hard to root cause. We should provide a runtime diagnostic feature that can be leveraged in opt-in scenario testing mode that will warn developers of potential problems in their app. Such a feature should have the following:
|
Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov Issue DetailsTrimming a .NET Core application is expected to give size wins without compromising correctness. However, there are some coding patterns, largely centered around arbitrary use of reflections, that make it challenging for the Trimmer to reason about correctness. Serializers (such as XML and DataContractSerilaizers), feature area like EventSource, and other make heavy use of such patterns. The trimmer does a good job of warning in these cases but for scenarios where these warnings are incorrectly suppressed, trimmed applications can fail in production environment that will be hard to root cause. We should provide a runtime diagnostic feature that can be leveraged in opt-in scenario testing mode that will warn developers of potential problems in their app. Such a feature should have the following:
|
One concrete place where this feature would be helpful is in the following Lines 1148 to 1160 in 5533388
What this code does is given a Type (which is either an public async Task Invoke(HttpContext httpContext)
{
var startTimestamp = Stopwatch.GetTimestamp();
if (_diagnostics.IsEnabled("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareStarting"))
{
_diagnostics.Write(
"Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareStarting",
new
{
name = _middlewareName,
httpContext = httpContext,
instanceId = _instanceId,
timestamp = startTimestamp,
});
} This is problematic because the trimmer won't see anyone using the One approach to mitigating this problem is to log a message to the log that says |
Does the linker diagnostic produce warning for this case? |
Yes, it produces a warning, which is what I'm trying to address now as part of #45623: runtime/src/libraries/System.Diagnostics.DiagnosticSource/src/ILLink/ILLink.Suppressions.xml Lines 4 to 9 in 31385d4
My current thinking is that since there isn't a public API that can be annotated here to preserve the properties, the next best thing would be to give a runtime informational message via the log that tells the user their diagnostic information is incomplete. |
Note that we would also like to have the freedom to strip the properties themselves if we see they're not being reflected. We should assume that the code will not see any properties at all (not just properties with missing getters). |
It might be worth pointing out that for the above ASP.NET example, the diagnostic message would be along the lines of: Type '<>f__AnonymousType0`2[System.String,System.Int32,System.DateTime]' has been modified by trimming. Ensure the necessary properties are preserved to get full diagnostic information. Not even I would be able to figure out what to do with that. |
We could put the fully qualified type name, so at least you would know what assembly it is coming from. Also, it looks like C# embeds the property names into the generic type names, so you would get the names of the properties in the type name as well, which gives you more information about which type it is referring to:
Offline @jkotas had the suggestion to add a new generic |
Are we still considering this feature? Or should this issue be closed? |
There are no plans to add this right now - closing. |
Opening to reconsider for 7.0. |
I don't think we should do this. Over the course of .NET 5 and 6 we put a lot of effort into making sure that the behavior of the app before/after trimming is the same (and one doesn't need to debug their trimmed app to iron out problems caused by trimming). The only possible use for this diagnostic is to aid in experience that we explicitly don't want to be part of trimming - having to debug the trimmed app because trimming changes the behavior without warnings. Adding a feature like this will only encourage the ecosystem to build solutions that are fundamentally not compatible with trimming and pat themselves on the back for "doing the work to make it compatible with trimming" (by which they'll mean suppressing trimming warnings, and putting a runtime assert in place). |
Is there any new information here that caused us to reconsider? Otherwise I think the previous decision should stand. |
I agree with @MichalStrehovsky and @jkotas comments above in not pursuing this runtime feature. The position of having the same behavior for trimmed/non-trimmed application allows us to provide a predictable experience for the developer. This feature will fundamentally break that principle. |
When we looked at ASP.NET Core + STJ, we found it hard to come up with patterns where we could guarantee that a type was not going to use the reflection based code path. The best option we came up with was a way to use the source generated context if it has metadata, and fallback to a reflection based code path if it does not contain that information. The failure behavior for the fallback is really poor for trimmed types and we had hoped having some way to indicate to the user that it failed was important. |
The primary issue is that the default ASP.NET Core + STJ is not really compatible with trimming, and the .NET 6 trimming-compatible model that we were able to come up with is not pleasant to use. In .NET 7, we have to double down on designing a model that is both trimming-compatible and pleasant to use. It will likely require work from accross the team: ASP.NET, libraries - serializers, source generators - Roslyn, runtime, linker.
The experience would be still poor even with this diagnostic feature. The apps are still going to break at will, one just will have more details about the break. It is not a viable option for larger actively developed apps where debugging failures like this is prohibitively expensive. |
Trimming a .NET Core application is expected to give size wins without compromising correctness. However, there are some coding patterns, largely centered around arbitrary use of reflections, that make it challenging for the Trimmer to reason about correctness. Serializers (such as XML and DataContractSerilaizers), feature area like EventSource, and other make heavy use of such patterns. The trimmer does a good job of warning in these cases but for scenarios where these warnings are incorrectly suppressed, trimmed applications can fail in production environment that will be hard to root cause.
We should provide a runtime diagnostic feature that can be leveraged in opt-in scenario testing mode that will warn developers of potential problems in their app. Such a feature should have the following:
Metadata in trimmed assemblies on trimmed types. This issue tracks the request in the mono repo
Library support to query the trimmed assemblies to get trimmed metadata
Showcase this feature using a trim-problematic component
Developer Experience to leverage this feature including a mechanism for make problematic types trim safe
The text was updated successfully, but these errors were encountered: