question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Startup hooks fail on .NET 6

See original GitHub issue

APM Agent version

1.12.1

Environment

Operating system and version: Debian 11 (mcr.microsoft.com/dotnet/aspnet:6.0)

.NET Framework/Core name and version: 6.0.0

Host (useful for support): Version: 6.0.0 Commit: 4822e3c3aa

.NET SDKs installed: No SDKs were found.

.NET runtimes installed: Microsoft.AspNetCore.App 6.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Application Target Framework(s): net6.0

Describe the bug

When starting a .NET 6 application instrumented with the zero code change approach, the application crashes.

To Reproduce

Steps to reproduce the behavior:

With the .NET 6 SDK:

dotnet new console -n CrashRepro

cat <<EOF > Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

RUN apt update && apt install -y unzip && apt clean

WORKDIR /build
ADD https://github.com/elastic/apm-agent-dotnet/releases/download/1.12.1/ElasticApmAgent_1.12.1.zip elastic.zip
RUN unzip elastic.zip -d elastic && rm elastic.zip
COPY ./ ./

WORKDIR /build/CrashRepro
RUN dotnet publish --configuration Release --output out

FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app

COPY --from=build /build/elastic ./elastic
COPY --from=build /build/CrashRepro/out ./
ENTRYPOINT ["dotnet", "CrashRepro.dll"]

ENV DOTNET_STARTUP_HOOKS=/app/elastic/ElasticApmAgentStartupHook.dll
ENV ELASTIC_APM_ENVIRONMENT=test
ENV ELASTIC_APM_SECRET_TOKEN=redacted
ENV ELASTIC_APM_SERVER_URL=redacted
ENV ELASTIC_APM_SERVICE_NAME=crash-repro

USER 1000
EOF

docker build . -t apm-crash-repro
docker run -it --rm apm-crash-repro

Substitute .NET 5 to show that the same steps succeed on that version.

Expected behavior

The application starts successfully as it does when targeting .NET 5.

Actual behavior

The application immediately crashes with this exception:

Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at StartupHook.InvokerLoaderMethod(Assembly loaderAssembly) in C:\Users\russc\source\apm-agent-dotnet\src\ElasticApmAgentStartupHook\StartupHook.cs:line 179
   at StartupHook.Initialize() in C:\Users\russc\source\apm-agent-dotnet\src\ElasticApmAgentStartupHook\StartupHook.cs:line 118
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)
   at System.StartupHookProvider.ProcessStartupHooks()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gregkalaposcommented, Feb 9, 2022

@twogood the release with the fix is out: https://github.com/elastic/apm-agent-dotnet/releases/tag/1.14.0

Sorry, this issue took longer than I expected.

1reaction
gregkalaposcommented, Jan 12, 2022

Thanks for reporting - I’m looking into this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

VS2022 debugger crash with error Failed to read ...
VS2022 debugger crash with error Failed to read environment variable [DOTNET_STARTUP_HOOKS], HRESULT: 0x800700CB - Microsoft Q&A.
Read more >
Failed to read environment variable ...
net 7 install from 7.0.7 to 7.0.9 when the error started. Searching on DOTNET_STARTUP_HOOKS within Visual Studio's install location, I got a hit ......
Read more >
runtime/docs/design/features/host-startup-hook.md at main
Startup hooks are disabled by default on trimmed apps. The usage of startup hooks on a trimmed app is potentially dangerous since these...
Read more >
Disable DOTNET_STARTUP_HOOKS in ASP.NET Core 5 ...
Startup hooks are disabled by default on trimmed apps. Not sure if you're in the position to do app trimming, but if you...
Read more >
NET 6.0 - Global Error Handler Tutorial with Example
This is a quick post to show how to implement a global exception handler in .NET 6.0. These are the main pieces involved...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found