[BUG] Startup hooks fail on .NET 6
See original GitHub issueAPM 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:
- Created 2 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top GitHub Comments
@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.
Thanks for reporting - I’m looking into this.