DataDog / dd-trace-dotnet

.NET Client Library for Datadog APM
https://docs.datadoghq.com/tracing/
Apache License 2.0
449 stars 141 forks source link

Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401) #475

Closed s-tokutake closed 4 years ago

s-tokutake commented 5 years ago

Describe the bug


Exception: System.SystemException

Message: Failed to create AppDomain.

StackTrace:    at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
   at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironmentAndReportErrors(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
   at System.Web.Hosting.ApplicationManager.GetAppDomainWithHostingEnvironment(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
   at System.Web.Hosting.ApplicationManager.CreateObjectInternal(String appId, Type type, IApplicationHost appHost, Boolean failIfExists, HostingEnvironmentParameters hostingParameters)
   at System.Web.Hosting.ProcessHost.StartApplication(String appId, String appPath, Object& runtimeInterface)

InnerException: System.IO.FileLoadException

Message: Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)

StackTrace:    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(String assemblyName, String typeName)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
   at System.AppDomain.CreateAppDomainManager()
   at System.AppDomain.Setup(Object arg)
   at System.AppDomain.nCreateDomain(String friendlyName, AppDomainSetup setup, Evidence providedSecurityInfo, Evidence creatorsSecurityInfo, IntPtr parentSecurityDescriptor)
   at System.AppDomain.InternalCreateDomain(String friendlyName, Evidence securityInfo, AppDomainSetup info)
   at System.AppDomain.CreateDomain(String friendlyName, Evidence securityInfo, AppDomainSetup info)
   at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)

To Reproduce

  1. install datadog-dotnet-apm-1.6.1-x64.msi
  2. exec iisreset
  3. access the application.

Runtime environment (please complete the following information):

Additional context

─ App1 (asp.net webforms) ok
        ├─App2(asp.net mvc) ok
        ├─App3(asp.net webapi) ok
        └─App4(asp.net webforms) System.IO.FileLoadException occurred!!
colin-higgins commented 5 years ago

Hi @s-tokutake, is this new to 1.6.1 or did this happen in 1.6.0 as well? Is there anything about the nested web forms App4 that is particularly different from the others? Does it have specific security settings?

s-tokutake commented 5 years ago

@colin-higgins thanks for your reply.

is this new to 1.6.1 or did this happen in 1.6.0 as well?

Does it have specific security settings?

colin-higgins commented 5 years ago

@s-tokutake Do you know if any of the web applications are consuming the Nuget package (https://www.nuget.org/packages/Datadog.Trace.ClrProfiler.Managed/)? If they are, would you mind listing the versions for each app? In fact, if you wouldn't mind sharing the dependencies & versions for each application, that could greatly help us troubleshoot this issue. Thank you.

s-tokutake commented 5 years ago

Do you know if any of the web applications are consuming the Nuget package )?

No. These apps don't use Datadog.Trace.ClrProfiler.Managed .

ItsMeBrianD commented 5 years ago

Would like to chime in and note that we are also having this issue, our application has 1.6.1, and does use the Datadog.Trace.ClrProfiler.Managed

Are there any fixes for this yet?

prahutomo commented 5 years ago

I had the same issue. The workaround is to create a new registry DWORD value called LoaderOptimization under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework and set the value 1. Then do iisreset.

(edit: formatting)

lucaspimentel commented 5 years ago

Thanks, @prahutomo. Here is some documentation on the valid values for that setting.

Please keep in mind that forcing the loader optimization value to 1 (SingleDomain) is a workaround. The drawback is that multiple AppDomains in the same process will no longer be able share resources. For users with many applications running in the same IIS Application Pool, this setting can increase CPU and memory usage. Please use with caution!

Btw, we are actively working (https://github.com/DataDog/dd-trace-dotnet/pull/505) on loading our tracer assemblies in a different way that doesn't interfere with the ability to share assemblies between AppDomains. This should eliminate the error without the need for this workaround.

colin-higgins commented 5 years ago

While we are working through this. If anybody with the issue is able and willing to enable fuslogvw so that we can have a full picture of the issue you are having, that would be very helpful. Zipping up the directory and sending it to us would be greatly appreciated. image

lucaspimentel commented 4 years ago

We've done several changes over the last few months to alleviate this issue, if not solve it completely. There is also the workaround of changing the LoaderOptimization setting mentioned above.

We are closing this issue. Please re-open or create a new github issue if you encounter this again. Thanks.

SomeTroglodyte commented 10 months ago

Workaround still required and successful december 2023. Standalone windows forms project from back in 2010, no iis on the dev box - ergo no iisreset... VS updated earlier today... 🙈

rdvanbuuren-gasunie commented 3 months ago

This also fixed our problem creating assembly redirects. So it looks like it issue is still present nowadays.