Skip to content

Consider a hot reload feature switch for linking #51159

@stephentoub

Description

@stephentoub

Background and Motivation

We are going to have a variety of places across the core libraries that use MetadataUpdateHandlerAttribute to react to hot reload updates. Some of these handlers will have zero impact unless used (other than their presence in the library binaries) while others will have additional code on "normal" execution code paths, e.g. to track things that later need to be cleared. We should look at either using an existing feature switch (e.g. Debugging.IsSupported) or a new feature switch (e.g. AssemblyExtensions.IsApplyUpdateAvailable) that we tie all of these to and use to both link away and disable any expense when not required. We should aim to use the same switch across the whole stack (currently ASP.NET is using its own property).

Proposed API

namespace System.Reflection.Metadata
{
    public static class AssemblyExtensions
    {
        /// <summary>
        /// Returns true if the apply assembly update is enabled and available.
        /// </summary>
        public static bool IsApplyUpdateAvailable { get; }
    }
}

The .NET Core runtime will basically implement like this (and will cache the value in a static):

    IsApplyUpdateAvailable => Debugger.IsAttached || Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES") != "" || Environment.GetEnvironmentVariable("COMPlus_ForceEnc") == "1";

The feature switch support in the runtime build files include this property so the conditional apply update code is elided on release builds.

Usage Examples

    if (AssemblyExtensions.IsApplyUpdateAvailable)
    {
        // Hot reload/ENC specific initialization or bookkeeping code
    }

The hot reload agent can use this to ensure that it can actually apply updates to the application. Third party libraries can use it to run some apply update specific initialization or bookkeeping code.

Risks

Low. This API is used only in very limited cases and by very few developers (mostly internal).

Metadata

Metadata

Labels

api-approvedAPI was approved in API review, it can be implementedarea-MetablockingMarks issues that we want to fast track in order to unblock other important worklinkable-frameworkIssues associated with delivering a linker friendly frameworksize-reductionIssues impacting final app size primary for size sensitive workloads

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions